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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@
<version>3.21.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${commons.jmh.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${commons.jmh.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>clean verify apache-rat:check spotbugs:check japicmp:cmp checkstyle:check javadoc:javadoc</defaultGoal>
Expand Down
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<!-- FIX -->
<action dev="henrib" due-to="Claude" type="fix" issue="JEXL-475">A jexl.options pragma modifies the engine options when evaluated with a plain context.</action>
<!-- ADD -->
<action dev="henrib" due-to="Claude" type="add" issue="JEXL-474">Declare antish variable names at parse time through JexlFeatures.antishTest(Predicate), disabling runtime antish resolution; add JexlFeatures.namePatterns(Collection).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 105 to 107.</action>
</release>
Expand Down
98 changes: 83 additions & 15 deletions src/main/java/org/apache/commons/jexl3/JexlFeatures.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ public static String stringify(final int feature) {
/** The allowed pragma keys. */
private Predicate<String> pragmas = TEST_STR_TRUE;

/** The declared antish variable names. */
private Predicate<String> antishNames = TEST_STR_FALSE;

/**
* Creates default instance, equivalent to the result of calling the preferred alternative
* {@link JexlFeatures#createDefault()}
Expand All @@ -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;
}

/**
Expand All @@ -365,6 +369,45 @@ protected JexlFeatures(final long f, final Set<String> r, final Predicate<String
this.nameSpaces = n == null? TEST_STR_FALSE : n;
}

/**
* Gets the declared antish variable names test.
*
* @return The declared antish variable names test, never null.
* @since 3.7.2
*/
public Predicate<String> antishTest() {
return antishNames;
}

/**
* Sets a test to declare antish variable names, resolved at parse time.
* <p>
* An antish variable is a global variable whose name contains dots, like <code>x.y</code>.
* When set, the parser rewrites a reference rooted on a global variable, like <code>x.y.z</code>,
* so that its longest dotted prefix satisfying the predicate, like <code>x.y</code>, is a single variable;
* the remaining segments, like <code>.z</code>, are property accesses on that variable value.
* A local variable root, a safe navigation segment (<code>?.</code>) or an expression segment
* (<code>`...`</code>) stops the rewrite.
* </p>
* <p>
* When no such prefix exists and the root is not itself a declared name, a method name may complete
* a declared variable, like <code>f.g</code> in <code>f.g(42)</code>; that variable is then called as a functor.
* </p>
* <p>
* 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.
* </p>
* <p>{@link #namePatterns(Collection)} creates such a test from name patterns.</p>
*
* @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<String> names) {
antishNames = names == null ? TEST_STR_FALSE : names;
return this;
}

/**
* Sets whether annotation constructs are enabled.
* <p>
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -830,19 +877,34 @@ public JexlFeatures pragmaTest(final Predicate<String> keys) {

/**
* Creates a pragma key test from a collection of patterns, meant for {@link #pragmaTest(Predicate)}.
* <p>
* A pattern ending with <code>*</code> matches any key starting with what precedes it, any other
* pattern matches the key exactly. For instance, <code>dlm.*</code> matches <code>dlm.foo</code> and
* <code>dlm.foo.bar</code>; <code>*</code> matches any key. Patterns are trimmed, null or empty
* patterns are ignored.
* </p>
* <p>Tests created from the same patterns are equal, so are the features using them.</p>
* <p>This is equivalent to {@link #namePatterns(Collection)}.</p>
*
* @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<String> pragmaPatterns(final Collection<String> patterns) {
return namePatterns(patterns);
}

/**
* Creates a dotted name test from a collection of patterns.
* <p>
* Meant for features that test names, like {@link #pragmaTest(Predicate)} or {@link #antishTest(Predicate)}.
* </p>
* <p>
* A pattern ending with <code>*</code> matches any name starting with what precedes it, any other
* pattern matches the name exactly. For instance, <code>dlm.*</code> matches <code>dlm.foo</code> and
* <code>dlm.foo.bar</code>; <code>*</code> matches any name. Patterns are trimmed, null or empty
* patterns are ignored.
* </p>
* <p>Tests created from the same patterns are equal, so are the features using them.</p>
*
* @param patterns The name patterns; null or empty matches no name
* @return The name test
* @since 3.7.2
*/
public static Predicate<String> namePatterns(final Collection<String> patterns) {
final Set<String> exact = new TreeSet<>();
final Set<String> prefixes = new TreeSet<>();
if (patterns != null) {
Expand All @@ -858,19 +920,19 @@ public static Predicate<String> pragmaPatterns(final Collection<String> 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<String> {
/** The exactly matched keys. */
private static final class NamePatterns implements Predicate<String> {
/** The exactly matched names. */
private final Set<String> exact;
/** The matched key prefixes. */
/** The matched name prefixes. */
private final Set<String> prefixes;

PragmaPatterns(final Set<String> exact, final Set<String> prefixes) {
NamePatterns(final Set<String> exact, final Set<String> prefixes) {
this.exact = exact;
this.prefixes = prefixes;
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -1033,6 +1095,12 @@ public JexlFeatures sideEffect(final boolean flag) {
* When disabled, parsing a script/expression using syntactical constructs modifying variables
* <em>including all potentially ant-ish variables</em> will throw a parsing exception.
* </p>
* <p>
* When antish variable names are declared ({@link #antishTest(Predicate)}), they are all solved at parse time;
* only assigning a global variable, like <code>x = 1</code> or <code>x.y = 1</code> when <code>x.y</code>
* is declared, is then a global side effect. Assigning a property of a global variable value, like
* <code>x.y.z = 1</code>, is not.
* </p>
*
* @param flag true to enable, false to disable
* @return this features instance
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/org/apache/commons/jexl3/internal/Debugger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/apache/commons/jexl3/internal/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<JexlCache<?, ?>> factory = conf.cacheFactory();
Expand Down Expand Up @@ -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);
Comment thread
henrib marked this conversation as resolved.
Resolved
}
return opts;
}

Expand Down
Loading
Loading