Skip to content

Commit

Permalink
#550 Fall back to synthetic property getter/setter if none other avai…
Browse files Browse the repository at this point in the history
…lable (#653)
  • Loading branch information
grubeninspekteur authored and garethahealy committed Jun 15, 2018
1 parent b89f370 commit 941de29
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ private static PropertyDescriptor fixGenericDescriptor(Class<?> clazz, PropertyD
String baseName = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
String setMethodName = "set" + baseName;
String getMethodName = "get" + baseName;
Method getMethod = findNonSyntheticMethod(getMethodName, clazz);
Method setMethod = findNonSyntheticMethod(setMethodName, clazz);
Method getMethod = findPreferablyNonSyntheticMethod(getMethodName, clazz);
Method setMethod = findPreferablyNonSyntheticMethod(setMethodName, clazz);
try {
return new PropertyDescriptor(propertyName, getMethod, setMethod);
} catch (IntrospectionException e) {
Expand All @@ -120,14 +120,19 @@ private static PropertyDescriptor fixGenericDescriptor(Class<?> clazz, PropertyD
return descriptor;
}

private static Method findNonSyntheticMethod(String methodName, Class<?> clazz) {
private static Method findPreferablyNonSyntheticMethod(String methodName, Class<?> clazz) {
Method[] methods = clazz.getMethods();
Method syntheticMethod = null;
for (Method method : methods) {
if (method.getName().equals(methodName) && !method.isBridge() && !method.isSynthetic()) {
return method;
if (method.getName().equals(methodName)) {
if (!method.isBridge() && !method.isSynthetic()) {
return method;
} else {
syntheticMethod = method;
}
}
}
return null;
return syntheticMethod;
}

public static DeepHierarchyElement[] getDeepFieldHierarchy(Class<?> parentClass, String field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ public void shouldDetermineGenericTypeForNestedGenericType() throws Exception {
assertEquals("java.util.Map", clazz.getCanonicalName());
}

@Test
public void shouldDetermineReadMethodForSyntheticOnlyMethod() throws Exception {
PropertyDescriptor propertyDescriptor = ReflectionUtils.findPropertyDescriptor(ListHolderWrapperImpl.class, "list", null);
assertNotNull(propertyDescriptor.getReadMethod());
}

public class Y implements HasX<ClassInheritsClassX> {
private ClassInheritsClassX x;

Expand Down Expand Up @@ -256,4 +262,31 @@ private interface TestIF2 {
Integer getB();
}

public class ListHolderWrapperImpl<T> extends AbstractListHolder<T> implements ListHolderWrapper<T> {
public ListHolderWrapperImpl(List<T> content) {
super(content);
}
}

abstract class AbstractListHolder<T> implements ListHolder<T> {
private List<T> list;

AbstractListHolder(List<T> list) {
this.list = list;
}

@Override
public List<T> getList() {
return list;
}
}

interface ListHolder<T> {
List<T> getList();
}

interface ListHolderWrapper<T> extends ListHolder<T> {

}

}

0 comments on commit 941de29

Please sign in to comment.