jeudi 23 juin 2016

Java: Can we iterate over all methods of the class?

Lets say I have following class:

public class ExampleList{
  // fields

  List<A> getAList(){}
  List<B> getBList(){}
  List<C> getCList(){}
  //A, B and C all extends class X with field num
}

public class Example{
  ExampleList getExampleList(){}
}

public class Test{
  main(){
    Example example = //from somewhere I get object;

    List<A> lA = example.getExampleList().getAList();
    List<B> lB = example.getExampleList().getBList();
    List<C> lC = example.getExampleList().getCList();

    //Currently I am doing
    if(lA != null) { 
      //iterate and call getCount(num)

    if(lB != null) { 
      //iterate and call getCount(num)

    if(lC != null) { 
      //iterate and call getCount(num)

  }

  getCount(int num) { //do something }
}

What I would like to do is dynamically iterate over all the methods of ExampleList and call getCount(num) only once. like:

main(){
  for ( Methods mList : Methods)
     for ( X x: mList )
       getCount(x.getNum);
}

I know I can create a generic method which takes List of anything that extends X and I can iterate each List there and call getCount(). But I also want to be able to iterate over methods of a class. Is there a way I can achieve this ?





Aucun commentaire:

Enregistrer un commentaire