henrib commented on code in PR #424:
URL: https://github.com/apache/commons-jexl/pull/424#discussion_r4541353581
##########
src/main/java/org/apache/commons/jexl3/internal/Interpreter.java:
##########
@@ -1885,24 +1952,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;
Review Comment:
Fixed in 0091d579: the access applied to the null root decides. `var x;
x?.y` is null, `var x; x.y` is null with `safe(true)`, `var x; x.y?.z` is an
error. Covered in `InterpreterCharacterizationTest`.
##########
src/main/java/org/apache/commons/jexl3/parser/JexlParser.java:
##########
@@ -1026,6 +1042,63 @@ protected void jjtreeCloseNodeScope(final JexlNode node)
{
featureController.controlNode(node);
}
+ /**
+ * Resolves a declared antish variable in a reference.
+ * <p>When the features declare antish variable names, the longest dotted
prefix satisfying the
+ * declaration test of a reference rooted on a global variable, like
<code>x.y</code> in
+ * <code>x.y.z</code>, is rewritten as a single identifier.</p>
+ *
+ * @param reference The reference node
+ * @return the reference (rewritten or not) or the identifier if the whole
reference is an antish variable
+ * @see JexlFeatures#antishTest(Predicate)
+ */
+ private JexlNode resolveAntish(final ASTReference reference) {
+ final Predicate<String> declared = getFeatures().antishTest();
+ if (declared == JexlFeatures.TEST_STR_FALSE) {
+ return reference;
+ }
+ final JexlNode first = reference.jjtGetChild(0);
+ // a local variable or a namespace can not be the root of an antish
variable
+ if (!(first instanceof ASTIdentifier)
+ || ((ASTIdentifier) first).getSymbol() >= 0
+ || ((ASTIdentifier) first).getNamespace() != null) {
+ return reference;
+ }
+ final ASTIdentifier root = (ASTIdentifier) first;
+ final int count = reference.jjtGetNumChildren();
+ final StringBuilder name = new StringBuilder(root.getName());
+ String antish = null;
+ int last = 0;
+ for (int c = 1; c < count; ++c) {
+ final JexlNode child = reference.jjtGetChild(c);
+ // only plain identifier access, no safe navigation nor expression
+ if (child.getClass() != ASTIdentifierAccess.class) {
+ break;
Review Comment:
Fixed in 0091d579: a method name that completes a declared name is rewritten
as a functor call of that variable, as in `f.g(41)`. Regression test in
`AntishFeaturesTest`.
##########
src/main/java/org/apache/commons/jexl3/internal/Engine.java:
##########
@@ -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);
Review Comment:
Acknowledged, not addressed here. It only occurs when per-call features
differ from the engine's; it will be tracked in a separate minor JIRA ticket.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]