Support SpEL compilation of #root or #this with non-public type
Prior to this commit, if a Spring Expression Language (SpEL) expression
referenced the root context object via the #root or #this variable, we
inserted a checkcast in the generated byte code that cast the object to
its concrete type. However if the root context object's type was
non-public, that resulted in an IllegalAccessError when the compiled
byte code was executed.
VariableReference.getValueInternal() already contains a solution for
global variables which inserts a checkcast to Object in the generated
byte code instead of to the object's concrete non-public type.
This commit therefore applies the same logic to #root (or #this when
used to reference the root context object) that is already applied to
global variables.
Closes gh-32356
@ -124,7 +124,7 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@@ -124,7 +124,7 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@ -143,6 +143,84 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@@ -143,6 +143,84 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@ -466,10 +544,13 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@@ -466,10 +544,13 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@ -479,22 +560,37 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@@ -479,22 +560,37 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@ -516,12 +612,13 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@@ -516,12 +612,13 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@ -644,7 +741,9 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@@ -644,7 +741,9 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
.withMessageEndingWith("Type cannot be found 'Missing'");
assertCantCompile(expression);
}
@ -915,16 +1014,20 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@@ -915,16 +1014,20 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
// Code gen is different for -1 .. 6 because there are bytecode instructions specifically for those values
// Not an int literal but an opminus with one operand:
// expression = parser.parseExpression("-1");
// assertCanCompile(expression);
// assertEquals(-1, expression.getValue());
// Not an int literal but an opMinus with one operand:
expression=parser.parseExpression("-1");
expression.getValue(Integer.class);
assertCanCompile(expression);
assertThat(expression.getValue()).isEqualTo(-1);
expression=parser.parseExpression("0");
assertCanCompile(expression);
assertThat(expression.getValue()).isEqualTo(0);
expression=parser.parseExpression("2");
assertCanCompile(expression);
assertThat(expression.getValue()).isEqualTo(2);
expression=parser.parseExpression("7");
assertCanCompile(expression);
assertThat(expression.getValue()).isEqualTo(7);
@ -1374,23 +1477,6 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@@ -1374,23 +1477,6 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@ -1776,34 +1862,6 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@@ -1776,34 +1862,6 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@ -5402,21 +5460,23 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@@ -5402,21 +5460,23 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {