Skip to content

Commit 107fafb

Browse files
committed
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
1 parent f8bf577 commit 107fafb

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,15 @@ protected Method findGetterForProperty(String propertyName, Class<?> clazz, bool
320320
// Try "get*" method...
321321
String getterName = "get" + propertyWriteMethodSuffix;
322322
for (Method method : ms) {
323-
if (method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
323+
if (!method.isBridge() && method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
324324
(!mustBeStatic || Modifier.isStatic(method.getModifiers()))) {
325325
return method;
326326
}
327327
}
328328
// Try "is*" method...
329329
getterName = "is" + propertyWriteMethodSuffix;
330330
for (Method method : ms) {
331-
if (method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
331+
if (!method.isBridge() && method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
332332
boolean.class.equals(method.getReturnType()) &&
333333
(!mustBeStatic || Modifier.isStatic(method.getModifiers()))) {
334334
return method;
@@ -344,7 +344,7 @@ protected Method findSetterForProperty(String propertyName, Class<?> clazz, bool
344344
Method[] methods = clazz.getMethods();
345345
String setterName = "set" + StringUtils.capitalize(propertyName);
346346
for (Method method : methods) {
347-
if (method.getName().equals(setterName) && method.getParameterTypes().length == 1 &&
347+
if (!method.isBridge() && method.getName().equals(setterName) && method.getParameterTypes().length == 1 &&
348348
(!mustBeStatic || Modifier.isStatic(method.getModifiers()))) {
349349
return method;
350350
}

‎spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
import org.springframework.expression.spel.support.StandardTypeLocator;
5151
import org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver;
5252

53+
import static org.hamcrest.Matchers.equalTo;
54+
import static org.hamcrest.Matchers.instanceOf;
55+
import static org.hamcrest.Matchers.is;
56+
import static org.hamcrest.Matchers.isA;
5357
import static org.junit.Assert.*;
5458

5559
/**
@@ -1633,6 +1637,25 @@ public void SPR_9486_floatPowerDoubleTest() {
16331637
}
16341638
}
16351639

1640+
@Test
1641+
public void testBridgeMethods_SPR_9994() throws Exception {
1642+
ReflectivePropertyAccessor accessor = new ReflectivePropertyAccessor();
1643+
StandardEvaluationContext context = new StandardEvaluationContext();
1644+
Object target = new GenericImplementation();
1645+
TypedValue value = accessor.read(context, target , "property");
1646+
assertEquals(Integer.class, value.getTypeDescriptor().getType());
1647+
}
1648+
1649+
private static interface GenericInterface<T extends Number> {
1650+
public T getProperty();
1651+
}
1652+
1653+
private static class GenericImplementation implements GenericInterface<Integer> {
1654+
public Integer getProperty() {
1655+
return null;
1656+
}
1657+
}
1658+
16361659
}
16371660

16381661

0 commit comments

Comments
 (0)