Merge branch '5.2.x'

# Conflicts:
#	spring-expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java
This commit is contained in:
Juergen Hoeller
2020-09-05 13:02:01 +02:00
5 changed files with 76 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -348,8 +348,8 @@ public class MethodReference extends SpelNodeImpl {
generateCodeForArguments(mv, cf, method, this.children);
mv.visitMethodInsn((isStaticMethod ? INVOKESTATIC : (method.isDefault() ? INVOKEINTERFACE : INVOKEVIRTUAL)),
classDesc, method.getName(),
CodeFlow.createSignatureDescriptor(method), method.getDeclaringClass().isInterface());
classDesc, method.getName(), CodeFlow.createSignatureDescriptor(method),
method.getDeclaringClass().isInterface());
cf.pushDescriptor(this.exitTypeDescriptor);
if (this.originalPrimitiveExitTypeDescriptor != null) {

View File

@@ -38,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class SpelCompilerTests {
@Test // gh-24357
@Test // gh-24357
void expressionCompilesWhenMethodComesFromPublicInterface() {
SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, null);
SpelExpressionParser parser = new SpelExpressionParser(config);
@@ -50,6 +50,31 @@ class SpelCompilerTests {
IntStream.rangeClosed(1, 5).forEach(i -> assertThat(expression.getValue(component)).isEqualTo(42));
}
@Test // gh-25706
void defaultMethodInvocation() {
SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, null);
SpelExpressionParser parser = new SpelExpressionParser(config);
StandardEvaluationContext context = new StandardEvaluationContext();
Item item = new Item();
context.setRootObject(item);
Expression expression = parser.parseExpression("#root.isEditable2()");
assertThat(SpelCompiler.compile(expression)).isFalse();
assertThat(expression.getValue(context)).isEqualTo(false);
assertThat(SpelCompiler.compile(expression)).isTrue();
SpelCompilationCoverageTests.assertIsCompiled(expression);
assertThat(expression.getValue(context)).isEqualTo(false);
context.setVariable("user", new User());
expression = parser.parseExpression("#root.isEditable(#user)");
assertThat(SpelCompiler.compile(expression)).isFalse();
assertThat(expression.getValue(context)).isEqualTo(true);
assertThat(SpelCompiler.compile(expression)).isTrue();
SpelCompilationCoverageTests.assertIsCompiled(expression);
assertThat(expression.getValue(context)).isEqualTo(true);
}
static class OrderedComponent implements Ordered {
@@ -114,4 +139,40 @@ class SpelCompilerTests {
boolean hasSomeProperty();
}
public static class User {
boolean isAdmin() {
return true;
}
}
public static class Item implements Editable {
// some fields
private String someField = "";
// some getters and setters
@Override
public boolean hasSomeProperty() {
return someField != null;
}
}
public interface Editable {
default boolean isEditable(User user) {
return user.isAdmin() && hasSomeProperty();
}
default boolean isEditable2() {
return false;
}
boolean hasSomeProperty();
}
}