diff --git a/java/org/apache/el/lang/EvaluationContext.java b/java/org/apache/el/lang/EvaluationContext.java index 2bcb68cb0a80..529a6952f8dc 100644 --- a/java/org/apache/el/lang/EvaluationContext.java +++ b/java/org/apache/el/lang/EvaluationContext.java @@ -19,6 +19,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import jakarta.el.ELContext; import jakarta.el.ELResolver; @@ -41,6 +42,13 @@ public final class EvaluationContext extends ELContext { private LambdaExpressionNestedState lambdaExpressionNestedState; + /* + * The context object for ELResolver.StandaloneIdentifierMarker, which AstIdentifier sets for every identifier it + * resolves. Held here rather than in the wrapped context, so that resolving an identifier does not update the + * wrapped context's map. The resolvers are passed this context, so they see it. + */ + private Object standaloneIdentifierMarker; + /** * Creates a new EvaluationContext wrapping the given ELContext. * @@ -75,6 +83,9 @@ public VariableMapper getVariableMapper() { @Override public Object getContext(Class key) { + if (key == ELResolver.StandaloneIdentifierMarker.class) { + return standaloneIdentifierMarker; + } return elContext.getContext(key); } @@ -90,6 +101,10 @@ public boolean isPropertyResolved() { @Override public void putContext(Class key, Object contextObject) { + if (key == ELResolver.StandaloneIdentifierMarker.class) { + standaloneIdentifierMarker = Objects.requireNonNull(contextObject); + return; + } elContext.putContext(key, contextObject); } diff --git a/test/org/apache/el/parser/TestAstIdentifier.java b/test/org/apache/el/parser/TestAstIdentifier.java index 81bcf60c796e..e3a3c5f672b4 100644 --- a/test/org/apache/el/parser/TestAstIdentifier.java +++ b/test/org/apache/el/parser/TestAstIdentifier.java @@ -16,9 +16,13 @@ */ package org.apache.el.parser; +import java.util.ArrayList; +import java.util.List; + import jakarta.el.ELContext; import jakarta.el.ELException; import jakarta.el.ELProcessor; +import jakarta.el.ELResolver; import jakarta.el.ExpressionFactory; import jakarta.el.ValueExpression; @@ -30,6 +34,51 @@ public class TestAstIdentifier { + @Test + public void testStandaloneIdentifierMarker() { + List markers = new ArrayList<>(); + ELProcessor processor = new ELProcessor(); + processor.getELManager().addELResolver(new ELResolver() { + @Override + public Object getValue(ELContext context, Object base, Object property) { + if (base == null && "marked".equals(property)) { + markers.add(context.getContext(ELResolver.StandaloneIdentifierMarker.class)); + context.setPropertyResolved(null, property); + return "value"; + } + return null; + } + + @Override + public Class getType(ELContext context, Object base, Object property) { + return null; + } + + @Override + public void setValue(ELContext context, Object base, Object property, Object value) { + } + + @Override + public boolean isReadOnly(ELContext context, Object base, Object property) { + return true; + } + + @Override + public Class getCommonPropertyType(ELContext context, Object base) { + return null; + } + }); + + Assert.assertEquals("value", processor.getValue("marked", String.class)); + Assert.assertEquals(Integer.valueOf(5), processor.getValue("marked.length()", Integer.class)); + Assert.assertEquals("value", processor.getValue("marked", String.class)); + + Assert.assertEquals(List.of(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE).toString(), + markers.stream().map(m -> Boolean.valueOf(Boolean.TRUE.equals(m))).toList().toString()); + // Held by the evaluation, not by the ELContext of the application + Assert.assertNull(processor.getELManager().getELContext().getContext(ELResolver.StandaloneIdentifierMarker.class)); + } + @Test public void testImport01() { ELProcessor processor = new ELProcessor();