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

SpEL: cannot call methods declared in java.lang.Object on a JDK proxy #25316

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per Code-Style#import-statements we should avoid wildcard imports.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct. We do not use wildcard imports for types.


import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.MethodParameter;
Expand Down Expand Up @@ -269,7 +263,13 @@ else if (Proxy.isProxyClass(type)) {
* @since 3.1.1
*/
protected Method[] getMethods(Class<?> type) {
return type.getMethods();
Set<Method> methods=new HashSet<>();
methods.addAll(Arrays.asList(type.getMethods()));
//Add all methods of Object to have methods like toString on Proxy-Objects
methods.addAll(Arrays.asList(Object.class.getMethods()));

Method[] methods1 = methods.toArray(new Method[0]);
return methods1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to make a change specific to JDK proxies in getMethods(Class<?>, Object) instead of having this change globally applied in getMethods(Class<?>).

However, there is no need to modify this PR.

I already have such a change in place locally and will apply that when merging your PR.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;

import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ParseException;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;
import org.springframework.expression.*;
import org.springframework.expression.spel.AbstractExpressionTests;
import org.springframework.expression.spel.SpelUtilities;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind;
import org.springframework.util.Assert;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand Down Expand Up @@ -364,6 +364,19 @@ public void testOptimalReflectivePropertyAccessor() throws Exception {
field.write(ctx, tester, "field", null));
}

@Test
void testReflectiveMethodResolver() throws AccessException {
MethodResolver resolver=new ReflectiveMethodResolver();
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
Object obj= Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[]{Runnable.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return null;
}
});
MethodExecutor mexec=resolver.resolve(evaluationContext,obj,"toString",new ArrayList<>());
Assert.notNull(mexec,"MethodExecutor should not be empty.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use AssertJ for test assertions, not Spring's org.springframework.util.Assert class.

In any case, I'll make that change when merging the PR.

}

/**
* Used to validate the match returned from a compareArguments call.
Expand Down