Close

Java - BeanInfo Examples

Java 

    private GenericCacheDecorator (Object obj) {
this.obj = obj;
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
cachedData.put(desc.getReadMethod()
.getName(), EMPTY);
}

} catch (IntrospectionException e) {
e.printStackTrace();
}
}
Original Post




This example creates a custom BeanInfo 'ABeanBeanInfo' by extending java.beans.SimpleBeanInfo

package com.logicbig.example;

import java.beans.*;

public class IntrospectorExample2 {
public static void main (String[] args) throws IntrospectionException {
//auto discovering ABeanBeanInfo
BeanInfo beanInfo = Introspector.getBeanInfo(ABean.class);
System.out.println(beanInfo.getClass());
System.out.println(beanInfo.getBeanDescriptor()
.getDisplayName());
}

public static class ABean {
private String someStr;

public String getSomeStr () {
return someStr;
}

public void setSomeStr (String someStr) {
this.someStr = someStr;
}

}

public class ABeanBeanInfo extends SimpleBeanInfo {
@Override
public BeanDescriptor getBeanDescriptor () {
BeanDescriptor bd = new BeanDescriptor(ABean.class);
bd.setDisplayName("A Bean");
return bd;
}
}
}

Output:

 class java.beans.GenericBeanInfo
IntrospectorExample2$ABean




See Also