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());
}
}
}
}
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);
}
}
}
}
}
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());
}
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];
}
}
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("}");
}
Aggregations