henrib commented on code in PR #425:
URL: https://github.com/apache/commons-jexl/pull/425#discussion_r4551621768
##########
src/main/java/org/apache/commons/jexl3/internal/Interpreter.java:
##########
@@ -1721,7 +1721,7 @@ private Object visit(final ASTMethodNode node, final
Object antish, final Object
object = data;
if (object == null) {
// no object, we fail
- return node.isSafeLhs(isSafe())
+ return isSafe() || ((ASTIdentifierAccess)
methodNode).isSafe()
? null
: unsolvableMethod(methodNode, "<null>.<?>(...)");
Review Comment:
The cast could not throw, it is inside the `if (methodNode instanceof
ASTIdentifierAccess)` branch. Replaced by `isSafeAccess(methodNode)` in
4e2f8b35 anyway, which is equivalent there and reads better.
##########
src/main/java/org/apache/commons/jexl3/internal/Interpreter.java:
##########
@@ -1721,7 +1721,7 @@ private Object visit(final ASTMethodNode node, final
Object antish, final Object
object = data;
if (object == null) {
// no object, we fail
- return node.isSafeLhs(isSafe())
+ return isSafe() || ((ASTIdentifierAccess)
methodNode).isSafe()
? null
: unsolvableMethod(methodNode, "<null>.<?>(...)");
Review Comment:
The cast could not throw, it is inside the `if (methodNode instanceof
ASTIdentifierAccess)` branch. Replaced by `isSafeAccess(methodNode)` in
4e2f8b35 anyway, which is equivalent there and reads better.
##########
src/site/xdoc/reference/syntax.xml:
##########
@@ -291,6 +291,10 @@
In the previous expression, if 'foo' is null, the
whole expression will evaluate as null.
Note that this can also be used in a chain as in
<code>x?[y]?[z]</code>.
</p>
+ <p>Whether safe-access operators shortcut non-existent
references - an undefined variable,
+ property or method - as they do null is determined by
the <code>safeUndefined</code> option,
Review Comment:
Fixed in 4e2f8b35: the paragraph now reads "an undefined variable, property,
method or function".
##########
src/site/xdoc/reference/syntax.xml:
##########
@@ -291,6 +291,10 @@
In the previous expression, if 'foo' is null, the
whole expression will evaluate as null.
Note that this can also be used in a chain as in
<code>x?[y]?[z]</code>.
</p>
+ <p>Whether safe-access operators shortcut non-existent
references - an undefined variable,
+ property or method - as they do null is determined by
the <code>safeUndefined</code> option,
+ true by default; when false, only null is shortcut and
a non-existent reference is an error.
Review Comment:
Fixed in 4e2f8b35: the paragraph now reads "an undefined variable, property,
method or function".
##########
src/test/java/org/apache/commons/jexl3/Issues400Test.java:
##########
@@ -1393,5 +1393,76 @@ void test475() {
assertNull(jexl.createScript("#pragma jexl.options
'-strict'\nfoo").execute(null));
assertThrows(JexlException.Variable.class, () ->
jexl.createScript("foo").execute(null));
}
-}
+ @Test
+ void test476() {
+ final Object err = new Object();
+ // an expression and its result when (safeUndefined, safe) is (true,
false), (true, true), (false, false), (false, true)
+ final Object[][] cases = {
+ // an undefined variable
+ {"u", err, err, err, err},
+ {"u.y", err, null, err, err},
+ {"u?.y", null, null, err, err},
+ {"u[0]", err, null, err, err},
+ {"u?[0]", null, null, err, err},
+ {"u.m()", err, null, err, err},
+ {"u?.m()", null, null, err, err},
+ {"u == null", err, err, err, err},
+ {"u ?? 1", 1, 1, 1, 1},
+ // an undefined property
+ {"'abc'.nothing", err, err, err, err},
+ {"'abc'?.nothing", null, null, err, err},
+ {"'abc'['nothing']", err, err, err, err},
+ {"'abc'?['nothing']", null, null, err, err},
+ {"'abc'.nothing.y", err, err, err, err},
+ {"'abc'.nothing?.y", null, null, err, err},
+ {"'abc'.nothing ?? 1", 1, 1, 1, 1},
+ // an undefined method
+ {"'abc'.nothing()", err, null, err, err},
+ {"'abc'?.nothing()", null, null, err, err},
+ {"'abc'.nothing().y", err, null, err, err},
+ {"'abc'.nothing()?.y", null, null, err, err},
+ {"'abc'.nothing() ?? 1", 1, 1, 1, 1},
+ // an undefined function
+ {"nothing(1)", err, err, err, err},
+ {"nothing(1).y", err, null, err, err},
+ {"nothing(1)?.y", null, null, err, err},
+ {"nothing(1) ?? 1", 1, 1, 1, 1},
+ // null is not undefined
+ {"nul", null, null, null, null},
+ {"nul.y", err, null, err, null},
+ {"nul?.y", null, null, null, null},
+ {"nul?.y.z", null, null, null, null},
+ {"nul[0]", err, null, err, null},
+ {"nul?[0]", null, null, null, null},
+ {"nul.m()", err, null, err, null},
+ {"nul?.m()", null, null, null, null},
+ {"nul(1)", err, err, err, err},
+ {"nul + 1", err, err, err, err},
+ {"nul.y ?? 1", 1, 1, 1, 1},
+ };
+ final JexlContext ctxt = new MapContext();
+ ctxt.set("nul", null);
+ for (int m = 0; m < 4; ++m) {
+ final boolean undef = (m & 2) == 0;
+ final boolean safe = (m & 1) != 0;
+ final JexlEngine jexl = new
JexlBuilder().strict(true).safe(safe).safeUndefined(undef).create();
+ for (final Object[] c : cases) {
+ final String src = (String) c[0];
+ final Object expected = c[m + 1];
+ final String msg = src + ", safeUndefined: " + undef + ",
safe: " + safe;
Review Comment:
Renamed to `safeUndefined` in 4e2f8b35.
--
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]