Search in sources :

Example 86 with BeanInfo

use of java.beans.BeanInfo in project CodenameOne by codenameone.

the class DefaultBeanInfoResolver method getBeanInfo.

public BeanInfo getBeanInfo(Class clazz) {
    if (clazz == null) {
        return null;
    }
    String classname = clazz.getName();
    // look for .impl.basic., remove it and call getBeanInfo(class)
    int index = classname.indexOf(".impl.basic");
    if (index != -1 && classname.endsWith("Basic")) {
        classname = classname.substring(0, index) + classname.substring(index + ".impl.basic".length(), classname.lastIndexOf("Basic"));
        try {
            return getBeanInfo(Class.forName(classname));
        } catch (ClassNotFoundException e) {
            return null;
        }
    } else {
        try {
            BeanInfo beanInfo = (BeanInfo) Class.forName(classname + "BeanInfo").newInstance();
            return beanInfo;
        } catch (Exception e) {
            return null;
        }
    }
}
Also used : BeanInfo(java.beans.BeanInfo)

Example 87 with BeanInfo

use of java.beans.BeanInfo in project activemq-artemis by apache.

the class PropertyUtil method getProperties.

/**
 * Get properties from an object using reflection.  If the passed object is null an
 * empty <code>Map</code> is returned.
 *
 * @param object the Object whose properties are to be extracted.
 * @return <Code>Map</Code> of properties extracted from the given object.
 * @throws Exception if an error occurs while examining the object's properties.
 */
public static Map<String, String> getProperties(Object object) throws Exception {
    if (object == null) {
        return Collections.emptyMap();
    }
    Map<String, String> properties = new LinkedHashMap<>();
    BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
    Object[] NULL_ARG = {};
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    if (propertyDescriptors != null) {
        for (PropertyDescriptor pd : propertyDescriptors) {
            if (pd.getReadMethod() != null && !pd.getName().equals("class") && !pd.getName().equals("properties") && !pd.getName().equals("reference")) {
                Object value = pd.getReadMethod().invoke(object, NULL_ARG);
                if (value != null) {
                    if (value instanceof Boolean || value instanceof Number || value instanceof String || value instanceof URI || value instanceof URL) {
                        properties.put(pd.getName(), ("" + value));
                    } else if (value instanceof SSLContext) {
                    // ignore this one..
                    } else {
                        Map<String, String> inner = getProperties(value);
                        for (Entry<String, String> entry : inner.entrySet()) {
                            properties.put(pd.getName() + "." + entry.getKey(), entry.getValue());
                        }
                    }
                }
            }
        }
    }
    return properties;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) SSLContext(javax.net.ssl.SSLContext) URI(java.net.URI) URL(java.net.URL) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 88 with BeanInfo

use of java.beans.BeanInfo in project activemq-artemis by apache.

the class ActiveMQTestBase method validateGettersAndSetters.

/**
 * It validates a Bean (POJO) using simple setters and getters with random values.
 * You can pass a list of properties to be ignored, as some properties will have a pre-defined domain (not being possible to use random-values on them)
 */
protected void validateGettersAndSetters(final Object pojo, final String... ignoredProperties) throws Exception {
    HashSet<String> ignoreSet = new HashSet<>();
    for (String ignore : ignoredProperties) {
        ignoreSet.add(ignore);
    }
    BeanInfo info = Introspector.getBeanInfo(pojo.getClass());
    PropertyDescriptor[] properties = info.getPropertyDescriptors();
    for (PropertyDescriptor prop : properties) {
        Object value;
        if (prop.getPropertyType() == String.class) {
            value = RandomUtil.randomString();
        } else if (prop.getPropertyType() == Integer.class || prop.getPropertyType() == Integer.TYPE) {
            value = RandomUtil.randomInt();
        } else if (prop.getPropertyType() == Long.class || prop.getPropertyType() == Long.TYPE) {
            value = RandomUtil.randomLong();
        } else if (prop.getPropertyType() == Boolean.class || prop.getPropertyType() == Boolean.TYPE) {
            value = RandomUtil.randomBoolean();
        } else if (prop.getPropertyType() == Double.class || prop.getPropertyType() == Double.TYPE) {
            value = RandomUtil.randomDouble();
        } else {
            System.out.println("Can't validate property of type " + prop.getPropertyType() + " on " + prop.getName());
            value = null;
        }
        if (value != null && prop.getWriteMethod() != null && prop.getReadMethod() == null) {
            System.out.println("WriteOnly property " + prop.getName() + " on " + pojo.getClass());
        } else if (value != null && prop.getWriteMethod() != null && prop.getReadMethod() != null && !ignoreSet.contains(prop.getName())) {
            System.out.println("Validating " + prop.getName() + " type = " + prop.getPropertyType());
            prop.getWriteMethod().invoke(pojo, value);
            Assert.assertEquals("Property " + prop.getName(), value, prop.getReadMethod().invoke(pojo));
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) HashSet(java.util.HashSet)

Example 89 with BeanInfo

use of java.beans.BeanInfo in project tomcat70 by apache.

the class BeanELResolver method getFeatureDescriptors.

@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
    if (base == null) {
        return null;
    }
    try {
        BeanInfo info = Introspector.getBeanInfo(base.getClass());
        PropertyDescriptor[] pds = info.getPropertyDescriptors();
        for (int i = 0; i < pds.length; i++) {
            pds[i].setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
            pds[i].setValue(TYPE, pds[i].getPropertyType());
        }
        return Arrays.asList((FeatureDescriptor[]) pds).iterator();
    } catch (IntrospectionException e) {
    // 
    }
    return null;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 90 with BeanInfo

use of java.beans.BeanInfo in project jdepth by Crab2died.

the class IntrospectorFeature method main.

public static void main(String[] args) {
    IntrospectorFeature introspector = new IntrospectorFeature();
    try {
        // 
        PropertyDescriptor descriptor = new PropertyDescriptor("property", IntrospectorFeature.class);
        // setter 函数获取
        Method setMethod = descriptor.getWriteMethod();
        // getter 函数获取
        Method getMethod = descriptor.getReadMethod();
        // 调用 setter
        Object obj = setMethod.invoke(introspector, "setter方法");
        // 调用getter
        Object prop = getMethod.invoke(introspector);
        System.out.println(prop);
        BeanInfo beanInfo = Introspector.getBeanInfo(IntrospectorFeature.class);
        PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor _prop : props) {
            System.out.println(_prop.getDisplayName());
            if ("property".equals(_prop.getName())) {
                System.out.println(_prop.getDisplayName());
            }
        }
    } catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
        e.printStackTrace();
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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