开发者

How to get value from POJO using reflection?

I am just create a One generic method using Method Reflection API. In This Method I am trying to get a particulate method(getter method / setter method) val开发者_运维知识库ue but I am stuck I don't know how to do this. I am Abel to get all the method Name using Method Reflection API but not Abel to get value of that method. So please help me.

here is my code......

/*

propertyNames List is contain two records.

   and that records are two different EntityName(Variable Name)

   ex. in my class i have declared two Entity(Variable)
   private Integer productId;
   private Integer bomBucket;

   so propertyNames List is contain [productId ,  bomBucket] this two records.

*/

 public <T>void fillList(List<String> propertyNames , T clas){

    try{
        for(Object entity : entities.keySet()){
            if(propertyNames != null && propertyNames.size() > 0){  
                Method[] methodList = clas.getClass().getMethods(); 
                Method methodName;
                for (String propertyName : propertyNames) {
                    for(Method method : methodList){
                        if(method.getName().trim().startsWith("set")){
                           if(propertyName.trim().equalsIgnoreCase(method.getName().substring(3, method.getName().trim().length()))){

                               methodName = clas.getClass().getMethod(method.getName().trim(),new Class[]{Integer.class});

                               System.out.println("HERE I GET METHOD NAME ::: " + methodName.getName());

                               /*
                                * here one by one i am getting all the Setter methods name from the class .
                                * but i want a that Setter methods values. Not return type.  
                                */
                           }
                       } 
                   }   
                }
            }
        }   

    }catch (Exception e) {
        e.printStackTrace();
    }
}


What do you mean by "value of the method" Do you mean the method's return type, or do you want to invoke that method, on a certain instance of it's parent class, maybe with some argumetns, and then get the value that it returns? In this case, if you're method is not public you must first call setAccessible(true) on the method object before you invoke it:

//get instance of the class that has your method
    DsrProcessor dsrProcessor = DsrProcessor.class.newInstance();
    //get method object (by passing in it's name and the list of argument types he's accepting - echo(String):
    Method m = DsrProcessor.class.getMethod("echo", String.class);
    //use reflection to invoke the method passing the instance of it's class and a list of arguments :
    String methodReturnValue = (String) m.invoke(dsrProcessor, "Hello");
    //do something with what the method returned
    System.out.println(methodReturnValue);//prints: "echo:hello"


You can also use the java.beans.Introspector class and one of its getBeanInfo method. It returns an instance of BeanInfo which references all property descriptors java.beans.PropertyDescriptor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜