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 ?
I know I can get list of getter methods via reflection. But I don't know how I can use it in this situation.
BTW this question is not about how to get list of methods from using reflection. It is more about how to use it or how reflection works.
Aucun commentaire:
Enregistrer un commentaire