Search in sources :

Example 36 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project powermock by powermock.

the class ReflectUtils method getPropertiesHelper.

private static PropertyDescriptor[] getPropertiesHelper(Class type, boolean read, boolean write) {
    try {
        BeanInfo info = Introspector.getBeanInfo(type, Object.class);
        PropertyDescriptor[] all = info.getPropertyDescriptors();
        if (read && write) {
            return all;
        }
        List properties = new ArrayList(all.length);
        for (int i = 0; i < all.length; i++) {
            PropertyDescriptor pd = all[i];
            if ((read && pd.getReadMethod() != null) || (write && pd.getWriteMethod() != null)) {
                properties.add(pd);
            }
        }
        return (PropertyDescriptor[]) properties.toArray(new PropertyDescriptor[properties.size()]);
    } catch (IntrospectionException e) {
        throw new CodeGenerationException(e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 37 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project powermock by powermock.

the class ReflectUtils method getPropertyMethods.

public static Method[] getPropertyMethods(PropertyDescriptor[] properties, boolean read, boolean write) {
    Set methods = new HashSet();
    for (int i = 0; i < properties.length; i++) {
        PropertyDescriptor pd = properties[i];
        if (read) {
            methods.add(pd.getReadMethod());
        }
        if (write) {
            methods.add(pd.getWriteMethod());
        }
    }
    methods.remove(null);
    return (Method[]) methods.toArray(new Method[methods.size()]);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method) HashSet(java.util.HashSet)

Example 38 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project powermock by powermock.

the class BeanMapEmitter method generateGetPropertyType.

private void generateGetPropertyType(final Map allProps, String[] allNames) {
    final CodeEmitter e = begin_method(Constants.ACC_PUBLIC, GET_PROPERTY_TYPE, null);
    e.load_arg(0);
    EmitUtils.string_switch(e, allNames, Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {

        public void processCase(Object key, Label end) {
            PropertyDescriptor pd = (PropertyDescriptor) allProps.get(key);
            EmitUtils.load_class(e, Type.getType(pd.getPropertyType()));
            e.return_value();
        }

        public void processDefault() {
            e.aconst_null();
            e.return_value();
        }
    });
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) PropertyDescriptor(java.beans.PropertyDescriptor) Label(org.powermock.api.mockito.repackaged.asm.Label) ObjectSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)

Example 39 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project Gargoyle by callakrsos.

the class ObjectUtil method toMap.

/**
	 * @작성자 : KYJ
	 * @작성일 : 2017. 3. 31. 
	 * @param obj
	 * @param filter
	 * @return
	 * @throws IntrospectionException 
	 */
@SuppressWarnings("rawtypes")
public static Map toMap(Object t, Predicate<String> fieldNameFilter) {
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(t.getClass());
    } catch (IntrospectionException e1) {
        throw new RuntimeException(e1);
    }
    Map<String, Object> hashMap = new HashMap<String, Object>();
    // Iterate over all the attributes
    for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
        // write메소드와 read메소드가 존재할때만.
        Method writeMethod = descriptor.getWriteMethod();
        Method readMethod = descriptor.getReadMethod();
        if (ValueUtil.isEmpty(writeMethod) || ValueUtil.isEmpty(readMethod)) {
            continue;
        }
        // Class<?> returnType = readMethod.getReturnType();
        String methodName = ValueUtil.getSimpleMethodName(readMethod.getName());
        if (fieldNameFilter != null) {
            if (fieldNameFilter.test(methodName))
                continue;
        }
        Object originalValue = null;
        try {
            originalValue = readMethod.invoke(t);
        } catch (Exception e) {
        }
        if (ValueUtil.isNotEmpty(originalValue)) {
            hashMap.put(methodName, originalValue);
        } else {
            hashMap.put(methodName, null);
        }
    }
    return hashMap;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) IntrospectionException(java.beans.IntrospectionException)

Example 40 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project JMRI by JMRI.

the class DefaultJavaBeanConfigXML method unpack.

Object unpack(Element e) throws Exception {
    String classname = e.getAttributeValue("beanClass");
    Class<?> cl = Class.forName(classname);
    Constructor<?> ctor = cl.getConstructor(new Class<?>[] {});
    Object o = ctor.newInstance(new Object[] {});
    // reflect through and add parameters
    BeanInfo b = Introspector.getBeanInfo(o.getClass());
    PropertyDescriptor[] properties = b.getPropertyDescriptors();
    // add properties
    List<Element> children = e.getChildren("property");
    for (int i = 0; i < children.size(); i++) {
        // unpack XML
        Element property = children.get(i);
        Element eName = property.getChild("name");
        Element eValue = property.getChild("value");
        String name = eName.getText();
        String value = eValue.getText();
        String type = eName.getAttributeValue("type");
        // find matching method
        for (int j = 0; j < properties.length; j++) {
            if (properties[j].getName().equals(name)) {
                // match, set this one by first finding method
                Method m = properties[j].getWriteMethod();
                // sort by type
                if (type.equals("class java.lang.String")) {
                    m.invoke(o, new Object[] { value });
                } else if (type.equals("int")) {
                    m.invoke(o, new Object[] { Integer.valueOf(value) });
                } else {
                    log.error("Can't handle type: " + type);
                }
                break;
            }
        }
    }
    return o;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Element(org.jdom2.Element) Method(java.lang.reflect.Method)

Aggregations

PropertyDescriptor (java.beans.PropertyDescriptor)841 Method (java.lang.reflect.Method)400 BeanInfo (java.beans.BeanInfo)342 IntrospectionException (java.beans.IntrospectionException)191 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)109 ArrayList (java.util.ArrayList)90 HashMap (java.util.HashMap)66 Field (java.lang.reflect.Field)60 Map (java.util.Map)52 Test (org.junit.Test)39 IOException (java.io.IOException)35 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)34 List (java.util.List)33 Type (java.lang.reflect.Type)21 HashSet (java.util.HashSet)21 Set (java.util.Set)20 LinkedHashMap (java.util.LinkedHashMap)19