use of java.beans.BeanInfo in project blue by kunstmusik.
the class TestObject method setObject.
public void setObject(Object obj) {
this.obj = obj;
if (obj == null) {
props = null;
} else {
try {
BeanInfo info = Introspector.getBeanInfo(obj.getClass());
props = info.getPropertyDescriptors();
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
this.fireTableDataChanged();
}
use of java.beans.BeanInfo in project jeesuite-libs by vakinge.
the class ExcelBeanHelper method doCacheClass.
/**
* @param clazz
* @param canonicalName
* @return
* @throws IntrospectionException
*/
private static synchronized void doCacheClass(Class<?> clazz, String canonicalName) throws Exception {
if (aliasPropertyDescriptorCache.containsKey(canonicalName))
return;
Map<String, PropertyDescriptor> map = new HashMap<>();
Map<String, PropertyDescriptor> aliasMap = new HashMap<>();
List<TitleMeta> titleMetas = new ArrayList<>();
BeanInfo srcBeanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] descriptors = srcBeanInfo.getPropertyDescriptors();
Map<String, TitleMeta> parentMap = new HashMap<>();
int maxRow = 1;
for (PropertyDescriptor descriptor : descriptors) {
String name = descriptor.getName();
if ("class".equals(name))
continue;
Method readMethod = descriptor.getReadMethod();
Method writeMethod = descriptor.getWriteMethod();
if (readMethod == null)
try {
readMethod = clazz.getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
descriptor.setReadMethod(readMethod);
} catch (NoSuchMethodException | SecurityException e) {
}
if (writeMethod == null)
try {
writeMethod = clazz.getMethod("set" + name.substring(0, 1).toUpperCase() + name.substring(1), descriptor.getPropertyType());
descriptor.setWriteMethod(writeMethod);
} catch (NoSuchMethodException | SecurityException e) {
}
if (readMethod != null || writeMethod != null) {
map.put(descriptor.getName(), descriptor);
//
TitleCell annotation = null;
try {
annotation = clazz.getDeclaredField(name).getAnnotation(TitleCell.class);
} catch (NoSuchFieldException e) {
annotation = clazz.getSuperclass().getDeclaredField(name).getAnnotation(TitleCell.class);
}
if (annotation != null) {
aliasMap.put(annotation.name().trim(), descriptor);
TitleMeta cell = new TitleMeta(annotation.name());
cell.setValueType(annotation.type());
if (StringUtils.isBlank(annotation.parentName())) {
cell.setColumnIndex(annotation.column());
cell.setRowIndex(annotation.row());
titleMetas.add(cell);
} else {
TitleMeta cellParent = parentMap.get(annotation.parentName());
if (cellParent == null) {
cellParent = new TitleMeta(annotation.parentName());
cellParent.setValueType(annotation.type());
cellParent.setColumnIndex(annotation.column());
parentMap.put(annotation.parentName(), cellParent);
titleMetas.add(cellParent);
}
cell.setColumnIndex(annotation.column());
cell.setRowIndex(annotation.row());
cellParent.addChildren(cell);
maxRow = annotation.row() > maxRow ? annotation.row() : maxRow;
}
}
}
}
ExcelMeta excelMeta = new ExcelMeta(clazz, titleMetas, maxRow);
titleCellBeanCache.put(canonicalName, excelMeta);
aliasPropertyDescriptorCache.put(canonicalName, aliasMap);
}
use of java.beans.BeanInfo in project jeesuite-libs by vakinge.
the class BeanCopyUtils method doCacheClass.
/**
* @param clazz
* @param canonicalName
* @return
* @throws IntrospectionException
*/
private static synchronized Map<String, PropertyDescriptor> doCacheClass(Class<?> clazz, String canonicalName) throws IntrospectionException {
if (cache.containsKey(canonicalName))
return cache.get(canonicalName);
Map<String, PropertyDescriptor> map = new ConcurrentHashMap<>();
List<String> fieldNames = new ArrayList<>();
BeanInfo srcBeanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] descriptors = srcBeanInfo.getPropertyDescriptors();
for (PropertyDescriptor descriptor : descriptors) {
fieldNames.add(descriptor.getName());
Method readMethod = descriptor.getReadMethod();
Method writeMethod = descriptor.getWriteMethod();
String name = descriptor.getName();
if (readMethod == null)
try {
readMethod = clazz.getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
descriptor.setReadMethod(readMethod);
} catch (NoSuchMethodException | SecurityException e) {
}
if (writeMethod == null)
try {
writeMethod = clazz.getMethod("set" + name.substring(0, 1).toUpperCase() + name.substring(1), descriptor.getPropertyType());
descriptor.setWriteMethod(writeMethod);
} catch (NoSuchMethodException | SecurityException e) {
}
if (readMethod != null && writeMethod != null) {
map.put(descriptor.getName(), descriptor);
}
}
cache.put(canonicalName, map);
fieldCache.put(canonicalName, fieldNames);
return map;
}
use of java.beans.BeanInfo in project com.revolsys.open by revolsys.
the class Property method descriptor.
static PropertyDescriptor descriptor(final Class<?> beanClass, final String name) {
if (beanClass != null && Property.hasValue(name)) {
try {
final BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
final PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
for (final PropertyDescriptor property : props) {
if (property.getName().equals(name)) {
return property;
}
}
} catch (final IntrospectionException e) {
Logs.error(Property.class, e);
}
}
return null;
}
use of java.beans.BeanInfo in project com.revolsys.open by revolsys.
the class PropertyDescriptorCache method getPropertyDescriptors.
protected static Map<String, PropertyDescriptor> getPropertyDescriptors(final Class<?> clazz) {
synchronized (propertyDescriptorByClassAndName) {
Map<String, PropertyDescriptor> propertyDescriptors = propertyDescriptorByClassAndName.get(clazz);
if (propertyDescriptors == null) {
propertyDescriptors = new HashMap<>();
try {
final BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
for (final PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
final String propertyName = propertyDescriptor.getName();
propertyDescriptors.put(propertyName, propertyDescriptor);
Method writeMethod = propertyDescriptor.getWriteMethod();
if (writeMethod == null) {
final String setMethodName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
try {
final Class<?> propertyType = propertyDescriptor.getPropertyType();
writeMethod = clazz.getMethod(setMethodName, propertyType);
propertyDescriptor.setWriteMethod(writeMethod);
} catch (NoSuchMethodException | SecurityException e) {
}
}
Maps.put(propertyWriteMethodByClassAndName, clazz, propertyName, writeMethod);
}
} catch (final IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
propertyDescriptorByClassAndName.put(clazz, propertyDescriptors);
}
return propertyDescriptors;
}
}
Aggregations