简体   繁体   English

如何<I>在Java中</i>通过BeanInfo访问私有字段

[英]How to access private field via BeanInfo<I> in java

I know how to access private fields via Class.forName() and Field[]. 我知道如何通过Class.forName()和Field []访问私有字段。 Now I am trying samething via BeanInfo Interface. 现在,我正在通过BeanInfo接口尝试相同的操作。

What I did is below. 我所做的如下。

  1. get Class instance via Class.forName() 通过Class.forName()获取Class实例

  2. BeanInfo info = Introspector.getBeanInfo(Class) - Here, I can see 'org.owls.anno.vo.Target'. BeanInfo info = Introspector.getBeanInfo(Class)-在这里,我可以看到“ org.owls.anno.vo.Target”。

  3. get elements via for syntax. 通过语法获取元素。

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

  4. I expected list of Field names(msg, open_msg), but it prints 'class.java.lang.Class'. 我期望列出字段名称(msg,open_msg),但它会打印“ class.java.lang.Class”。

The Target Class is here 目标类别在这里

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 + "]";
    }
};

Thanks for Answer :D 感谢您的回答:D

您的课程不是bean:没有访问器(getter和/或setter)... getClass()除外!

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

相关问题 如何在Java 9中使用新的BeanInfo批注 - How to use new BeanInfo Annotations in Java 9 如何使用Java访问另一个类的私有字段中的私有字段 - How to access a private field in a private field in another class using Java 如何通过Java中的反射访问超级class的超级class的私有字段? - How to access a private field of the super class of the super class with reflection in Java? 如何在Java中访问非静态私有字段? - How to access a non-static private field in java? 通过反射获取私有字段,在 Java - Get a private field via reflection, in Java Java:通过反射访问私有字段(行为) - Java: Accessing private field via reflection (behaviour) 使用Java反射,我可以访问类型为private静态嵌套类的private字段吗? - Using Java reflection, can I access a private field of type private static nested class? 我该如何模拟通过Powermock和Mockito通过私有构造函数初始化的私有static final字段? - How do I mock a private static final field initialized via a private constructor using Powermock and Mockito? 通过 Java 中的反射访问私有继承字段 - Access to private inherited fields via reflection in Java 如何通过反射访问私有方法和私有数据成员? - How do I access private methods and private data members via reflection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM