Skip to content

Commit 1de36ab

Browse files
committed
Revise contribution
See gh-25316
1 parent 97b5af8 commit 1de36ab

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
import java.lang.reflect.Method;
2020
import java.lang.reflect.Modifier;
2121
import java.lang.reflect.Proxy;
22-
import java.util.*;
22+
import java.util.ArrayList;
23+
import java.util.Collections;
24+
import java.util.HashMap;
25+
import java.util.LinkedHashSet;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Set;
2329

2430
import org.springframework.core.BridgeMethodResolver;
2531
import org.springframework.core.MethodParameter;
@@ -42,6 +48,7 @@
4248
* @author Andy Clement
4349
* @author Juergen Hoeller
4450
* @author Chris Beams
51+
* @author Sam Brannen
4552
* @since 3.0
4653
* @see StandardEvaluationContext#addMethodResolver(MethodResolver)
4754
*/
@@ -238,6 +245,12 @@ else if (Proxy.isProxyClass(type)) {
238245
}
239246
}
240247
}
248+
// Ensure methods defined in java.lang.Object are exposed for JDK proxies.
249+
for (Method method : getMethods(Object.class)) {
250+
if (isCandidateForInvocation(method, type)) {
251+
result.add(method);
252+
}
253+
}
241254
return result;
242255
}
243256
else {
@@ -260,13 +273,7 @@ else if (Proxy.isProxyClass(type)) {
260273
* @since 3.1.1
261274
*/
262275
protected Method[] getMethods(Class<?> type) {
263-
Set<Method> methods=new HashSet<>();
264-
methods.addAll(Arrays.asList(type.getMethods()));
265-
//Add all methods of Object to have methods like toString on Proxy-Objects
266-
methods.addAll(Arrays.asList(Object.class.getMethods()));
267-
268-
Method[] methods1 = methods.toArray(new Method[0]);
269-
return methods1;
276+
return type.getMethods();
270277
}
271278

272279
/**

spring-expression/src/test/java/org/springframework/expression/spel/support/ReflectionHelperTests.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.ByteArrayOutputStream;
2020
import java.io.PrintStream;
21-
import java.lang.reflect.InvocationHandler;
2221
import java.lang.reflect.Method;
2322
import java.lang.reflect.Proxy;
2423
import java.util.ArrayList;
@@ -27,12 +26,16 @@
2726
import org.junit.jupiter.api.Test;
2827

2928
import org.springframework.core.convert.TypeDescriptor;
30-
import org.springframework.expression.*;
29+
import org.springframework.expression.EvaluationContext;
30+
import org.springframework.expression.MethodExecutor;
31+
import org.springframework.expression.MethodResolver;
32+
import org.springframework.expression.ParseException;
33+
import org.springframework.expression.PropertyAccessor;
34+
import org.springframework.expression.TypedValue;
3135
import org.springframework.expression.spel.AbstractExpressionTests;
3236
import org.springframework.expression.spel.SpelUtilities;
3337
import org.springframework.expression.spel.standard.SpelExpression;
3438
import org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind;
35-
import org.springframework.util.Assert;
3639

3740
import static org.assertj.core.api.Assertions.assertThat;
3841
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -365,17 +368,18 @@ public void testOptimalReflectivePropertyAccessor() throws Exception {
365368
}
366369

367370
@Test
368-
void testReflectiveMethodResolver() throws AccessException {
369-
MethodResolver resolver=new ReflectiveMethodResolver();
371+
void reflectiveMethodResolverForJdkProxies() throws Exception {
372+
Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { Runnable.class }, (p, m, args) -> null);
373+
374+
MethodResolver resolver = new ReflectiveMethodResolver();
370375
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
371-
Object obj= Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[]{Runnable.class}, new InvocationHandler() {
372-
@Override
373-
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
374-
return null;
375-
}
376-
});
377-
MethodExecutor mexec=resolver.resolve(evaluationContext,obj,"toString",new ArrayList<>());
378-
Assert.notNull(mexec,"MethodExecutor should not be empty.");
376+
377+
MethodExecutor bogus = resolver.resolve(evaluationContext, proxy, "bogus", List.of());
378+
assertThat(bogus).as("MethodExecutor for bogus()").isNull();
379+
MethodExecutor toString = resolver.resolve(evaluationContext, proxy, "toString", List.of());
380+
assertThat(toString).as("MethodExecutor for toString()").isNotNull();
381+
MethodExecutor hashCode = resolver.resolve(evaluationContext, proxy, "hashCode", List.of());
382+
assertThat(hashCode).as("MethodExecutor for hashCode()").isNotNull();
379383
}
380384

381385
/**

0 commit comments

Comments
 (0)