简体   繁体   中英

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

I know how to access private fields via Class.forName() and Field[]. Now I am trying samething via BeanInfo Interface.

What I did is below.

  1. get Class instance via Class.forName()

  2. BeanInfo info = Introspector.getBeanInfo(Class) - Here, I can see 'org.owls.anno.vo.Target'.

  3. get elements via for syntax.

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

  4. I expected list of Field names(msg, open_msg), but it prints '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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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