diff --git a/pom.xml b/pom.xml
index ce74ae484..534af2f24 100644
--- a/pom.xml
+++ b/pom.xml
@@ -131,6 +131,18 @@
3.21.0test
+
+ org.openjdk.jmh
+ jmh-core
+ ${commons.jmh.version}
+ test
+
+
+ org.openjdk.jmh
+ jmh-generator-annprocess
+ ${commons.jmh.version}
+ test
+ clean verify apache-rat:check spotbugs:check japicmp:cmp checkstyle:check javadoc:javadoc
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5e8b71c17..33beeb12c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -31,6 +31,7 @@
A jexl.options pragma modifies the engine options when evaluated with a plain context.
+ Declare antish variable names at parse time through JexlFeatures.antishTest(Predicate), disabling runtime antish resolution; add JexlFeatures.namePatterns(Collection).Bump org.apache.commons:commons-parent from 105 to 107.
diff --git a/src/main/java/org/apache/commons/jexl3/JexlFeatures.java b/src/main/java/org/apache/commons/jexl3/JexlFeatures.java
index 55b5f401f..7d915c710 100644
--- a/src/main/java/org/apache/commons/jexl3/JexlFeatures.java
+++ b/src/main/java/org/apache/commons/jexl3/JexlFeatures.java
@@ -332,6 +332,9 @@ public static String stringify(final int feature) {
/** The allowed pragma keys. */
private Predicate pragmas = TEST_STR_TRUE;
+ /** The declared antish variable names. */
+ private Predicate antishNames = TEST_STR_FALSE;
+
/**
* Creates default instance, equivalent to the result of calling the preferred alternative
* {@link JexlFeatures#createDefault()}
@@ -348,6 +351,7 @@ public JexlFeatures() {
public JexlFeatures(final JexlFeatures features) {
this(features.flags, features.reservedNames, features.nameSpaces);
this.pragmas = features.pragmas;
+ this.antishNames = features.antishNames;
}
/**
@@ -365,6 +369,45 @@ protected JexlFeatures(final long f, final Set r, final Predicate antishTest() {
+ return antishNames;
+ }
+
+ /**
+ * Sets a test to declare antish variable names, resolved at parse time.
+ *
+ * An antish variable is a global variable whose name contains dots, like x.y.
+ * When set, the parser rewrites a reference rooted on a global variable, like x.y.z,
+ * so that its longest dotted prefix satisfying the predicate, like x.y, is a single variable;
+ * the remaining segments, like .z, are property accesses on that variable value.
+ * A local variable root, a safe navigation segment (?.) or an expression segment
+ * (`...`) stops the rewrite.
+ *
+ *
+ * When no such prefix exists and the root is not itself a declared name, a method name may complete
+ * a declared variable, like f.g in f.g(42); that variable is then called as a functor.
+ *
+ *
+ * Declaring antish names means they are all known beforehand: a script parsed with such features
+ * does not solve antish variables at runtime, regardless of the {@link JexlOptions#setAntish(boolean)} option.
+ *
+ *
{@link #namePatterns(Collection)} creates such a test from name patterns.
+ *
+ * @param names The antish variable name predicate; must be stateless or thread-safe; null declares none
+ * @return this features instance
+ * @since 3.7.2
+ */
+ public JexlFeatures antishTest(final Predicate names) {
+ antishNames = names == null ? TEST_STR_FALSE : names;
+ return this;
+ }
+
/**
* Sets whether annotation constructs are enabled.
*
@@ -484,6 +527,9 @@ public boolean equals(final Object obj) {
if (!this.pragmas.equals(other.pragmas)) {
return false;
}
+ if (!this.antishNames.equals(other.antishNames)) {
+ return false;
+ }
if (!Objects.equals(this.reservedNames, other.reservedNames)) {
return false;
}
@@ -540,6 +586,7 @@ public int hashCode() { //CSOFF: MagicNumber
hash = 53 * hash + Long.hashCode(this.flags);
hash = 53 * hash + (this.reservedNames != null ? this.reservedNames.hashCode() : 0);
hash = 53 * hash + this.pragmas.hashCode();
+ hash = 53 * hash + this.antishNames.hashCode();
return hash;
}
@@ -830,19 +877,34 @@ public JexlFeatures pragmaTest(final Predicate keys) {
/**
* Creates a pragma key test from a collection of patterns, meant for {@link #pragmaTest(Predicate)}.
- *
- * A pattern ending with * matches any key starting with what precedes it, any other
- * pattern matches the key exactly. For instance, dlm.* matches dlm.foo and
- * dlm.foo.bar; * matches any key. Patterns are trimmed, null or empty
- * patterns are ignored.
- *
- *
Tests created from the same patterns are equal, so are the features using them.
+ *
This is equivalent to {@link #namePatterns(Collection)}.
*
* @param patterns The allowed pragma key patterns; null or empty allows no pragma key
* @return The pragma key test
* @since 3.7.1
*/
public static Predicate pragmaPatterns(final Collection patterns) {
+ return namePatterns(patterns);
+ }
+
+ /**
+ * Creates a dotted name test from a collection of patterns.
+ *
+ * Meant for features that test names, like {@link #pragmaTest(Predicate)} or {@link #antishTest(Predicate)}.
+ *
+ *
+ * A pattern ending with * matches any name starting with what precedes it, any other
+ * pattern matches the name exactly. For instance, dlm.* matches dlm.foo and
+ * dlm.foo.bar; * matches any name. Patterns are trimmed, null or empty
+ * patterns are ignored.
+ *
+ *
Tests created from the same patterns are equal, so are the features using them.
+ *
+ * @param patterns The name patterns; null or empty matches no name
+ * @return The name test
+ * @since 3.7.2
+ */
+ public static Predicate namePatterns(final Collection patterns) {
final Set exact = new TreeSet<>();
final Set prefixes = new TreeSet<>();
if (patterns != null) {
@@ -858,19 +920,19 @@ public static Predicate pragmaPatterns(final Collection patterns
}
}
}
- return exact.isEmpty() && prefixes.isEmpty() ? TEST_STR_FALSE : new PragmaPatterns(exact, prefixes);
+ return exact.isEmpty() && prefixes.isEmpty() ? TEST_STR_FALSE : new NamePatterns(exact, prefixes);
}
/**
- * A pragma key test made of exact keys and key prefixes.
+ * A name test made of exact names and name prefixes.
*/
- private static final class PragmaPatterns implements Predicate {
- /** The exactly matched keys. */
+ private static final class NamePatterns implements Predicate {
+ /** The exactly matched names. */
private final Set exact;
- /** The matched key prefixes. */
+ /** The matched name prefixes. */
private final Set prefixes;
- PragmaPatterns(final Set exact, final Set prefixes) {
+ NamePatterns(final Set exact, final Set prefixes) {
this.exact = exact;
this.prefixes = prefixes;
}
@@ -896,10 +958,10 @@ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
- if (!(obj instanceof PragmaPatterns)) {
+ if (!(obj instanceof NamePatterns)) {
return false;
}
- final PragmaPatterns other = (PragmaPatterns) obj;
+ final NamePatterns other = (NamePatterns) obj;
return exact.equals(other.exact) && prefixes.equals(other.prefixes);
}
@@ -1033,6 +1095,12 @@ public JexlFeatures sideEffect(final boolean flag) {
* When disabled, parsing a script/expression using syntactical constructs modifying variables
* including all potentially ant-ish variables will throw a parsing exception.
*
+ *
+ * When antish variable names are declared ({@link #antishTest(Predicate)}), they are all solved at parse time;
+ * only assigning a global variable, like x = 1 or x.y = 1 when x.y
+ * is declared, is then a global side effect. Assigning a property of a global variable value, like
+ * x.y.z = 1, is not.
+ *
*
* @param flag true to enable, false to disable
* @return this features instance
diff --git a/src/main/java/org/apache/commons/jexl3/internal/Debugger.java b/src/main/java/org/apache/commons/jexl3/internal/Debugger.java
index f8940a2e4..b2717eb1d 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/Debugger.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/Debugger.java
@@ -58,10 +58,14 @@ private static boolean isStatement(final JexlNode child) {
if (child instanceof ASTCaseStatement) {
return child.jjtGetNumChildren() > 0 && isStatement(child.jjtGetChild(0));
}
+ if (child instanceof ASTForeachStatement) {
+ // a loop whose body is not a statement needs a terminator, as in for (...) x += 1;
+ final int num = child.jjtGetNumChildren();
+ return num == 0 || isStatement(child.jjtGetChild(num - 1));
+ }
return child instanceof ASTJexlScript
|| child instanceof ASTBlock
|| child instanceof ASTIfStatement
- || child instanceof ASTForeachStatement
|| child instanceof ASTTryStatement
|| child instanceof ASTWhileStatement
|| child instanceof ASTDoWhileStatement
@@ -933,7 +937,7 @@ protected Object visit(final ASTForeachStatement node, final Object data) {
// the loop step
final JexlNode step = (form & 4) != 0 ? node.jjtGetChild(nc++) : null;
// last child is body
- body = (form & 8) != 0 ? node.jjtGetChild(nc) : null;
+ body = (form & 8) != 0 && nc < node.jjtGetNumChildren() ? node.jjtGetChild(nc) : null;
if (vars != null) {
accept(vars, data);
}
@@ -1110,7 +1114,8 @@ protected Object visit(final ASTJexlScript node, final Object arg) {
}
// no parameters or done with them
final int num = node.jjtGetNumChildren();
- if (num == 1 && !(node instanceof ASTJexlLambda)) {
+ if (num == 1 && (!(node instanceof ASTJexlLambda) || isLambdaExpr((ASTJexlLambda) node))) {
+ // a lambda expression is not a statement, it may be an argument or an operand
data = accept(node.jjtGetChild(0), data);
} else {
for (int i = named? 1 : 0; i < num; ++i) {
@@ -1169,12 +1174,15 @@ protected Object visit(final ASTMapLiteral node, final Object data) {
@Override
protected Object visit(final ASTMethodNode node, final Object data) {
final int num = node.jjtGetNumChildren();
- if (num == 2) {
+ if (num > 1) {
accept(node.jjtGetChild(0), data);
- if (depth <= 0) {
- builder.append("(...)");
- } else {
- accept(node.jjtGetChild(1), data);
+ // more than one arguments node when curried
+ for (int i = 1; i < num; ++i) {
+ if (depth <= 0) {
+ builder.append("(...)");
+ } else {
+ accept(node.jjtGetChild(i), data);
+ }
}
}
return data;
diff --git a/src/main/java/org/apache/commons/jexl3/internal/Engine.java b/src/main/java/org/apache/commons/jexl3/internal/Engine.java
index 06fbc8cc6..5a8aeafbd 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/Engine.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/Engine.java
@@ -408,6 +408,10 @@ public Engine(final JexlBuilder conf) {
if (!features.supportsNamespaceInstantiation()) {
options.setNamespaceInstantiation(false);
}
+ // declared antish names are all solved at parse time: no run-time solving
+ if (features.antishTest() != JexlFeatures.TEST_STR_FALSE) {
+ options.setAntish(false);
+ }
this.charset = conf.charset();
// caching:
final IntFunction> factory = conf.cacheFactory();
@@ -545,6 +549,14 @@ protected JexlOptions evalOptions(final ASTJexlScript script, final JexlContext
if (opts != options && !scriptFeatures.supportsNamespaceInstantiation()) {
opts.setNamespaceInstantiation(false);
}
+ // declared antish names are all solved at parse time: no run-time solving
+ final JexlFeatures features = script != null && script.getFeatures() != null ? script.getFeatures() : scriptFeatures;
+ if (features.antishTest() != JexlFeatures.TEST_STR_FALSE && opts.isAntish()) {
+ if (opts == options) {
+ opts = opts.copy();
+ }
+ opts.setAntish(false);
+ }
return opts;
}
diff --git a/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java b/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
index 227c337e4..9acb3908f 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
@@ -37,6 +37,7 @@
import org.apache.commons.jexl3.introspection.JexlMethod;
import org.apache.commons.jexl3.introspection.JexlPropertyGet;
import org.apache.commons.jexl3.parser.*;
+import org.apache.commons.logging.Log;
/**
* An interpreter of JEXL syntax.
@@ -100,6 +101,87 @@ public boolean isProcessed() {
}
}
+ /**
+ * The dotted name of an antish variable, like {@code x.y.z}, built from the children of a reference.
+ *
The first child of the reference is the root identifier; the following children are appended
+ * as they are traversed, as long as they are plain identifier accesses.
+ */
+ private static final class AntishName {
+
+ /**
+ * Checks whether an identifier access can be part of an antish variable name.
+ *
+ * @param access The identifier access
+ * @return true if neither safe navigation nor an expression, false otherwise
+ */
+ static boolean isPlain(final ASTIdentifierAccess access) {
+ return !access.isSafe() && !access.isExpression();
+ }
+
+ /** The name. */
+ private final StringBuilder name;
+
+ /** The index of the next reference child to append. */
+ private int next = 1;
+
+ /**
+ * Creates an antish name.
+ *
+ * @param root The root identifier name
+ */
+ AntishName(final String root) {
+ name = new StringBuilder(root);
+ }
+
+ /**
+ * Appends a dot and a name segment.
+ *
+ * @param segment The name segment
+ */
+ void append(final String segment) {
+ name.append('.').append(segment);
+ }
+
+ /**
+ * Appends the names of the reference children not yet appended, up to and including an index.
+ *
+ * @param reference The reference node
+ * @param index The index of the last child to append
+ * @return null if all children were appended, the first child that can not be part of an antish name otherwise
+ */
+ JexlNode appendUpTo(final JexlNode reference, final int index) {
+ for (; next <= index; ++next) {
+ final JexlNode child = reference.jjtGetChild(next);
+ if (!(child instanceof ASTIdentifierAccess) || !isPlain((ASTIdentifierAccess) child)) {
+ return child;
+ }
+ append(((ASTIdentifierAccess) child).getName());
+ }
+ return null;
+ }
+
+ /**
+ * @return The name length
+ */
+ int length() {
+ return name.length();
+ }
+
+ /**
+ * Truncates the name.
+ *
+ * @param length The new name length
+ */
+ void setLength(final int length) {
+ name.setLength(length);
+ }
+
+ @Override
+ public String toString() {
+ return name.toString();
+ }
+ }
+
/**
* The thread local interpreter.
*/
@@ -330,28 +412,25 @@ private Object evalCatch(final ASTReference catchVar, final JexlNode catchBody,
final int symbol = catchVariable.getSymbol();
final boolean lexical = catchVariable.isLexical() || options.isLexical();
if (lexical) {
- // create lexical frame
- final LexicalFrame locals = new LexicalFrame(frame, block);
+ pushBlock();
+ }
+ try {
// it may be a local previously declared
- final boolean trySymbol = symbol >= 0 && catchVariable instanceof ASTVar;
- if (trySymbol && !defineVariable((ASTVar) catchVariable, locals)) {
+ final boolean trySymbol = lexical && symbol >= 0 && catchVariable instanceof ASTVar;
+ if (trySymbol && !defineVariable((ASTVar) catchVariable, block)) {
return redefinedVariable(catchVar.jjtGetParent(), catchVariable.getName());
}
- block = locals;
- }
- if (symbol < 0) {
- setContextVariable(catchVar.jjtGetParent(), catchVariable.getName(), caught);
- } else {
- final Throwable cause = caught.getCause();
- frame.set(symbol, cause == null? caught : cause);
- }
- try {
+ if (symbol < 0) {
+ setContextVariable(catchVar.jjtGetParent(), catchVariable.getName(), caught);
+ } else {
+ final Throwable cause = caught.getCause();
+ frame.set(symbol, cause == null? caught : cause);
+ }
// evaluate body
return catchBody.jjtAccept(this, data);
} finally {
- // restore lexical frame
if (lexical) {
- block = block.pop();
+ popBlock();
}
}
}
@@ -503,8 +582,7 @@ protected Object executeAssign(final JexlNode node, final JexlOperator assignop,
}
// 1: follow children till penultimate, resolve dot/array
JexlNode objectNode = null;
- StringBuilder ant = null;
- int v = 1;
+ AntishName ant = null;
// start at 1 if symbol
main: for (int c = symbol >= 0 ? 1 : 0; c < last; ++c) {
objectNode = left.jjtGetChild(c);
@@ -524,21 +602,12 @@ protected Object executeAssign(final JexlNode node, final JexlOperator assignop,
antish = false;
break main;
}
- ant = new StringBuilder(firstId.getName());
+ ant = new AntishName(firstId.getName());
}
- // catch up to current child
- for (; v <= c; ++v) {
- final JexlNode child = left.jjtGetChild(v);
- final ASTIdentifierAccess aid = child instanceof ASTIdentifierAccess
- ? (ASTIdentifierAccess) child
- : null;
- // remain antish only if unsafe navigation
- if (aid == null || aid.isSafe() || aid.isExpression()) {
- antish = false;
- break main;
- }
- ant.append('.');
- ant.append(aid.getName());
+ // catch up to current child, remain antish only if unsafe navigation
+ if (ant.appendUpTo(left, c) != null) {
+ antish = false;
+ break main;
}
// solve antish
object = context.get(ant.toString());
@@ -554,8 +623,7 @@ protected Object executeAssign(final JexlNode node, final JexlOperator assignop,
final Object property;
if (propertyId != null) {
// deal with creating/assigning antish variable
- if (antish && ant != null && object == null && !propertyId.isSafe() && !propertyId.isExpression()) {
- ant.append('.');
+ if (antish && ant != null && object == null && AntishName.isPlain(propertyId)) {
ant.append(propertyId.getName());
final String name = ant.toString();
if (assignop == null) {
@@ -609,18 +677,14 @@ private Object forIterator(final ASTForeachStatement node, final Object data) {
final ASTIdentifier loopVariable = (ASTIdentifier) loopReference.jjtGetChild(0);
final int symbol = loopVariable.getSymbol();
final boolean lexical = loopVariable.isLexical() || options.isLexical();
- final LexicalFrame locals = lexical? new LexicalFrame(frame, block) : null;
+ final LexicalFrame locals = lexical ? pushBlock() : null;
final boolean loopSymbol = symbol >= 0 && loopVariable instanceof ASTVar;
- if (lexical) {
- // create lexical frame
+ Object forEach = null;
+ try {
// it may be a local previously declared
- if (loopSymbol && !defineVariable((ASTVar) loopVariable, locals)) {
+ if (lexical && loopSymbol && !defineVariable((ASTVar) loopVariable, locals)) {
return redefinedVariable(node, loopVariable.getName());
}
- block = locals;
- }
- Object forEach = null;
- try {
/* second objectNode is the variable to iterate */
final Object iterableValue = node.jjtGetChild(1).jjtAccept(this, data);
// make sure there is a value to iterate upon
@@ -671,9 +735,8 @@ private Object forIterator(final ASTForeachStatement node, final Object data) {
} finally {
// closeable iterator handling
closeIfSupported(forEach);
- // restore lexical frame
if (lexical) {
- block = block.pop();
+ popBlock();
}
}
return result;
@@ -681,50 +744,36 @@ private Object forIterator(final ASTForeachStatement node, final Object data) {
private Object forLoop(final ASTForeachStatement node, final Object data) {
Object result = null;
- int nc;
+ int nc = 0;
final int form = node.getLoopForm();
- final LexicalFrame locals;
/* first child node might be the loop variable */
- if ((form & 1) != 0) {
- nc = 1;
- final JexlNode init = node.jjtGetChild(0);
- ASTVar loopVariable = null;
- if (init instanceof ASTAssignment) {
- final JexlNode child = init.jjtGetChild(0);
- if (child instanceof ASTVar) {
- loopVariable = (ASTVar) child;
- }
- } else if (init instanceof ASTVar){
- loopVariable = (ASTVar) init;
- }
- if (loopVariable != null) {
- final boolean lexical = loopVariable.isLexical() || options.isLexical();
- locals = lexical ? new LexicalFrame(frame, block) : null;
- if (locals != null) {
- block = locals;
- }
- } else {
- locals = null;
- }
- // initialize after eventual creation of local lexical frame
- init.jjtAccept(this, data);
- // other inits
- for (JexlNode moreAssignment = node.jjtGetChild(nc);
- moreAssignment instanceof ASTAssignment;
- moreAssignment = node.jjtGetChild(++nc)) {
- moreAssignment.jjtAccept(this, data);
- }
- } else {
- locals = null;
- nc = 0;
+ final JexlNode init = (form & 1) != 0 ? node.jjtGetChild(nc++) : null;
+ // multiple declarations share the same kind, as in let i = 0, j = 1
+ final JexlNode declaration = init instanceof ASTDefineVars ? init.jjtGetChild(0) : init;
+ ASTVar loopVariable = null;
+ if (declaration instanceof ASTAssignment) {
+ final JexlNode child = declaration.jjtGetChild(0);
+ if (child instanceof ASTVar) {
+ loopVariable = (ASTVar) child;
+ }
+ } else if (declaration instanceof ASTVar){
+ loopVariable = (ASTVar) declaration;
+ }
+ final boolean lexical = loopVariable != null && (loopVariable.isLexical() || options.isLexical());
+ if (lexical) {
+ pushBlock();
}
try {
+ if (init != null) {
+ // initialize after eventual creation of local lexical frame
+ init.jjtAccept(this, data);
+ }
// the loop condition
final JexlNode predicate = (form & 2) != 0? node.jjtGetChild(nc++) : null;
// the loop step
final JexlNode step = (form & 4) != 0? node.jjtGetChild(nc++) : null;
- // last child is body
- final JexlNode statement = (form & 8) != 0 ? node.jjtGetChild(nc) : null;
+ // last child is body, if any
+ final JexlNode statement = (form & 8) != 0 && nc < node.jjtGetNumChildren() ? node.jjtGetChild(nc) : null;
// while(predicate())...
while (predicate == null || testPredicate(predicate, predicate.jjtAccept(this, data))) {
cancelCheck(node);
@@ -745,9 +794,8 @@ private Object forLoop(final ASTForeachStatement node, final Object data) {
}
}
} finally {
- // restore lexical frame
- if (locals != null) {
- block = block.pop();
+ if (lexical) {
+ popBlock();
}
}
return result;
@@ -919,6 +967,23 @@ protected Object processAnnotation(final String annotation, final Object[] args,
: stmt.call();
}
+ /**
+ * Pops the current lexical frame, restoring the enclosing one.
+ */
+ private void popBlock() {
+ block = block.pop();
+ }
+
+ /**
+ * Pushes a new lexical frame, enclosed by the current one, as the current block.
+ *
+ * @return the new current block
+ */
+ private LexicalFrame pushBlock() {
+ block = new LexicalFrame(frame, block);
+ return block;
+ }
+
/**
* Swaps the current thread local interpreter.
*
@@ -967,14 +1032,14 @@ protected Object runClosure(final Closure closure) {
if (numChildren == 0) {
return null;
}
- block = new LexicalFrame(frame, block).defineArgs();
+ pushBlock().defineArgs();
try {
final JexlNode body = script instanceof ASTJexlLambda
? script.jjtGetChild(numChildren - 1)
: script;
return interpret(body);
} finally {
- block = block.pop();
+ popBlock();
}
}
@@ -1067,7 +1132,7 @@ protected Object visit(final ASTArrayAccess node, final Object data) {
final JexlNode nindex = node.jjtGetChild(i);
if (object == null) {
// safe navigation access
- return node.isSafeChild(i)
+ return isSafe() || node.isSafeChild(i)
? null
:unsolvableProperty(nindex, stringifyProperty(nindex), false, null);
}
@@ -1154,11 +1219,11 @@ protected Object visit(final ASTBlock node, final Object data) {
if (cnt <= 0) {
return visitBlock(node, data);
}
+ pushBlock();
try {
- block = new LexicalFrame(frame, block);
return visitBlock(node, data);
} finally {
- block = block.pop();
+ popBlock();
}
}
@@ -1562,11 +1627,11 @@ protected Object visit(final ASTJexlScript script, final Object data) {
}
return closure;
}
- block = new LexicalFrame(frame, block).defineArgs();
+ pushBlock().defineArgs();
try {
return runScript(script, data);
} finally {
- block = block.pop();
+ popBlock();
}
}
@@ -1833,21 +1898,23 @@ protected Object visit(final ASTReference node, final Object data) {
Object object = null;
JexlNode objectNode = null;
JexlNode ptyNode = null;
- StringBuilder ant = null;
+ AntishName ant = null;
boolean antish = !(parent instanceof ASTReference) && options.isAntish();
- int v = 1;
+ // the index of the access to apply to the object
+ int deref = 0;
main:
for (int c = 0; c < numChildren; c++) {
objectNode = node.jjtGetChild(c);
+ deref = c;
if (objectNode instanceof ASTMethodNode) {
antish = false;
- if (object == null) {
+ // a leading method node evaluates its own functor, as in (f)(x).y
+ if (object == null && c > 0) {
// we may be performing a method call on an antish var
if (ant != null) {
final JexlNode child = objectNode.jjtGetChild(0);
if (child instanceof ASTIdentifierAccess) {
final int alen = ant.length();
- ant.append('.');
ant.append(((ASTIdentifierAccess) child).getName());
object = context.get(ant.toString());
if (object != null) {
@@ -1855,10 +1922,10 @@ protected Object visit(final ASTReference node, final Object data) {
continue;
}
// remove method name from antish
- ant.delete(alen, ant.length());
- ptyNode = objectNode;
+ ant.setLength(alen);
}
}
+ ptyNode = objectNode;
break;
}
} else if (objectNode instanceof ASTArrayAccess) {
@@ -1870,6 +1937,7 @@ protected Object visit(final ASTReference node, final Object data) {
}
// attempt to evaluate the property within the object (visit(ASTIdentifierAccess node))
object = objectNode.jjtAccept(this, object);
+ deref = c + 1;
cancelCheck(node);
if (object != null) {
// disallow mixing antish variable & bean with same root; avoid ambiguity
@@ -1885,24 +1953,23 @@ protected Object visit(final ASTReference node, final Object data) {
break main;
}
final ASTIdentifier afirst = (ASTIdentifier) first;
- ant = new StringBuilder(afirst.getName());
+ ant = new AntishName(afirst.getName());
+ if (afirst.getSymbol() >= 0) {
+ // top level is a symbol (a null local variable), cannot be an antish var; same as in executeAssign
+ ptyNode = numChildren > 1 ? node.jjtGetChild(1) : objectNode;
+ break main;
+ }
continue;
// skip the first node case since it was trialed in jjtAccept above and returned null
}
// catch up to current node
- for (; v <= c; ++v) {
- final JexlNode child = node.jjtGetChild(v);
- if (!(child instanceof ASTIdentifierAccess)) {
+ final JexlNode stop = ant.appendUpTo(node, c);
+ if (stop != null) {
+ if (!(stop instanceof ASTIdentifierAccess)) {
// not an identifier, not antish
ptyNode = objectNode;
- break main;
}
- final ASTIdentifierAccess achild = (ASTIdentifierAccess) child;
- if (achild.isSafe() || achild.isExpression()) {
- break main;
- }
- ant.append('.');
- ant.append(achild.getName());
+ break main;
}
// solve antish
object = context.get(ant.toString());
@@ -1915,12 +1982,13 @@ protected Object visit(final ASTReference node, final Object data) {
// dealing with null
if (object == null) {
if (ptyNode != null) {
- if (ptyNode.isSafeLhs(isSafe())) {
+ // the access applied to null decides, as in x?.y, x?.y() or x?[y]
+ if (isSafe() || deref < numChildren && isSafeAccess(node.jjtGetChild(deref))) {
return null;
}
if (ant != null) {
final String aname = ant.toString();
- final boolean defined = isVariableDefined(frame, block, aname);
+ final boolean defined = isVariableDefined(frame, aname);
return unsolvableVariable(node, aname, !defined);
}
return unsolvableProperty(node,
@@ -1931,7 +1999,7 @@ protected Object visit(final ASTReference node, final Object data) {
return null;
}
final String aname = Objects.toString(ant, "?");
- final boolean defined = isVariableDefined(frame, block, aname);
+ final boolean defined = isVariableDefined(frame, aname);
// defined but null; arg of a strict operator?
if (defined && !isStrictOperand(node)) {
return null;
@@ -1942,6 +2010,20 @@ protected Object visit(final ASTReference node, final Object data) {
return object;
}
+ /**
+ * Checks whether an access in a reference is explicitly safe, as in ?.y, ?.y() or ?[y].
+ *
+ * @param access the access, a method node being checked through the member it calls
+ * @return true if the access is safe, false otherwise
+ */
+ private static boolean isSafeAccess(final JexlNode access) {
+ final JexlNode member = access instanceof ASTMethodNode ? access.jjtGetChild(0) : access;
+ if (member instanceof ASTIdentifierAccess) {
+ return ((ASTIdentifierAccess) member).isSafe();
+ }
+ return member instanceof ASTArrayAccess && ((ASTArrayAccess) member).isSafeChild(0);
+ }
+
@Override
protected Object visit(final ASTReferenceExpression node, final Object data) {
return node.jjtGetChild(0).jjtAccept(this, data);
@@ -2191,13 +2273,10 @@ protected Object visit(final ASTTryResources node, final Object data) {
final int bodyChild = node.jjtGetNumChildren() - 1;
final JexlNode tryBody = node.jjtGetChild(bodyChild);
final Queue