Search in sources :

Example 56 with PropertyDescriptor

use of java.beans.PropertyDescriptor 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 57 with PropertyDescriptor

use of java.beans.PropertyDescriptor 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 58 with PropertyDescriptor

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

the class BeanHelper method setProperties.

/**
 * 设置 Java Bean 的属性
 *
 *  @param bean			: Bean 实例<br>
 *  @param properties	: 属性名 / 值映射<br>
 *  					  其中名称为 {@link String} 类型,与属性名称可能一直也可能不一致<br>
 *  					  属性值可能为以下 3 中类型:<br>
 *  					  &nbsp; &nbsp; 1) 属性的实际类型:直接对属性赋值<br>
 *  					  &nbsp; &nbsp; 2) {@link String} 类型:先执行自动类型转换再对属性赋值<br>
 *  					  &nbsp; &nbsp; 3) {@link String}[] 类型:先执行自动类型转换再对属性赋值<br>
 *  @param keyMap		: properties.key / Bean 属性名映射,当 properties 的 key 与属性名不对应时,
 *  					  用 keyMap 把它们关联起来
 */
public static final <T> void setProperties(Object bean, Map<String, T> properties, Map<String, String> keyMap) {
    if (properties == null || properties.isEmpty())
        return;
    Map<Object, Map<String, T>> subs = new HashMap<Object, Map<String, T>>();
    Map<String, PropertyDescriptor> pps = getPropDescMap(bean.getClass());
    Map<String, T> params = translateKVMap(properties, keyMap);
    parseCascadeProperties(bean, subs, pps, params, null);
    if (!subs.isEmpty()) {
        Set<Map.Entry<Object, Map<String, T>>> sset = subs.entrySet();
        for (Map.Entry<Object, Map<String, T>> e : sset) {
            try {
                PropertyDescriptor key = (PropertyDescriptor) e.getKey();
                Object o = key.getPropertyType().newInstance();
                setProperties(o, e.getValue());
                setProperty(bean, key, o);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 59 with PropertyDescriptor

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

the class BeanHelper method setPropertiesOrFieldValues.

/**
 * 设置 Java Bean 的属性
 *
 *  @param bean			: Bean 实例<br>
 *  @param valueMap		: 属性或成员变量名 / 值映射<br>
 *  					  其中名称为 {@link String} 类型,与属性或成员变量名称可能一直也可能不一致<br>
 *  					  属性或成员变量值可能为以下 3 中类型:<br>
 *  					  &nbsp; &nbsp; 1) 属性或成员变量的实际类型:直接对属性或成员变量赋值<br>
 *  					  &nbsp; &nbsp; 2) {@link String} 类型:先执行自动类型转换再对属或成员变量赋值<br>
 *  					  &nbsp; &nbsp; 3) {@link String}[] 类型:先执行自动类型转换再对属赋或成员变量值<br>
 *  @param keyMap		: properties.key / Bean 属性或成员变量名映射,当 properties 的 key 与属性或成员变量名不对应时,
 *  					  用 keyMap 把它们关联起来
 */
public static final <T> void setPropertiesOrFieldValues(Object bean, Map<String, T> valueMap, Map<String, String> keyMap) {
    if (valueMap == null || valueMap.isEmpty())
        return;
    Map<Object, Map<String, T>> subs = new HashMap<Object, Map<String, T>>();
    Map<String, PropertyDescriptor> pps = getPropDescMap(bean.getClass());
    Map<String, T> params = translateKVMap(valueMap, keyMap);
    Map<String, T> failParams = new HashMap<String, T>();
    parseCascadeProperties(bean, subs, pps, params, failParams);
    if (!failParams.isEmpty()) {
        Map<String, Field> fms = getInstanceFieldMap(bean.getClass());
        parseCascadeFields(bean, subs, fms, failParams);
    }
    if (!subs.isEmpty()) {
        Set<Map.Entry<Object, Map<String, T>>> sset = subs.entrySet();
        for (Map.Entry<Object, Map<String, T>> e : sset) {
            Object key = e.getKey();
            Map<String, T> value = e.getValue();
            try {
                if (key instanceof PropertyDescriptor) {
                    PropertyDescriptor pd = (PropertyDescriptor) key;
                    Object o = pd.getPropertyType().newInstance();
                    setPropertiesOrFieldValues(o, value);
                    setProperty(bean, pd, o);
                } else {
                    Field f = (Field) key;
                    Object o = f.getType().newInstance();
                    setPropertiesOrFieldValues(o, value);
                    setFieldValue(bean, f, o);
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) HashMap(java.util.HashMap) Map(java.util.Map)

Example 60 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project fabric8 by jboss-fuse.

the class BeanUtils method getFields.

public static List<String> getFields(Class clazz) {
    List<String> answer = new ArrayList<String>();
    try {
        for (PropertyDescriptor desc : PropertyUtils.getPropertyDescriptors(clazz)) {
            if (desc.getReadMethod() != null) {
                answer.add(desc.getName());
            }
        }
    } catch (Exception e) {
        throw new FabricException("Failed to get property descriptors for " + clazz.toString(), e);
    }
    // few tweaks to maintain compatibility with existing views for now...
    if (clazz.getSimpleName().equals("Container")) {
        answer.add("parentId");
        answer.add("versionId");
        answer.add("profileIds");
        answer.add("childrenIds");
        answer.remove("fabricService");
    } else if (clazz.getSimpleName().equals("Profile")) {
        answer.add("id");
        answer.add("parentIds");
        answer.add("childIds");
        answer.add("containerCount");
        answer.add("containers");
        answer.add("fileConfigurations");
    } else if (clazz.getSimpleName().equals("Version")) {
        answer.add("id");
        answer.add("defaultVersion");
    }
    return answer;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) FabricException(io.fabric8.api.FabricException) FabricException(io.fabric8.api.FabricException)

Aggregations

PropertyDescriptor (java.beans.PropertyDescriptor)836 Method (java.lang.reflect.Method)396 BeanInfo (java.beans.BeanInfo)339 IntrospectionException (java.beans.IntrospectionException)188 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)171 SimpleBeanInfo (java.beans.SimpleBeanInfo)142 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)141 InvocationTargetException (java.lang.reflect.InvocationTargetException)108 ArrayList (java.util.ArrayList)90 HashMap (java.util.HashMap)66 Field (java.lang.reflect.Field)59 Map (java.util.Map)52 Test (org.junit.Test)39 IOException (java.io.IOException)34 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)34 List (java.util.List)33 HashSet (java.util.HashSet)21 Type (java.lang.reflect.Type)20 Set (java.util.Set)20 LinkedHashMap (java.util.LinkedHashMap)19