Class<?> targetType = target.getClass();
try {
beanInfo = Introspector.getBeanInfo(targetType);
} catch (Throwable t) {
String format = "Failed to get BeanInfo for type %s";
throw new ReflectionError(String.format(format, targetType.getName()), t);
}
PropertyDescriptor found = null;
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
if (propertyName.equals(descriptor.getName())) {
found = descriptor;
break;
}
}
if (found != null) {
Class<?> actualType = found.getPropertyType();
if (!propertyType.isAssignableFrom(actualType)) {
String format = "Expecting type of property '%s' in %s to be <%s> but was <%s>";
String msg =
String.format(format, propertyName, targetType.getName(), propertyType.getName(), actualType.getName());
throw new ReflectionError(msg);
}
return found;
}
String msg = String.format("Failed to find property '%s' in %s", propertyName, targetType.getName());
throw new ReflectionError(msg);
}