Search in sources :

Example 86 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project iaf by ibissource.

the class ValidateAttributeRule method handleAttribute.

@Override
protected void handleAttribute(String name, String value, Map<String, String> attributes) throws Exception {
    PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(getBean(), name);
    Method m = null;
    if (pd != null) {
        m = PropertyUtils.getWriteMethod(pd);
    }
    if (m == null) {
        // validate if the attribute exists
        addLocalWarning("does not have an attribute [" + name + "] to set to value [" + value + "]");
    } else if (AnnotationUtils.findAnnotation(m, ProtectedAttribute.class) != null) {
        addLocalWarning("attribute [" + name + "] is protected, cannot be set from configuration");
    } else {
        // check if the setter has been deprecated
        checkDeprecationAndConfigurationWarning(name, m);
        if (value.contains(StringResolver.DELIM_START) && value.contains(StringResolver.DELIM_STOP)) {
            // If value contains a property, resolve it
            value = resolveValue(value);
        } else {
            // Only check for default values for non-property values
            checkTypeCompatibility(pd, name, value, attributes);
        }
        Object valueToSet = parseValueToSet(m, value);
        log.trace("attempting to populate field [{}] with value [{}] on object [{}]", () -> name, () -> valueToSet, () -> getBean());
        if (valueToSet != null) {
            try {
                BeanUtils.setProperty(getBean(), name, valueToSet);
            } catch (InvocationTargetException e) {
                log.warn("unable to populate field [{}] with value [{}] on object [{}]", name, valueToSet, getBean(), e);
                addLocalWarning(e.getCause().getMessage());
            }
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 87 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project iaf by ibissource.

the class MapPropertyDescriptorsTest method testPropertyDescriptorsBeingRegistered.

@Test
public void testPropertyDescriptorsBeingRegistered() throws ClassNotFoundException, IntrospectionException {
    BeanDefinitionRegistry beanDefinitionRegistry = new SimpleBeanDefinitionRegistry();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry);
    scanner.setIncludeAnnotationConfig(false);
    scanner.addIncludeFilter(new AssignableTypeFilter(IConfigurable.class));
    BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator() {

        @Override
        protected String buildDefaultBeanName(BeanDefinition definition) {
            String beanClassName = definition.getBeanClassName();
            Assert.state(beanClassName != null, "No bean class name set");
            return beanClassName;
        }
    };
    scanner.setBeanNameGenerator(beanNameGenerator);
    int numberOfBeans = scanner.scan("nl.nn.adapterframework", "nl.nn.ibistesttool");
    log.debug("Found " + numberOfBeans + " beans registered!");
    String[] names = scanner.getRegistry().getBeanDefinitionNames();
    for (String beanName : names) {
        BeanInfo beanInfo = Introspector.getBeanInfo(Class.forName(beanName));
        // get methods
        MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
        for (MethodDescriptor methodDescriptor : methodDescriptors) {
            String methodName = methodDescriptor.getName();
            if (methodName.startsWith("set")) {
                String propertyName = methodName.substring(3);
                String getterName = "get" + propertyName;
                String getterStartsWithIs = "is" + propertyName;
                propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
                boolean getterMatches = Arrays.stream(methodDescriptors).anyMatch(name -> name.getName().equals(getterName) || name.getName().equals(getterStartsWithIs));
                if (getterMatches) {
                    PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
                    PropertyDescriptor pd = Arrays.stream(pds).filter(name -> name.getWriteMethod() != null && methodName.equals(name.getWriteMethod().getName())).findAny().orElse(null);
                    assertNotNull("Make sure that the attribute [" + propertyName + "] has proper getter and setters in class [" + beanName + "].", pd);
                }
            }
        }
    }
}
Also used : SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) IConfigurable(nl.nn.adapterframework.core.IConfigurable) MethodDescriptor(java.beans.MethodDescriptor) ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) BeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator) AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) Test(org.junit.Test)

Example 88 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project iaf by ibissource.

the class ValidateAttributeRuleTest method testEnumGetterSetter.

@Test
public void testEnumGetterSetter() throws Exception {
    ClassWithEnum bean = new ClassWithEnum();
    PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(bean, "testEnum");
    Method writeMethod = pd.getWriteMethod();
    assertNotNull(writeMethod);
    assertEquals("TestEnum", writeMethod.getParameters()[0].getType().getSimpleName());
    Method readMethod = pd.getReadMethod();
    assertNotNull(readMethod);
    assertEquals("TestEnum", readMethod.getReturnType().getSimpleName());
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 89 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project jbosstools-hibernate by jbosstools.

the class GenericPropertySource method buildPropertyDescriptors.

private IPropertyDescriptor[] buildPropertyDescriptors() {
    if (real == null)
        return new IPropertyDescriptor[0];
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(real.getClass(), Object.class);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        IPropertyDescriptor[] result = new IPropertyDescriptor[propertyDescriptors.length];
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            result[i] = new BeanPropertyDescriptor(descriptor);
        }
        return result;
    } catch (IntrospectionException e) {
        return new IPropertyDescriptor[0];
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor)

Example 90 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project synergic-developing by zeemood.

the class JSONWriter method model.

private void model(Object object) {
    add("{");
    boolean addedSomething = false;
    Field[] ff = object.getClass().getDeclaredFields();
    try {
        for (int i = 0; i < ff.length; ++i) {
            Field field = ff[i];
            // 获取注解
            ApiField jsonField = field.getAnnotation(ApiField.class);
            ApiListField listField = field.getAnnotation(ApiListField.class);
            // 优先处理列表类型注解,非列表类型才处理字段注解
            if (listField != null) {
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), object.getClass());
                Method accessor = pd.getReadMethod();
                if (!accessor.isAccessible())
                    accessor.setAccessible(true);
                Object value = accessor.invoke(object, (Object[]) null);
                if (value == null)
                    continue;
                if (addedSomething)
                    add(',');
                add(listField.value(), value, true);
                addedSomething = true;
            } else if (jsonField != null) {
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), object.getClass());
                Method accessor = pd.getReadMethod();
                if (!accessor.isAccessible())
                    accessor.setAccessible(true);
                Object value = accessor.invoke(object, (Object[]) null);
                if (value == null)
                    continue;
                if (addedSomething)
                    add(',');
                add(jsonField.value(), value, true);
                addedSomething = true;
            }
        }
    } catch (IntrospectionException e1) {
        AlipayLogger.logBizError(e1);
    } catch (IllegalAccessException e2) {
        AlipayLogger.logBizError(e2);
    } catch (IllegalArgumentException e3) {
        AlipayLogger.logBizError(e3);
    } catch (InvocationTargetException e4) {
        AlipayLogger.logBizError(e4);
    }
    add("}");
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApiField(com.alipay.api.internal.mapping.ApiField) Field(java.lang.reflect.Field) ApiListField(com.alipay.api.internal.mapping.ApiListField) ApiListField(com.alipay.api.internal.mapping.ApiListField) ApiField(com.alipay.api.internal.mapping.ApiField)

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