Java 如何通过BeanInfo访问专用字段<;I>;在爪哇

Java 如何通过BeanInfo访问专用字段<;I>;在爪哇,java,reflection,field,private,beaninfo,Java,Reflection,Field,Private,Beaninfo,我知道如何通过Class.forName()和Field[]访问私有字段。 现在我正在通过BeanInfo接口尝试同样的事情 下面是我所做的 通过Class.forName()获取类实例 BeanInfo info=Introspector.getBeanInfo(类)-在这里,我可以看到'org.owls.anno.vo.Target' 通过获取语法的元素 对于(PropertyDescriptor pd:info.getPropertyDescriptors()){ log.info(pd.

我知道如何通过Class.forName()和Field[]访问私有字段。 现在我正在通过BeanInfo接口尝试同样的事情

下面是我所做的

  • 通过Class.forName()获取类实例

  • BeanInfo info=Introspector.getBeanInfo(类)-在这里,我可以看到'org.owls.anno.vo.Target'

  • 通过获取语法的元素

    对于(PropertyDescriptor pd:info.getPropertyDescriptors()){ log.info(pd.getName()); log.info(pd.getDisplayName()); log.info(pd.getPropertyType()); }

  • 我需要字段名列表(msg,open_msg),但它会打印“class.java.lang.class”

  • 目标类在这里

    package org.owls.anno.vo;
    
    import org.owls.anno.SimpleAnnotation;
    
    @SimpleAnnotation("Add missing attributes")
    public class Target {
        private String msg;
        public String open_msg;
    
        public Target(String msg) {
            super();
            this.msg = msg;
        }
    
        @Override
        public String toString() {
            return "Target [msg=" + msg + "]";
        }
    };
    

    感谢您的回答:D

    您的类不是bean:没有访问器(getter和/或setter)…除了
    getClass()

    所以你的意思是,如果我的类不是bean,那么我就不能通过BeanInfo接口获取类的信息?@juneyongoh嗯,据我所知,你需要getter(和setter)。它不是基于私有实例变量,这是非常合乎逻辑的,我认为:您可以通过访问器访问它们,而不是直接访问。