1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public static <T, P> P get(Class<T> type, String propertyName,
Class<P> propertyType, T bean)
throws IntrospectionException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
// Initialisation
Method m = null;
// Loop à travers les propriétés
for (PropertyDescriptor desc: Introspector.getBeanInfo(type)
.getPropertyDescriptors()) {
if (propertyName.equals(desc.getName())
&& propertyType.isAssignableFrom(desc.getPropertyType())) {
m = desc.getReadMethod();
break;
}
}
// Si on n'a pas trouvé de méthode, mauvais argument !
if (m == null)
throw new IllegalArgumentException();
// Invocation de la méthode trouvée
@SuppressWarnings("unchecked")
P value = (P)m.invoke(bean);
return value;
} |
Partager