function - Java get method from generic Class object -


i have method following class holds list of objects of 2 different classes can passed in.

public class datatableentity {  list<objects> objectcontainer;   public void setlinks(class cls){  for(object obj : objectcontainer){   //the cls variable hold type of object    //so need somehow if 'dog' obj going through dog   // , method dog.getname else might cat , cat.getcatname etc.  } } 

this function gets told list , should format property of objects in list correctly before being sent out client. different objects have different setter , getter names these properties. need somehow take generic class object, see kind of class can know method, ".getabc/.setabc" or ".getxyz/.setxyz" use.

is possible?

like ?

public class datatableentity {     list<objects> objectcontainer;      // objects know class are, no need pass     public void setlinks() {         for(object obj : objectcontainer) {             if(obj instanceof classa) {                 classa = (classa)obj;                 a.dosomething();             }             else if(obj instanceof classb) {                 classb b = (classb)obj;                 b.dosomethingelse();             }             // , on ...         }     } } 

a common inteface make code better, because wouldnt have check different classes. dont know if possible case


Comments

Popular posts from this blog

c++ - Linked List error when inserting for the last time -

java - activate/deactivate sonar maven plugin by profile? -

java - What is the difference between String. and String.this. ? -