Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions java/org/apache/el/lang/EvaluationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -75,6 +83,9 @@ public VariableMapper getVariableMapper() {

@Override
public Object getContext(Class<?> key) {
if (key == ELResolver.StandaloneIdentifierMarker.class) {
return standaloneIdentifierMarker;
}
return elContext.getContext(key);
}

Expand All @@ -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);
}

Expand Down
49 changes: 49 additions & 0 deletions test/org/apache/el/parser/TestAstIdentifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,6 +34,51 @@

public class TestAstIdentifier {

@Test
public void testStandaloneIdentifierMarker() {
List<Object> 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();
Expand Down