简体   繁体   中英

How can I get the instance of an object that is determined in the class

I have a class that has structure containing other classes, each of these classes also may contain different classes. So this is tree structure of classes.

public class FirstLevelClass {
   public SecondLevelClass1 secondLevelClass1;
   public SecondLevelClass2 secondLevelClass2;
   ...
}

public class SecondLevelClass1 {
   public ThirdLevelClass thirdLevelClass;
   public String fieldSecondLevelClass;
   ...
}

I have initialized variable of FirstLevelClass.

FirstLevelClass firstLevelClass;

I need to get instance of secondLevelClass1 that is stored inside firstLevelClass. How can I do that having a name of the class SecondLevelClass1?

Basically I need to get value of the field inside SecondLevelClass1. But I failed when wrote this:

String fieldName = "fieldSecondLevelClass";
Field[] fields = firstLevelClass.getClass().getDeclaredFields();
Object foundValue = null;

for (Field field : fields) {
   Field foundField = null;
   foundField = field.getType().getDeclaredField(fieldName);
   foundValue = fieldSecondLevelClass.get((Class.forName(foundField.getDeclaringClass().getName())).newInstance());
}

Because the instance that I call fieldSecondLevelClass.get(...) with isnt initialized. But if i write

foundValue = foundField.get(((FirstLevelClass) firstLevelClass).secondLevelClass1);

it will work fine. So, do any of you ideas how can I pass right instance of secondLevelClass1 using the name of it and instance of FirstLevelClass to fieldSecondLevelClass.get(...)?

It is very difficult to understand what you are trying to do, but I will attempt to simplify this for you.

First, your fields are declared public. And even if they were not, you could expose getters/accessors, so there is no reason for reflections.

It's also a red flag to me that you have a class for first level, another class for second level, and a third class for third level. I suppose it depends on how much these types vary in their interface. If this tree needs to go any deeper than 3 levels, I suggest abstracting these out into a generic Node class which has references to its children and you can nest them in any order.

public class Node {
    private List<Node> children = new ArrayList<>();

    public void addChild(Node c) {
       children.add(c);
    }

    public Node[] getChildren() {
        return children.toArray(new Node[0]);
    }
}

public class SomethingElse {
    public static void main(String[] args) {
        Node firstLevel = new Node();
        Node secondLevel = new Node();
        secondLevel.addChild(new Node());
        firstLevel.addChild(secondLevel);
    }
}

This would be much easier to work with. However, the design cost is that it forces all nodes at all tree depths to have identical behavior. If that is not desired, then this may not work well for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM