mardi 15 juin 2021

Is there a way to get all Memberclasses of a class and loop over them to excecute methods?

I have an ManagerCLass, which includes many other Objects. Here are methodes, that takes thes Objects and call an method on theses Objects.. Example:

public class Manager extends BaseManager {



ClassA classA = new ClassA();
ClassB classB = new ClassB();
ClassC classC = new ClassC();
ClassD classD = new ClassD();
ClassE classE = new ClassE();


public void callMethodsOnObjects() {
    classA.printResult();
    classB.printResult();
    classC.printResult();
    classD.printResult();
    classE.printResult();
}

}

These classes have all the same Superclass. Now my Question is, is there a way to automate the callMethodsOnObjects()-method?

My Idea was to get all declaredClasses of the Managerclass. Then to Loop of the array an excecute the printResult()-methode on each Object.

Class<?>[] classes = Manager.class.getDeclaredClasses();

    for (int i = 0; i < classes.length; i++) {
        ....
    }

But this don´t work. The Array is Empty.

So do you have an Idea if this a way to automate this? There are still more methods that are structured this way. I'm not getting anywhere here. Does it make sense to do it this way, as I imagined it?





Aucun commentaire:

Enregistrer un commentaire