Search in sources :

Example 96 with BeanInfo

use of java.beans.BeanInfo in project groovy-core by groovy.

the class TestSupport method getDescriptor.

protected PropertyDescriptor getDescriptor(Object bean, String property) throws Exception {
    BeanInfo info = Introspector.getBeanInfo(bean.getClass());
    PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getName().equals(property)) {
            return descriptor;
        }
    }
    fail("Could not find property: " + property + " on bean: " + bean);
    return null;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo)

Example 97 with BeanInfo

use of java.beans.BeanInfo in project spring-cloud-connectors by spring-cloud.

the class ServiceConnectorCreatorRegistry method getServiceProperties.

private Properties getServiceProperties(String keyLead, ServiceInfo serviceInfo) {
    Properties cloudProperties = new Properties();
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(serviceInfo.getClass());
        PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor propDescriptor : propDescriptors) {
            ServiceProperty propAnnotation = propDescriptor.getReadMethod().getAnnotation(ServiceProperty.class);
            String key = keyLead;
            if (propAnnotation != null) {
                if (!propAnnotation.category().isEmpty()) {
                    key = key + "." + propAnnotation.category();
                }
                if (!propAnnotation.name().isEmpty()) {
                    key = key + "." + propAnnotation.name();
                } else {
                    key = key + "." + propDescriptor.getName().toLowerCase();
                }
                Object value = propDescriptor.getReadMethod().invoke(serviceInfo);
                if (value != null) {
                    cloudProperties.put(key, value);
                }
            }
        }
    } catch (Exception e) {
        throw new CloudException(e);
    }
    return cloudProperties;
}
Also used : ServiceProperty(org.springframework.cloud.service.ServiceInfo.ServiceProperty) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Properties(java.util.Properties)

Example 98 with BeanInfo

use of java.beans.BeanInfo in project Mycat-Server by MyCATApache.

the class ParameterMapping method getDescriptors.

/**
 * 用于导出clazz这个JavaBean的所有属性的PropertyDescriptor
 * @param clazz
 * @return
 */
private static PropertyDescriptor[] getDescriptors(Class<?> clazz) {
    // PropertyDescriptor类表示JavaBean类通过存储器导出一个属性
    PropertyDescriptor[] pds;
    List<PropertyDescriptor> list;
    PropertyDescriptor[] pds2 = descriptors.get(clazz);
    // 该clazz是否第一次加载
    if (null == pds2) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
            pds = beanInfo.getPropertyDescriptors();
            list = new ArrayList<PropertyDescriptor>();
            // 加载每一个类型不为空的property
            for (int i = 0; i < pds.length; i++) {
                if (null != pds[i].getPropertyType()) {
                    list.add(pds[i]);
                }
            }
            pds2 = new PropertyDescriptor[list.size()];
            list.toArray(pds2);
        } catch (IntrospectionException ie) {
            LOGGER.error("ParameterMappingError", ie);
            pds2 = new PropertyDescriptor[0];
        }
    }
    descriptors.put(clazz, pds2);
    return (pds2);
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 99 with BeanInfo

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

the class ConfigurationMapper method map.

public void map(final Properties properties) {
    try {
        BeanInfo info = Introspector.getBeanInfo(configurationClass, Object.class);
        PropertyDescriptor[] propertyDescriptors = info.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            if (propertyDescriptor.getWriteMethod() != null) {
                mapProperty(propertyDescriptor, properties);
            }
        }
    } catch (Exception e) {
        throw new PowerMockInternalException(e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) PowerMockInternalException(org.powermock.PowerMockInternalException) PowerMockInternalException(org.powermock.PowerMockInternalException)

Example 100 with BeanInfo

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

the class TestSupport method getDescriptor.

protected PropertyDescriptor getDescriptor(Object bean, String property) throws Exception {
    BeanInfo info = Introspector.getBeanInfo(bean.getClass());
    PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getName().equals(property)) {
            return descriptor;
        }
    }
    fail("Could not find property: " + property + " on bean: " + bean);
    return null;
}
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