Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for introspection generic issues #4

Merged
merged 1 commit into from Jun 18, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.*;
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
* #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