Search in sources :

Example 56 with BeanInfo

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();
}
Also used : BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 57 with BeanInfo

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);
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) BeanInfo(java.beans.BeanInfo) TitleCell(com.jeesuite.common2.excel.annotation.TitleCell) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ExcelMeta(com.jeesuite.common2.excel.model.ExcelMeta) TitleMeta(com.jeesuite.common2.excel.model.TitleMeta)

Example 58 with BeanInfo

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;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 59 with BeanInfo

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;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 60 with BeanInfo

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;
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method)

Aggregations

BeanInfo (java.beans.BeanInfo)420 PropertyDescriptor (java.beans.PropertyDescriptor)328 Method (java.lang.reflect.Method)217 SimpleBeanInfo (java.beans.SimpleBeanInfo)167 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)161 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)148 IntrospectionException (java.beans.IntrospectionException)115 InvocationTargetException (java.lang.reflect.InvocationTargetException)38 HashMap (java.util.HashMap)33 Test (org.junit.jupiter.api.Test)33 ArrayList (java.util.ArrayList)30 Field (java.lang.reflect.Field)17 Map (java.util.Map)17 Test (org.junit.Test)13 EventSetDescriptor (java.beans.EventSetDescriptor)12 MethodDescriptor (java.beans.MethodDescriptor)12 List (java.util.List)11 BeanDescriptor (java.beans.BeanDescriptor)10 HashSet (java.util.HashSet)8 Introspector (java.beans.Introspector)7