ReflectionUtils { public static PropertyDescriptor findPropertyDescriptor(Class<?> objectClass, String fieldName, HintContainer deepIndexHintContainer) { PropertyDescriptor result = null; if (MappingUtils.isDeepMapping(fieldName)) { DeepHierarchyElement[] hierarchy = getDeepFieldHierarchy(objectClass, fieldName, deepIndexHintContainer); result = hierarchy[hierarchy.length - 1].getPropDescriptor(); } else { PropertyDescriptor[] descriptors = getPropertyDescriptors(objectClass); if (descriptors != null) { int size = descriptors.length; for (int i = 0; i < size; i++) { String propertyName = descriptors[i].getName(); Method readMethod = descriptors[i].getReadMethod(); if (fieldName.equals(propertyName)) { return fixGenericDescriptor(objectClass, descriptors[i]); } if (fieldName.equalsIgnoreCase(propertyName)) { result = descriptors[i]; } } } } return result; } private  ReflectionUtils(); static PropertyDescriptor findPropertyDescriptor(Class<?> objectClass, String fieldName,
                                                            HintContainer deepIndexHintContainer); static DeepHierarchyElement[] getDeepFieldHierarchy(Class<?> parentClass, String field,
                                                               HintContainer deepIndexHintContainer); static Method getMethod(Object obj, String methodName); static Method getMethod(Class<?> clazz, String methodName); static Method findAMethod(Class<?> clazz, String methodName, BeanContainer beanContainer); static PropertyDescriptor[] getPropertyDescriptors(Class<?> objectClass); static Field getFieldFromBean(Class<?> clazz, String fieldName); static Object invoke(Method method, Object obj, Object[] args); static Method getMethod(Class<?> clazz, String name, Class<?>[] parameterTypes); static T newInstance(Class<T> clazz); static Class<?> determineGenericsType(Class<?> parentClazz, PropertyDescriptor propDescriptor); static Class<?> determineGenericsType(Class<?> clazz, Method method, boolean isReadMethod); static Class<?> determineGenericsType(Type type); static Method getNonStandardSetter(Class<?> clazz, String fieldName);  }
@Test public void testFindPropertyDescriptor_InterfaceInheritance() { String fieldName = "parentField"; PropertyDescriptor pd = ReflectionUtils.findPropertyDescriptor(ChildChildIF.class, fieldName, null); assertNotNull("prop descriptor should not be null", pd); assertEquals("invalid prop descriptor name found", fieldName, pd.getName()); }
@Test public void shouldReturnBestMatch_ambigousIgonreCase() { PropertyDescriptor result = ReflectionUtils.findPropertyDescriptor(XMLGregorianCalendar.class, "timezone", null); assertThat(result.getName(), equalTo("timezone")); }
@Test public void shouldHandleBeanWithGenericInterface() { PropertyDescriptor propertyDescriptor = ReflectionUtils.findPropertyDescriptor(Y.class, "x", null); assertEquals("com.github.dozermapper.core.util.ReflectionUtilsTest$ClassInheritsClassX", propertyDescriptor.getReadMethod().getReturnType().getName()); }
@Test public void shouldDetermineReadMethodForSyntheticOnlyMethod() throws Exception { PropertyDescriptor propertyDescriptor = ReflectionUtils.findPropertyDescriptor(ListHolderWrapperImpl.class, "list", null); assertNotNull(propertyDescriptor.getReadMethod()); }