Invoke init/destroy/SpEL methods via public types whenever possible

Prior to this commit, when invoking init methods and destroy methods
for beans as well as methods within Spring Expression Language (SpEL)
expressions via reflection, we invoked them based on the "interface
method" returned from ClassUtils.getInterfaceMethodIfPossible(). That
works well for finding methods defined in an interface, but it does not
find public methods defined in a public superclass.

For example, in a SpEL expression it was previously impossible to
invoke toString() on a non-public type from a different module. This
could be seen when attempting to invoke toString() on an unmodifiable
list created by Collections.unmodifiableList(...). Doing so resulted in
an InaccessibleObjectException.

Although users can address that by adding an appropriate --add-opens
declaration, such as --add-opens java.base/java.util=ALL-UNNAMED, it is
better if applications do not have to add an --add-opens declaration
for such use cases in SpEL. The same applies to init methods and
destroy methods for beans.

This commit therefore introduces a new
getPubliclyAccessibleMethodIfPossible() method in ClassUtils which
serves as a replacement for getInterfaceMethodIfPossible().

This new method finds the first publicly accessible method in the
supplied method's type hierarchy that has a method signature equivalent
to the supplied method. If the supplied method is public and declared
in a public type, the supplied method will be returned. Otherwise, this
method recursively searches the class hierarchy and implemented
interfaces for an equivalent method that is public and declared in a
public type. If a publicly accessible equivalent method cannot be
found, the supplied method will be returned, indicating that no such
equivalent method exists.

All usage of getInterfaceMethodIfPossible() has been replaced with
getPubliclyAccessibleMethodIfPossible() in spring-beans and
spring-expression. In addition, getInterfaceMethodIfPossible() has been
marked as deprecated in favor of the new method.

As a bonus, the introduction of getPubliclyAccessibleMethodIfPossible()
allows us to delete a fair amount of obsolete code within the SpEL
infrastructure.

See gh-29857
Closes gh-33216
This commit is contained in:
Sam Brannen
2024-07-14 12:01:25 +02:00
parent cac623b3f4
commit 47f88e123f
15 changed files with 519 additions and 171 deletions

View File

@@ -21,6 +21,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
@@ -402,12 +404,32 @@ class MethodInvocationTests extends AbstractExpressionTests {
}
@Test
void methodOfClass() {
void methodOfJavaLangClass() {
Expression expression = parser.parseExpression("getName()");
Object value = expression.getValue(new StandardEvaluationContext(String.class));
assertThat(value).isEqualTo("java.lang.String");
}
@Test
void methodOfJavaLangObject() {
Expression expression = parser.parseExpression("toString()");
Object value = expression.getValue(new StandardEvaluationContext(42));
assertThat(value).isEqualTo("42");
}
@Test // gh-33216
void methodOfJavaLangObjectDeclaredInNonPublicType() throws Exception {
Expression expression = parser.parseExpression("toString()");
List<String> unmodifiableList = Collections.unmodifiableList(Arrays.asList("foo", "bar"));
Method toStringMethod = unmodifiableList.getClass().getMethod("toString");
// Prerequisite for this use case:
assertThat(toStringMethod.getDeclaringClass()).isPackagePrivate();
Object value = expression.getValue(new StandardEvaluationContext(unmodifiableList));
assertThat(value).isEqualTo(unmodifiableList.toString());
}
@Test
void invokeMethodWithoutConversion() {
final BytesService service = new BytesService();

View File

@@ -16,6 +16,7 @@
package org.springframework.expression.spel;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -145,12 +146,27 @@ class PropertyAccessTests extends AbstractExpressionTests {
}
@Test
void accessingPropertyOfClass() {
void accessingPropertyOfJavaLangClass() {
Expression expression = parser.parseExpression("name");
Object value = expression.getValue(new StandardEvaluationContext(String.class));
assertThat(value).isEqualTo("java.lang.String");
}
@Test // gh-33216
void accessingPropertyOfJavaTimeZoneIdDeclaredInNonPublicSubclass() throws Exception {
String ID = "CET";
ZoneId zoneId = ZoneId.of(ID);
// Prerequisites for this use case:
assertThat(zoneId.getClass()).isPackagePrivate();
assertThat(zoneId.getId()).isEqualTo(ID);
Expression expression = parser.parseExpression("id");
String result = expression.getValue(new StandardEvaluationContext(zoneId), String.class);
assertThat(result).isEqualTo(ID);
}
@Test
void shouldAlwaysUsePropertyAccessorFromEvaluationContext() {
SpelExpressionParser parser = new SpelExpressionParser();