Java 如何使用反射从POJO获得价值?

Java 如何使用反射从POJO获得价值?,java,reflection,Java,Reflection,我只是使用方法反射API创建一个通用方法。在这个方法中,我试图获得一个微粒方法(getter方法/setter方法)值,但我被卡住了,我不知道如何做到这一点。我是使用方法反射API获取所有方法名称的Abel,而不是获取该方法值的Abel。所以请帮帮我 这是我的密码 /* propertyNames列表包含两条记录 and that records are two different EntityName(Variable Name) ex. in my class i have d

我只是使用方法反射API创建一个通用方法。在这个方法中,我试图获得一个微粒方法(getter方法/setter方法)值,但我被卡住了,我不知道如何做到这一点。我是使用方法反射API获取所有方法名称的Abel,而不是获取该方法值的Abel。所以请帮帮我

这是我的密码

/*
propertyNames列表包含两条记录

   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 void fillList(列表属性名称,T类){
试一试{
for(对象实体:entities.keySet()){
如果(propertyNames!=null&&propertyNames.size()>0){
方法[]methodList=clas.getClass().getMethods();
方法methodName;
for(字符串propertyName:propertyNames){
对于(方法:方法列表){
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(),新类[]{Integer.Class});
System.out.println(“这里我得到方法名:::”+methodName.getName());
/*
*在这里,我一个接一个地从类中获取所有Setter方法的名称。
*但是我想要一个Setter方法值,而不是返回类型。
*/
}
} 
}   
}
}
}   
}捕获(例外e){
e、 printStackTrace();
}
}
您所说的“方法的值”是什么意思?您是指该方法的返回类型,还是希望在其父类的某个实例上调用该方法(可能带有一些参数),然后获取其返回的值?在这种情况下,如果您认为方法不是公共的,则在调用该方法对象之前,必须首先对该方法对象调用setAccessible(true):

//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"

您还可以使用java.beans.Introspector类及其getBeanInfo方法之一。
它返回引用所有属性描述符的BeanInfo实例。

您能发布代码吗?你的问题几乎没有意义。我添加了我的代码,请检查。这是你的答案,但我也使用了PropertyDescriptor,但仍然没有得到任何解决方案。
//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"