为私有成员|动态访问对象名称值的java反射getFields

时间:2023-01-15 15:17:43

I want to print all of the class's properties with their name and values. I have used reflection, but getFields give me length of 0.

我想打印类的所有属性及其名称和值。我使用了反射,但是getFields给出了0的长度。

RateCode getMaxRateCode = instance.getID(Integer.parseInt((HibernateUtil
            .currentSession().createSQLQuery("select max(id) from ratecodes")
            .list().get(0).toString())));
for (Field f : getMaxRateCode.getClass().getFields()) {
            try {
                System.out.println(f.getGenericType() + " " + f.getName() + " = "
                        + f.get(getMaxRateCode));
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
}

RateCode.java

RateCode.java

    private Integer rateCodeId;
    private String code;       
    private BigDecimal childStay;       
    private DateTime bookingTo;
    private Short minPerson;      
    private Boolean isFreeNightCumulative = false;
    private boolean flat = false;
    private Timestamp modifyTime;

2 个解决方案

#1


21  

Class.getFields() only gives you public fields. Perhaps you wanted the JavaBean getters?

getfields()只提供公共字段。也许你想要JavaBean getter ?

BeanInfo info = Introspector.getBeanInfo(getMaxRateCode.getClass());
for ( PropertyDescriptor pd : info.getPropertyDescriptors() )
    System.out.println(pd.getName()+": "+pd.getReadMethod().invoke(getMaxRateCode));

If you want to access the private fields, you can use getDeclaredFields() and call field.setAccessible(true) before you use them.

如果希望访问私有字段,可以在使用它们之前使用getDeclaredFields()并调用field. setaccess (true)。

for (Field f : getMaxRateCode.getClass().getDeclaredFields()) {
    f.setAccessible(true);
    Object o;
    try {
        o = f.get(getMaxRateCode);
    } catch (Exception e) {
        o = e;
    }
    System.out.println(f.getGenericType() + " " + f.getName() + " = " + o);
}

#2


15  

getFields only returns public fields. If you want all fields, see getDeclaredFields

getFields只返回公共字段。如果您想要所有字段,请参见getDeclaredFields

#1


21  

Class.getFields() only gives you public fields. Perhaps you wanted the JavaBean getters?

getfields()只提供公共字段。也许你想要JavaBean getter ?

BeanInfo info = Introspector.getBeanInfo(getMaxRateCode.getClass());
for ( PropertyDescriptor pd : info.getPropertyDescriptors() )
    System.out.println(pd.getName()+": "+pd.getReadMethod().invoke(getMaxRateCode));

If you want to access the private fields, you can use getDeclaredFields() and call field.setAccessible(true) before you use them.

如果希望访问私有字段,可以在使用它们之前使用getDeclaredFields()并调用field. setaccess (true)。

for (Field f : getMaxRateCode.getClass().getDeclaredFields()) {
    f.setAccessible(true);
    Object o;
    try {
        o = f.get(getMaxRateCode);
    } catch (Exception e) {
        o = e;
    }
    System.out.println(f.getGenericType() + " " + f.getName() + " = " + o);
}

#2


15  

getFields only returns public fields. If you want all fields, see getDeclaredFields

getFields只返回公共字段。如果您想要所有字段,请参见getDeclaredFields