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.
RateCode
Class.getFields() only gives you public fields. Perhaps you wanted the JavaBean getters?
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.
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);
}
getFields
only returns public fields. If you want all fields, see getDeclaredFields