3

There's a class which some of its fields are user-defined objects. I'm going to:

  1. Get the Primary class' fields and traverse through them to get their values.

    1.1 When encountering a field with the type of object, go through this object which has its own fields

  2. Get the values of these nested fields (fields of the object type field)

The problem is at step 2; When I get the fields of the object field, couldn't get their values as I need to pass an object to field.get(object) to indicate which object I want the values of the fields being extracted from, But how can I access the current object of our talking field with the type of object?

Here's the code:

public class PrimaryClass {
    String str;
    int num;
    MyClass cls;
}


PrimaryClass primaryObject = new PrimaryClass();

Field[] primaryObjectFields = primaryObject.getClass().getDeclaredFields();

// ... One of the fields is :  MyClass foo.bar.cls
// Assuming stored with name of clsField 

Field[] myClassObjectFields = clsField.getType().getDeclaredFields();

for (Field f : myClassObjectFields) {

    String fieldValue = f.get(primaryObject /* What to pass here? */); 
    // !!!! The above line Doesn't work since the primary Object doesn't have access to its child's fields

    System.out.println(fieldValue);

}

When I get the first level field (and set it accessible with setAccessible(true)), To get its inner object field I call these:

topLevelField.getClass().getDeclaredField("details"); 
topLevelField.setAccessible(true);
topLevelField.get(primaryObject);

But couldn't get access to the object field which is instantiated inside the parent object and get this error:

java.lang.IllegalArgumentException: Can not set java.util.List field com.foo.Bar.details to com.foo.Bar

The inner object is a List of objects but could be also non-list objects in some cases.

DummyBeginner
  • 411
  • 10
  • 34

1 Answers1

1

Here is a cool tutorial that can help you get started. in general, get returns you an object, and then you can cast it to what ever type you want. also , you can ask the field for its type ,and the do some logic accordingly to the type of the field . there are also cool methods that is better for you to get familiar with clazz.isAssignableFrom(obj.getClass())

you can read more about it here

NiNiCkNaMe
  • 181
  • 5
  • 1
    Thanks for the answer, But I can't see how this may be mapped as a solution to the problem. When I get the first level field (and set it accessible with `setAccessible(true)`), To get its inner object filed use this: `topLevelField.getClass().getDeclaredField("details"); topLevelField.setAccessible(true); topLevelField.get(primarySetObject); ` But couldn't get access to the object filed instaniated inide the parent object and et this error: `java.lang.IllegalArgumentException: Can not set java.util.List field com.foo.Bar.details to com.foo.Bar`. The inner object is a `List` of objects – DummyBeginner Jun 08 '19 at 09:42
  • @DummyBeginner fisrt of all using 'topLevelField.setAccessible(true);' is bad habbit , and may cause production issues . second of all , if you have appropriate names to the vairables and getters and setters , you may use [Field Utils from apache commons](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/reflect/FieldUtils.html) and for list its a bit more complicated , but what i ment , is to have a if else statement , in which you handle fields by their type . – NiNiCkNaMe Jun 09 '19 at 18:35