1

I'm fairly new to java and have come across a problem. My task is to create a class which contains a method write(Object obj) that writes the type of the object as well as the name and type of all attributes into a file. Recursion is involved since the object may have objects/arrays of objects as attributes.

Here is the code:

    public void write(Object obj) throws Exception {

    if(obj == null)
    {
        out.close();
        return;
    }

    Class c = obj.getClass();
    Class d;
    Field fields_c[] = c.getDeclaredFields();
    System.out.println("class_name:" + c.getName());

    int i, j;
    String tab = new String("");


    for(i = 0; i < indent_level; i++)
    {
        tab = tab + "\t";
    }

    out.write(tab + "class_name:" + c.getName() + "\n");

    for(i = 0; i < fields_c.length; i++) {
        System.out.println("field name: " + fields_c[i].getName() + " ");

        c = fields_c[i].getType();
        fields_c[i].setAccessible(true);

        if(c.isPrimitive()) {
            out.write("\t" + tab + "field_name:" + c.toString() + "\n");
        }
        else if(c.isArray()) {
            System.out.println("field of type array with elements of type:" + c.getComponentType());

            for(j = 0; j < Array.getLength(c); j++)
            {
                d = Array.get(c, j).getClass();
                indent_level = indent_level + 1;
                this.write(d);
                indent_level = indent_level - 1;            
            }
        }
        else
        {
            System.out.println("field is not primitive of type:" + c.getName());
            Object foo = fields_c[i].get(obj);
            indent_level = indent_level + 1;
            this.write(foo);
            indent_level = indent_level - 1;
        }
    }
}

An exception arises if I call the method and give an Object that has an array attribute; all attributes until the array are written properly to the output file. The exception is "java.lang.IllegalArgumentException: Argument is not an array".

6
  • You have a large number of bad coding practices. Why would anyone give you the task to work on something that is rather abstract as this (accessing classes and fields and whatnot, then analyzing them) if you're fairly new to java? For example, you can initialise a String by just assigning a value. I think the exception is thrown because C is not an array at that moment but a field. Try casting it into an array... But idk how. You're bothering with abstract stuff that is pretty useless imo. Commented Apr 9, 2012 at 21:53
  • I think you are mixing things a bit up in your code. Do you want to print the attribute names and types only or do you want to include values to? If you only want the names and types, you don't need recursion and you don't have to iterate over the array's elements. Commented Apr 9, 2012 at 22:12
  • I haven't picked this assignment by my own free will... Commented Apr 9, 2012 at 22:12
  • Recursion is needed only if the object given as argument contains objects or arrays of objects of different types. They themselves may contain objects... Commented Apr 9, 2012 at 22:14
  • So you want to go into the contents of the attributes to? In your question you write you only want names and types. Commented Apr 9, 2012 at 22:19

3 Answers 3

1

In d = Array.get(c, j).getClass(); c is of type java.lang.Class, but an array is expected.

You should change it to d = Array.get(fields_c[i].get(obj), j) and use c#getComponentType for get the type of the array.

Sign up to request clarification or add additional context in comments.

6 Comments

I only take this branch if c.isArray() proves to be true. Or is there a cast that I must make.
A cast is needed. If if you've verified it's an array before the call, at that point the jvm doesn't know what the object actually is, other than an Object.
c is of type Class as Field#getType returns Class. isArray is a method of Class Class.
Now it appears to give a type mismatch: cannot convert from Object to Class.
Right after the if "if(c.isArray())". It still prints the type the array and its name as well, it crashes in that "for".
|
0

This may not be what you're after, but if this is to do with serialisation then I recommend "Simple";

http://simple.sourceforge.net/

It makes Java <=> XML serialisation unbelievably easy.

Comments

0

Why you're passing Class of elements instead of elements:

        Object[] array = fields_c[i].get(obj);
        for(j = 0; j < Array.getLength(array); j++)
        {
            Object foo = Array.get(array, j); // not .getClass()
            indent_level = indent_level + 1;
            this.write(foo);
            indent_level = indent_level - 1;            
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.