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

IncompatibleClassChangeError encountered on execution of compiled SPEL expressions #24357

Closed
Smahlatz opened this issue Jan 14, 2020 · 7 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches status: feedback-provided Feedback has been provided type: regression A bug that is also a regression
Milestone

Comments

@Smahlatz
Copy link

Smahlatz commented Jan 14, 2020

Affects: 5.1.12, 5.2.2


Since this change in 5.1.8.RELEASE (#22242) we are witnessing failures in evaluation of compiled SpEL expressions.

The following test exposes the issue:

    public void compiledExpression_typeDeclaresInterface() {
        final SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, null);
        SpelExpressionParser parser = new SpelExpressionParser(config);
        final Expression expression = parser.parseExpression("name");
        Person barney = new Person("Barney");
        assertThat(expression.getValue(barney), is("Barney"));
        assertThat(expression.getValue(barney), is("Barney"));
        //This time the compiled expression will be used
        assertThat(expression.getValue(barney), is("Barney"));
    }

    public static class Person implements Named {

        private final String name;

        public Person(final String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    public interface Named {
        String getName();
    }

Running this results in the following exception:

org.springframework.expression.spel.SpelEvaluationException: EL1072E: An exception occurred whilst evaluating a compiled expression
	at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:194)
	at org.springframework.expression.spel.support.CompiledSpelExpressionTest.compiledExpression_typeDeclaresInterface(CompiledSpelExpressionTest.java:24)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IncompatibleClassChangeError: Found interface org.springframework.expression.spel.support.CompiledSpelExpressionTest$Named, but class was expected
	at spel.Ex2.getValue(Unknown Source)
	at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:184)

The issue looks to be with the compilation - org.springframework.expression.spel.support.ReflectivePropertyAccessor.generateCode contains the following:

            if (this.member instanceof Method) {

                mv.visitMethodInsn((isStatic ? INVOKESTATIC :  INVOKEVIRTUAL), classDesc, this.member.getName(),
                        CodeFlow.createSignatureDescriptor((Method) this.member). false);
            }

As we now are potentially referencing an interface, the following change fixes the issue:

            if (this.member instanceof Method) {

                boolean isInterface = this.member.getDeclaringClass().isInterface();
                mv.visitMethodInsn((isStatic ? INVOKESTATIC : isInterface ? INVOKEINTERFACE : INVOKEVIRTUAL), classDesc, this.member.getName(),
                        CodeFlow.createSignatureDescriptor((Method) this.member), isInterface);
            }

This has been tested against Oracle JDK 8u231 and spring-expression 5.1.12.RELEASE and 5.2.2.RELEASE. The issue is not encountered in spring-expression 5.1.7 and below.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 14, 2020
@Smahlatz Smahlatz changed the title IncompatibleClassChangeError encountered on evaluation of compiled SPEL expressions IncompatibleClassChangeError encountered on execution of compiled SPEL expressions Jan 14, 2020
@sbrannen
Copy link
Member

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

@sbrannen
Copy link
Member

Thanks for raising the issue.

@Smahlatz, would you be interested in submitting a PR with your patch and your test?

@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) status: waiting-for-feedback We need additional information before we can continue type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 15, 2020
@sbrannen sbrannen added this to the 5.2.4 milestone Jan 15, 2020
@sbrannen sbrannen added the for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x label Jan 15, 2020
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x labels Jan 15, 2020
@sbrannen sbrannen self-assigned this Jan 15, 2020
@Smahlatz
Copy link
Author

@sbrannen I would certainly be interested in submitting a PR, however my current terms of employment make this problematic.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jan 15, 2020
@sbrannen
Copy link
Member

sbrannen commented Jan 16, 2020

@sbrannen I would certainly be interested in submitting a PR, however my current terms of employment make this problematic.

Understood.

May we use your test case?

Never mind: I've implemented a simplified version.

@sbrannen
Copy link
Member

sbrannen commented Jan 16, 2020

I have confirmed that the following test case fails on master.

Interestingly, the test only fails if the interface is public.

import java.util.stream.IntStream;

import org.junit.jupiter.api.Test;

import org.springframework.core.Ordered;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelCompiler;
import org.springframework.expression.spel.standard.SpelExpressionParser;

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

/**
 * Tests for the {@link SpelCompiler}.
 *
 * @since 5.1.14
 */
class SpelCompilerTests {

	@Test // gh-24357
	void expressionCompilesWhenMethodComesFromPublicInterface() {
		SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, null);
		SpelExpressionParser parser = new SpelExpressionParser(config);

		OrderedComponent component = new OrderedComponent();
		Expression expression = parser.parseExpression("order");
		
		// Evaluate the expression multiple times to ensure that it gets compiled.
		IntStream.rangeClosed(1, 5).forEach(i -> assertThat(expression.getValue(component)).isEqualTo(42));
	}


	static class OrderedComponent implements Ordered {

		@Override
		public int getOrder() {
			return 42;
		}
	}

}

Whereas, the following JUnit 4 based test case passes against the v5.1.7.RELEASE tag and fails against the v5.1.8.RELEASE tag.

import java.util.stream.IntStream;

import org.junit.Test;

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

import static org.junit.Assert.assertEquals;

public class SpelCompilerTests {

	@Test // gh-24357
	public void expressionCompilesWhenMethodComesFromPublicInterface() {
		SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, null);
		SpelExpressionParser parser = new SpelExpressionParser(config);

		OrderedComponent component = new OrderedComponent();
		Expression expression = parser.parseExpression("order");
		
		// Evaluate the expression multiple times to ensure that it gets compiled.
		IntStream.rangeClosed(1, 5).forEach(i -> assertEquals(42, expression.getValue(component)));
	}


	static class OrderedComponent implements Ordered {

		@Override
		public int getOrder() {
			return 42;
		}
	}

}

@Smahlatz
Copy link
Author

@sbrannen Regarding the public interface - that's right - if not marked public, expression.isCompilable returns false and no compilation happens.

sbrannen added a commit that referenced this issue Feb 12, 2020
Spring Framework 5.1.8 introduced a regression for the compilation of
SpEL expressions referencing a method declared in an interface. An
attempt to compile such an expression resulted in a
SpelEvaluationException caused by an IncompatibleClassChangeError.

This commit fixes this regression by adding explicit support in
ReflectivePropertyAccessor.OptimalPropertyAccessor.generateCode() for
methods declared in interfaces.

Closes gh-24357
@sbrannen
Copy link
Member

This has been fixed, to be included in 5.2.4 and backported to 5.1.14.

Feel free to try out the fix in upcoming snapshot builds for those versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches status: feedback-provided Feedback has been provided type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

3 participants