Do not consider bridge methods in SpEL properties

Modify ReflectivePropertyAccessor so that it no longer considers
bridge methods when finding getters or setters. This should help
to prevent subtle errors that can occur when particular JDK
implementations happen to return bridge methods before non-bridge
methods when calling Class.getMethods()

Issue: SPR-9994
master
Phillip Webb 12 years ago
parent f8bf577eff
commit 107fafbcc5
  1. 6
      spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java
  2. 23
      spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java

@ -320,7 +320,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
// Try "get*" method... // Try "get*" method...
String getterName = "get" + propertyWriteMethodSuffix; String getterName = "get" + propertyWriteMethodSuffix;
for (Method method : ms) { for (Method method : ms) {
if (method.getName().equals(getterName) && method.getParameterTypes().length == 0 && if (!method.isBridge() && method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
(!mustBeStatic || Modifier.isStatic(method.getModifiers()))) { (!mustBeStatic || Modifier.isStatic(method.getModifiers()))) {
return method; return method;
} }
@ -328,7 +328,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
// Try "is*" method... // Try "is*" method...
getterName = "is" + propertyWriteMethodSuffix; getterName = "is" + propertyWriteMethodSuffix;
for (Method method : ms) { for (Method method : ms) {
if (method.getName().equals(getterName) && method.getParameterTypes().length == 0 && if (!method.isBridge() && method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
boolean.class.equals(method.getReturnType()) && boolean.class.equals(method.getReturnType()) &&
(!mustBeStatic || Modifier.isStatic(method.getModifiers()))) { (!mustBeStatic || Modifier.isStatic(method.getModifiers()))) {
return method; return method;
@ -344,7 +344,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
Method[] methods = clazz.getMethods(); Method[] methods = clazz.getMethods();
String setterName = "set" + StringUtils.capitalize(propertyName); String setterName = "set" + StringUtils.capitalize(propertyName);
for (Method method : methods) { for (Method method : methods) {
if (method.getName().equals(setterName) && method.getParameterTypes().length == 1 && if (!method.isBridge() && method.getName().equals(setterName) && method.getParameterTypes().length == 1 &&
(!mustBeStatic || Modifier.isStatic(method.getModifiers()))) { (!mustBeStatic || Modifier.isStatic(method.getModifiers()))) {
return method; return method;
} }

@ -50,6 +50,10 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.StandardTypeLocator; import org.springframework.expression.spel.support.StandardTypeLocator;
import org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver; import org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isA;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
@ -1633,6 +1637,25 @@ public class SpelReproTests extends ExpressionTestCase {
} }
} }
@Test
public void testBridgeMethods_SPR_9994() throws Exception {
ReflectivePropertyAccessor accessor = new ReflectivePropertyAccessor();
StandardEvaluationContext context = new StandardEvaluationContext();
Object target = new GenericImplementation();
TypedValue value = accessor.read(context, target , "property");
assertEquals(Integer.class, value.getTypeDescriptor().getType());
}
private static interface GenericInterface<T extends Number> {
public T getProperty();
}
private static class GenericImplementation implements GenericInterface<Integer> {
public Integer getProperty() {
return null;
}
}
} }

Loading…
Cancel
Save