Skip to content

Commit

Permalink
Bug fix for introspection bugs with generics. DozerMapper#3
Browse files Browse the repository at this point in the history
  • Loading branch information
kentongray committed Jun 14, 2012
1 parent 0fa8772 commit 22a21a6
Showing 1 changed file with 45 additions and 14 deletions.
59 changes: 45 additions & 14 deletions core/src/main/java/org/dozer/util/ReflectionUtils.java
Expand Up @@ -23,16 +23,8 @@

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;
import java.lang.reflect.*;

This comment has been minimized.

Copy link
@kentongray

kentongray Jun 14, 2012

Author Owner

you can ignore these changes as they are just the IDE cleaning up imports

import java.util.*;

/**
* Internal class that provides a various reflection utilities(specific to Dozer requirements) used throughout the code
Expand Down Expand Up @@ -76,8 +68,9 @@ public static PropertyDescriptor findPropertyDescriptor(Class<?> objectClass, St
// }

String propertyName = descriptors[i].getName();
Method readMethod = descriptors[i].getReadMethod();
if (fieldName.equals(propertyName)) {
return descriptors[i];
return fixGenericDescriptor(objectClass, descriptors[i]);
}

if (fieldName.equalsIgnoreCase(propertyName)) {
Expand All @@ -90,7 +83,45 @@ public static PropertyDescriptor findPropertyDescriptor(Class<?> objectClass, St
return result;
}

public static DeepHierarchyElement[] getDeepFieldHierarchy(Class<?> parentClass, String field,
/**
* There are some nasty bugs for introspection with generics. This method addresses those nasty bugs and tries to find proper methods if available
* http://bugs.sun.com/view_bug.do?bug_id=6788525
* http://bugs.sun.com/view_bug.do?bug_id=6528714
* @param descriptor
* @return
*/
private static PropertyDescriptor fixGenericDescriptor(Class<?> clazz, PropertyDescriptor descriptor) {
Method readMethod = descriptor.getReadMethod();
Method writeMethod = descriptor.getWriteMethod();

if(readMethod != null && (readMethod.isBridge() || readMethod.isSynthetic())) {
String propertyName = descriptor.getName();
//capitalize the first letter of the string;
String baseName = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
String setMethodName = "set" + baseName;
String getMethodName = "get" + baseName;
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if(method.getName().equals(getMethodName) && !method.isBridge() && !method.isSynthetic() ) {
try {
descriptor.setReadMethod(method);
} catch (IntrospectionException e) {
//move on
}
}
if(method.getName().equals(setMethodName) && !method.isBridge() && !method.isSynthetic() ) {
try {
descriptor.setWriteMethod(method);
} catch (IntrospectionException e) {
//move on
}
}
}
}
return descriptor;
}

public static DeepHierarchyElement[] getDeepFieldHierarchy(Class<?> parentClass, String field,
HintContainer deepIndexHintContainer) {
if (!MappingUtils.isDeepMapping(field)) {
MappingUtils.throwMappingException("Field does not contain deep field delimitor");
Expand Down Expand Up @@ -229,9 +260,9 @@ static PropertyDescriptor[] getInterfacePropertyDescriptors(Class<?> interfaceCl
List<PropertyDescriptor> superInterfacePropertyDescriptors = Arrays
.asList(getInterfacePropertyDescriptors(superInterfaceClass));
/*
* #1814758

This comment has been minimized.

Copy link
@kentongray

kentongray Jun 14, 2012

Author Owner

same here, ignore these

* #1814758
* Check for existing descriptor with the same name to prevent 2 property descriptors with the same name being added
* to the result list. This caused issues when getter and setter of an attribute on different interfaces in
* to the result list. This caused issues when getter and setter of an attribute on different interfaces in
* an inheritance hierarchy
*/
for (PropertyDescriptor superPropDescriptor : superInterfacePropertyDescriptors) {
Expand Down

0 comments on commit 22a21a6

Please sign in to comment.