Skip to content

Commit

Permalink
Fix SpEL generated code for default method invocation
Browse files Browse the repository at this point in the history
Closes gh-25706
  • Loading branch information
aclement committed Sep 5, 2020
1 parent 4044f4c commit a404bf5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
Expand Up @@ -347,7 +347,8 @@ public void generateCode(MethodVisitor mv, CodeFlow cf) {
}

generateCodeForArguments(mv, cf, method, this.children);
mv.visitMethodInsn((isStaticMethod ? INVOKESTATIC : INVOKEVIRTUAL), classDesc, method.getName(),
mv.visitMethodInsn((isStaticMethod ? INVOKESTATIC : (method.isDefault() ? INVOKEINTERFACE : INVOKEVIRTUAL)),
classDesc, method.getName(),
CodeFlow.createSignatureDescriptor(method), method.getDeclaringClass().isInterface());
cf.pushDescriptor(this.exitTypeDescriptor);

Expand Down
Expand Up @@ -5159,7 +5159,7 @@ private void assertGetValueFail(Expression expression) {
assertThatExceptionOfType(Exception.class).isThrownBy(expression::getValue);
}

private void assertIsCompiled(Expression expression) {
public static void assertIsCompiled(Expression expression) {
try {
Field field = SpelExpression.class.getDeclaredField("compiledAst");
field.setAccessible(true);
Expand Down
Expand Up @@ -22,15 +22,18 @@

import org.springframework.core.Ordered;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.SpelCompilationCoverageTests;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for the {@link SpelCompiler}.
*
* @author Sam Brannen
* @author Andy Clement
* @since 5.1.14
*/
class SpelCompilerTests {
Expand All @@ -55,5 +58,60 @@ public int getOrder() {
return 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);
}

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();
}

}

0 comments on commit a404bf5

Please sign in to comment.