繁体   English   中英

如何使用反射API访问bean类的getter方法?

[英]How to access getter method of bean class using reflection api?

如果我使用Reflection API实例化类,为什么variable2的值为null?

其中,根据设置正确返回了Variable1的值,在这里我正常实例化了该对象。 如何使用ReflectionAPI获取variable2的值?

    package com.OP.app;

    public class Bean {

    private String variable1;
    private String variable2;
    public String getVariable1() {
        return variable1;
    }
    public void setVariable1(String variable1) {
        this.variable1 = variable1;
    }
    public String getVariable2() {
        return variable2;
    }
    public void setVariable2(String variable2) {
        this.variable2 = variable2;
    }


}

package com.OP.app;

import java.lang.reflect.Method;

public class ObjectCall {

    public static void main(String []args){
        Bean beanobject = new Bean();
        beanobject.setVariable1("Ram");
        beanobject.setVariable2("Rakesh");
        System.out.println(beanobject.getVariable1());
        String path = "com.OP.app.Bean";
        Class<?> newClass;
        try {
            newClass = Class.forName(path);
            Object obj = newClass.newInstance();
             String getMethod = "getVariable2";
             Method getNameMethod = obj.getClass().getMethod(getMethod);
             String name = (String) getNameMethod.invoke(obj); 
             System.out.println(name);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // convert string classname to class
      }
    }

输出:Ram null

那是因为您正在新创建的对象(例如obj )上调用该方法,该对象没有为variable1variable2设置variable2 ,例如:

Object obj = newClass.newInstance();

上面的代码将创建一个新的BeanObject ,其中variable1variable2值为空。 如果要打印在beanobject方法中设置的值,则需要使用beanobject调用getter方法。 即改变

String name = (String) getNameMethod.invoke(obj); 

String name = (String) getNameMethod.invoke(beanobject);

您创建未设置任何值的目标类的新实例。

Object obj = newClass.newInstance();         
Method getNameMethod = obj.getClass().getMethod(getMethod);

更改此行,它应该可以工作:

Method getNameMethod = beanobject.getClass().getMethod(getMethod);

另外:您对变量的命名不是很好。 我将代码重构为此以便更好地阅读:

public static void main(String[] args) {
    Bean beanInstance = new Bean();
    beanInstance.setVariable1("Ram");
    beanInstance.setVariable2("Rakesh");

    System.out.println("Value 1 of fresh bean instance: " + beanInstance.getVariable1());

    String beanType = Bean.class.getName();
    Class<?> beanClazz;

    try {
        beanClazz = Class.forName(beanType);

        String getterMethodName = "getVariable2";
        Method getterMethod = beanClazz.getMethod(getterMethodName);
        Object returnValue = getterMethod.invoke(beanInstance);

        System.out.println("Value 2 of by reflection loaded bean instance: " + String.valueOf(returnValue));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // convert string classname to class
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM