Search in sources :

Example 41 with BeanInfo

use of java.beans.BeanInfo in project charts by vaadin.

the class ChartDesignAttributeHandler method resolveSupportedAttributes.

/**
 * Resolves the supported attributes and corresponding getters and setters
 * for the class using introspection. After resolving, the information is
 * cached internally by this class
 *
 * @param clazz
 *            the class to resolve the supported attributes for
 */
private static void resolveSupportedAttributes(Class<?> clazz) {
    if (clazz == null) {
        throw new IllegalArgumentException("The clazz can not be null");
    }
    if (CACHE.containsKey(clazz)) {
        // NO-OP
        return;
    }
    BeanInfo beanInfo;
    try {
        beanInfo = Introspector.getBeanInfo(clazz);
    } catch (IntrospectionException e) {
        throw new RuntimeException("Could not get supported attributes for class " + clazz.getName());
    }
    AttributeCacheEntry entry = new AttributeCacheEntry();
    for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
        Method getter = descriptor.getReadMethod();
        Method setter = descriptor.getWriteMethod();
        Class<?> propertyType = descriptor.getPropertyType();
        if (getter != null && setter != null && propertyType != null && getFormatter().canConvert(propertyType)) {
            String attribute = toAttributeName(descriptor.getName());
            entry.addAttribute(attribute, getter, setter);
        }
    }
    CACHE.put(clazz, entry);
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method)

Example 42 with BeanInfo

use of java.beans.BeanInfo in project JWildfire by thargor6.

the class Action method importProperties.

public void importProperties(ManagedObject pManagedObject) {
    BeanInfo beanInfo = pManagedObject.getBeanInfo();
    if (beanInfo != null) {
        PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
        if (props != null) {
            for (PropertyDescriptor prop : props) {
                Method readMethod = prop.getReadMethod();
                if (readMethod != null) {
                    try {
                        Object val = readMethod.invoke(pManagedObject);
                        Class<?> cls = prop.getPropertyType();
                        if (cls == Color.class) {
                            Color color = (Color) val;
                            String name = prop.getName();
                            Parameter param;
                            param = new Parameter();
                            parameterList.add(param);
                            param.setName(name + ".r");
                            param.setValue(color != null ? String.valueOf(color.getRed()) : null);
                            param = new Parameter();
                            parameterList.add(param);
                            param.setName(name + ".g");
                            param.setValue(color != null ? String.valueOf(color.getGreen()) : null);
                            param = new Parameter();
                            parameterList.add(param);
                            param.setName(name + ".b");
                            param.setValue(color != null ? String.valueOf(color.getBlue()) : null);
                        } else {
                            Parameter param = new Parameter();
                            parameterList.add(param);
                            param.setName(prop.getName());
                            param.setValue(val != null ? val.toString() : "");
                        }
                    } catch (Throwable ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Color(java.awt.Color) ManagedObject(org.jwildfire.base.ManagedObject) Method(java.lang.reflect.Method)

Example 43 with BeanInfo

use of java.beans.BeanInfo in project JessMA by ldcsaa.

the class BeanHelper method getPropDescByName.

/**
 * 获取指定类型 Java Bean 的名称为 name 的属性描述对象
 *
 *  @param startClass	: Bean 类型
 *  @param stopClass	: 截止查找的父类类型
 *  @param name			: 属性名称
 *  @return				  描述对象映射,找不到属性则返回 null
 */
public static final PropertyDescriptor getPropDescByName(Class<?> startClass, Class<?> stopClass, String name) {
    try {
        BeanInfo info = Introspector.getBeanInfo(startClass, stopClass);
        PropertyDescriptor[] pds = info.getPropertyDescriptors();
        for (PropertyDescriptor pd : pds) {
            if (pd.getName().equals(name))
                return pd;
        }
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }
    return null;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 44 with BeanInfo

use of java.beans.BeanInfo in project JessMA by ldcsaa.

the class BeanHelper method getPropDescMap.

/**
 * 获取指定类型 Java Bean 的所有属性描述(包括 stopClass 及更高层父类除外的所有父类定义的属性)
 *
 *  @param startClass	: Bean 类型
 *  @param stopClass	: 截止查找的父类类型
 *  @return				  属性名 / 描述对象映射
 */
public static final Map<String, PropertyDescriptor> getPropDescMap(Class<?> startClass, Class<?> stopClass) {
    Map<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>();
    try {
        BeanInfo info = Introspector.getBeanInfo(startClass, stopClass);
        PropertyDescriptor[] pds = info.getPropertyDescriptors();
        for (PropertyDescriptor pd : pds) map.put(pd.getName(), pd);
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }
    return map;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 45 with BeanInfo

use of java.beans.BeanInfo in project abstools by abstools.

the class Bean method init.

private void init() {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(dataClass);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor property = propertyDescriptors[i];
            if (property.getWriteMethod() != null && property.getWriteMethod().isAnnotationPresent(Adjustable.class)) {
                String key = property.getWriteMethod().getAnnotation(Adjustable.class).key();
                if (key.equals("")) {
                    key = norm(property.getName());
                }
                order.put(key, norm(property.getName()));
                properties.put(norm(property.getName()), property);
                if (getValue(property) == null) {
                    // may not be null when called in the process of deserialization
                    // (see readObject)
                    setValue(property, NullValueProvider.getNullValue(property.getPropertyType()));
                }
            }
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Throwable t) {
        t.printStackTrace();
        throw new IllegalStateException("FATAL: data class introspection was not successful");
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo)

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