This is an automated email from the ASF dual-hosted git repository.
henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git
The following commit(s) were added to refs/heads/master by this push:
new 9cdab2c3 JEXL: various nits & warnings;
9cdab2c3 is described below
commit 9cdab2c35a7d7934937590942a3e9f4330383ec0
Author: Henrib <[email protected]>
AuthorDate: Tue Feb 3 18:57:05 2026 +0100
JEXL: various nits & warnings;
---
.../org/apache/commons/jexl3/JexlArithmetic.java | 36 ++++---
.../jexl3/internal/introspection/IndexedType.java | 23 ++---
.../jexl3/internal/introspection/Introspector.java | 6 +-
.../introspection/PropertySetExecutor.java | 12 +--
.../org/apache/commons/jexl3/Issues400Test.java | 113 ++++++---------------
5 files changed, 69 insertions(+), 121 deletions(-)
diff --git a/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
b/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
index e75e03ab..f2d36eac 100644
--- a/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
+++ b/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
@@ -30,6 +30,7 @@ import java.math.MathContext;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.ToLongFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -1416,19 +1417,7 @@ public class JexlArithmetic {
* @return an Integer or Long if narrowing is possible, the original
BigDecimal otherwise
*/
protected Number narrowBigDecimal(final Object lhs, final Object rhs,
final BigDecimal big) {
- if (isNumberable(lhs) || isNumberable(rhs)) {
- try {
- final long l = big.longValueExact();
- // coerce to int when possible (int being so often used in
method parms)
- if ((int) l == l) {
- return (int) l;
- }
- return l;
- } catch (final ArithmeticException xa) {
- // ignore, no exact value possible
- }
- }
- return big;
+ return narrowToLong(lhs, rhs, big, BigDecimal::longValueExact);
}
/**
@@ -1446,9 +1435,24 @@ public class JexlArithmetic {
* @return an Integer or Long if narrowing is possible, the original
BigInteger otherwise
*/
protected Number narrowBigInteger(final Object lhs, final Object rhs,
final BigInteger big) {
+ return narrowToLong(lhs, rhs, big, BigInteger::longValueExact);
+ }
+
+ /**
+ * Given a generic number, attempt to narrow it to an Integer or Long if
it fits and
+ * one of the arguments is numberable.
+ *
+ * @param lhs the left-hand side operand that lead to the big result
+ * @param rhs the right-hand side operand that lead to the big result
+ * @param big the number to narrow
+ * @param toLongFunction the function to convert the number to a long
+ * @param <X> the number type
+ * @return an Integer or Long if narrowing is possible, the original
number otherwise
+ */
+ protected <X extends Number> Number narrowToLong(final Object lhs, final
Object rhs, final X big, final ToLongFunction<X> toLongFunction) {
if (isNumberable(lhs) || isNumberable(rhs)) {
try {
- final long l = big.longValueExact();
+ final long l = toLongFunction.applyAsLong(big);
// coerce to int when possible (int being so often used in
method parms)
if ((int) l == l) {
return (int) l;
@@ -1728,7 +1732,7 @@ public class JexlArithmetic {
/**
* Converts a string to an int.
- * <p>This ensure the represented number is a natural (not a real).</p>
+ * <p>This ensures the represented number is a natural (not a real).</p>
*
* @param arg the arg
* @return an int
@@ -1745,7 +1749,7 @@ public class JexlArithmetic {
/**
* Converts a string to a long.
- * <p>This ensure the represented number is a natural (not a real).</p>
+ * <p>This ensures the represented number is a natural (not a real).</p>
*
* @param arg the arg
* @return a long
diff --git
a/src/main/java/org/apache/commons/jexl3/internal/introspection/IndexedType.java
b/src/main/java/org/apache/commons/jexl3/internal/introspection/IndexedType.java
index 1449d6c3..dee0b502 100644
---
a/src/main/java/org/apache/commons/jexl3/internal/introspection/IndexedType.java
+++
b/src/main/java/org/apache/commons/jexl3/internal/introspection/IndexedType.java
@@ -110,12 +110,11 @@ public final class IndexedType implements JexlPropertyGet
{
public static JexlPropertyGet discover(final Introspector is, final Object
object, final String name) {
if (object != null && name != null && !name.isEmpty()) {
final String base = name.substring(0, 1).toUpperCase() +
name.substring(1);
- final String container = name;
final Class<?> clazz = object.getClass();
final Method[] getters = is.getMethods(object.getClass(), "get" +
base);
final Method[] setters = is.getMethods(object.getClass(), "set" +
base);
if (getters != null) {
- return new IndexedType(container, clazz, getters, setters);
+ return new IndexedType(name, clazz, getters, setters);
}
}
return null;
@@ -175,8 +174,8 @@ public final class IndexedType implements JexlPropertyGet {
if (getters != null && getters.length > 0) {
Method jm = get;
if (jm != null) {
- final Class<?>[] ptypes = jm.getParameterTypes();
- if (ptypes[0].isAssignableFrom(key.getClass())) {
+ final Class<?>[] types = jm.getParameterTypes();
+ if (types[0].isAssignableFrom(key.getClass())) {
return jm.invoke(object, key);
}
}
@@ -191,8 +190,7 @@ public final class IndexedType implements JexlPropertyGet {
}
}
throw new IntrospectionException("property get error: "
- + object.getClass().toString()
- + "@" + key.toString());
+ + object.getClass() + "@" + key.toString());
}
/**
@@ -209,10 +207,10 @@ public final class IndexedType implements JexlPropertyGet
{
if (setters != null && setters.length > 0) {
Method jm = set;
if (jm != null) {
- final Class<?>[] ptypes = jm.getParameterTypes();
- if (ptypes[0].isAssignableFrom(key.getClass())
+ final Class<?>[] types = jm.getParameterTypes();
+ if (types[0].isAssignableFrom(key.getClass())
&& (value == null
- || ptypes[1].isAssignableFrom(value.getClass()))) {
+ || types[1].isAssignableFrom(value.getClass()))) {
return jm.invoke(object, key, value);
}
}
@@ -227,8 +225,7 @@ public final class IndexedType implements JexlPropertyGet {
}
}
throw new IntrospectionException("property set error: "
- + object.getClass().toString()
- + "@" + key.toString());
+ + object.getClass() + "@" + key.toString());
}
@Override
@@ -237,8 +234,8 @@ public final class IndexedType implements JexlPropertyGet {
}
@Override
- public boolean tryFailed(final Object rval) {
- return rval == Uberspect.TRY_FAILED;
+ public boolean tryFailed(final Object val) {
+ return val == Uberspect.TRY_FAILED;
}
@Override
diff --git
a/src/main/java/org/apache/commons/jexl3/internal/introspection/Introspector.java
b/src/main/java/org/apache/commons/jexl3/internal/introspection/Introspector.java
index 43fc1c6e..8e962b59 100644
---
a/src/main/java/org/apache/commons/jexl3/internal/introspection/Introspector.java
+++
b/src/main/java/org/apache/commons/jexl3/internal/introspection/Introspector.java
@@ -133,12 +133,12 @@ public final class Introspector {
* Create the introspector.
*
* @param log the logger to use
- * @param cloader the class loader
+ * @param loader the class loader
* @param perms the permissions
*/
- public Introspector(final Log log, final ClassLoader cloader, final
JexlPermissions perms) {
+ public Introspector(final Log log, final ClassLoader loader, final
JexlPermissions perms) {
this.logger = log;
- this.loader = cloader;
+ this.loader = loader;
this.permissions = perms == null ? JexlPermissions.RESTRICTED : perms;
}
diff --git
a/src/main/java/org/apache/commons/jexl3/internal/introspection/PropertySetExecutor.java
b/src/main/java/org/apache/commons/jexl3/internal/introspection/PropertySetExecutor.java
index b6f024b5..2f006019 100644
---
a/src/main/java/org/apache/commons/jexl3/internal/introspection/PropertySetExecutor.java
+++
b/src/main/java/org/apache/commons/jexl3/internal/introspection/PropertySetExecutor.java
@@ -28,12 +28,12 @@ import org.apache.commons.jexl3.JexlException;
*/
public class PropertySetExecutor extends AbstractExecutor.Set {
- /** Index of the first character of the set{p,P}roperty. */
+ /** Index of the first character of the "set{p,P}roperty". */
private static final int SET_START_INDEX = 3;
/**
* Discovers a PropertySetExecutor.
- * <p>The method to be found should be named "set{P,p}property.</p>
+ * <p>The method to be found should be named "set{P,p}property".</p>
*
* @param is the introspector
* @param clazz the class to find the get method from
@@ -54,7 +54,7 @@ public class PropertySetExecutor extends AbstractExecutor.Set
{
/**
* Discovers the method for a {@link
org.apache.commons.jexl3.introspection.JexlPropertySet}.
- * <p>The method to be found should be named "set{P,p}property.
+ * <p>The method to be found should be named "set{P,p}property".
* As a special case, any empty array will try to find a valid
array-setting non-ambiguous method.
*
* @param is the introspector
@@ -188,10 +188,10 @@ public class PropertySetExecutor extends
AbstractExecutor.Set {
&& valueClass.equals(classOf(value))) {
try {
return invoke(o, value);
- } catch (IllegalAccessException | IllegalArgumentException xill) {
+ } catch (IllegalAccessException | IllegalArgumentException
illegal) {
return TRY_FAILED; // fail
- } catch (final InvocationTargetException xinvoke) {
- throw JexlException.tryFailed(xinvoke); // throw
+ } catch (final InvocationTargetException invoke) {
+ throw JexlException.tryFailed(invoke); // throw
}
}
return TRY_FAILED;
diff --git a/src/test/java/org/apache/commons/jexl3/Issues400Test.java
b/src/test/java/org/apache/commons/jexl3/Issues400Test.java
index 1ec15961..ae33d5eb 100644
--- a/src/test/java/org/apache/commons/jexl3/Issues400Test.java
+++ b/src/test/java/org/apache/commons/jexl3/Issues400Test.java
@@ -339,10 +339,7 @@ public class Issues400Test {
void test413d() {
final JexlBuilder builder = new JexlBuilder().features(new
JexlFeatures().constCapture(true));
final JexlEngine jexl = builder.create();
- final JexlException.Parsing xparse = assertThrows(
- JexlException.Parsing.class,
- () -> jexl.createScript("var c = 42; var f = y -> c += y;
f(z)", "z"),
- "c should be const");
+ final JexlException.Parsing xparse =
assertThrows(JexlException.Parsing.class, () -> jexl.createScript("var c = 42;
var f = y -> c += y; f(z)", "z"), "c should be const");
assertTrue(xparse.getMessage().contains("const"));
}
@@ -616,14 +613,7 @@ public class Issues400Test {
@Test
void test437a() {
final JexlEngine jexl = new JexlBuilder().create();
- final String src = "let values = [...]\n"
- + "function append(const value) {\n"
- + " values.add(value)\n"
- + "}\n"
- + "\n"
- + "append(1)\n"
- + "append(2)\n"
- + "return values ";
+ final String src = "let values = [...]\n" + "function append(const
value) {\n" + " values.add(value)\n" + "}\n" + "\n" + "append(1)\n" +
"append(2)\n" + "return values ";
final JexlScript script = jexl.createScript(src);
assertNotNull(script);
final Object result = script.execute(null);
@@ -637,13 +627,7 @@ public class Issues400Test {
final JexlFeatures features =
JexlFeatures.createDefault().ambiguousStatement(true);
assertTrue(features.supportsAmbiguousStatement());
final JexlEngine jexl = new JexlBuilder().features(features).create();
- final String src = "let values = [...]"
- + "function append(const value) {"
- + " values.add(value)"
- + "}"
- + "append(1)"
- + "append(2)"
- + "return values ";
+ final String src = "let values = [...]" + "function append(const
value) {" + " values.add(value)" + "}" + "append(1)" + "append(2)" + "return
values ";
final JexlScript script = jexl.createScript(src);
assertNotNull(script);
final Object result = script.execute(null);
@@ -789,12 +773,9 @@ public class Issues400Test {
void testIssue442() {
final JexlEngine jexl = new JexlBuilder().create();
final JexlContext context = new MapContext();
- final String code = "var x = 'hello';\n" +
- "function test(z) {\n" +
- //"x + ' ' + z\n"+
- "`${x} ${z}`;\n" +
- "}\n" +
- "test('world');";
+ final String code = "var x = 'hello';\n" + "function test(z) {\n" +
+ //"x + ' ' + z\n"+
+ "`${x} ${z}`;\n" + "}\n" + "test('world');";
final JexlScript script = jexl.createScript(code);
final Object result = script.execute(context);
Assertions.assertEquals("hello world", result);
@@ -808,15 +789,14 @@ public class Issues400Test {
final JexlScript script = jexl.createScript(src, "a", "b");
final Object result = script.execute(null, "a", "b");
Assertions.assertEquals("a\n?= ba\n?== b", result);
- String[] locals = script.getLocalVariables();
+ String[] locals = script.getLocalVariables();
Assertions.assertArrayEquals(new String[]{"c", "foo"}, locals);
final String TEST447 = "src/test/scripts/test447.jexl";
final File src447 = new File(TEST447);
final JexlScript script447 = jexl.createScript(src447);
final Object result447 = script447.execute(null);
Assertions.assertInstanceOf(List.class, result447);
- @SuppressWarnings("unchecked")
- final List<Boolean> list = (List<Boolean>) result447;
+ @SuppressWarnings("unchecked") final List<Boolean> list =
(List<Boolean>) result447;
for (final Boolean item : list) {
Assertions.assertTrue(item);
}
@@ -838,27 +818,21 @@ public class Issues400Test {
@Test
void test450a() {
JexlEngine jexl0 = new
JexlBuilder().silent(false).permissions(JexlPermissions.RESTRICTED).create();
- assertThrows(JexlException.Method.class, ()->jexl0.newInstance(
- "org.apache.commons.jexl3.internal.introspection.Uberspect", null,
null),
- "should not be able to create Uberspect with RESTRICTED");
+ assertThrows(JexlException.Method.class, () ->
jexl0.newInstance("org.apache.commons.jexl3.internal.introspection.Uberspect",
null, null), "should not be able to create Uberspect with RESTRICTED");
JexlPermissions perm = new
JexlPermissions.ClassPermissions(org.apache.commons.jexl3.internal.introspection.Uberspect.class);
JexlEngine jexl1 = new
JexlBuilder().silent(false).permissions(perm).create();
- assertNotNull(jexl1.newInstance(
-
"org.apache.commons.jexl3.internal.introspection.Uberspect", null, null),
- "should able to create Uberspect with Uberspect permission");
+
assertNotNull(jexl1.newInstance("org.apache.commons.jexl3.internal.introspection.Uberspect",
null, null), "should able to create Uberspect with Uberspect permission");
}
@Test
void test450b() {
// cannot load System with RESTRICTED
- assertThrows(JexlException.Method.class,
- () -> run450b(JexlPermissions.RESTRICTED), "should not be able
to load System with RESTRICTED");
+ assertThrows(JexlException.Method.class, () ->
run450b(JexlPermissions.RESTRICTED), "should not be able to load System with
RESTRICTED");
// can load System with UNRESTRICTED
assertEquals(java.lang.System.class, run450b(UNRESTRICTED));
// need to explicitly allow Uberspect and the current class loader to
load System
- JexlPermissions perm = new JexlPermissions.ClassPermissions(
- getClass().getClassLoader().getClass(),
org.apache.commons.jexl3.internal.introspection.Uberspect.class);
+ JexlPermissions perm = new
JexlPermissions.ClassPermissions(getClass().getClassLoader().getClass(),
org.apache.commons.jexl3.internal.introspection.Uberspect.class);
assertEquals(java.lang.System.class, run450b(perm));
}
@@ -874,93 +848,66 @@ public class Issues400Test {
// can reach and invoke System::currentTimeMillis with UNRESTRICTED
assertNotNull(run450c(UNRESTRICTED));
// need explicit permissions to ClassPermissions and Uberspect to
reach and invoke System::currentTimeMillis
- JexlPermissions perm = new JexlPermissions.ClassPermissions(
- JexlPermissions.ClassPermissions.class,
- org.apache.commons.jexl3.internal.introspection.Uberspect.class);
+ JexlPermissions perm = new
JexlPermissions.ClassPermissions(JexlPermissions.ClassPermissions.class,
org.apache.commons.jexl3.internal.introspection.Uberspect.class);
assertNotNull(run450c(perm));
// cannot reach and invoke System::currentTimeMillis with RESTRICTED
- assertThrows(JxltEngine.Exception.class,
- () -> run450c(JexlPermissions.RESTRICTED), "should not be able
to load System with RESTRICTED");
+ assertThrows(JxltEngine.Exception.class, () ->
run450c(JexlPermissions.RESTRICTED), "should not be able to load System with
RESTRICTED");
}
private static Object run450c(JexlPermissions perm) {
JexlBuilder builder = new
JexlBuilder().silent(false).permissions(perm);
- Object result = new TemplateEngine(new Engine32(builder),false, 2,
'$', '#').createExpression(
- "${x = new
('org.apache.commons.jexl3.internal.introspection.Uberspect', null, null,
UNRESTRICTED);" +
- "sys = x?.getClassLoader()?.loadClass('java.lang.System') ?:
SYSTEM;" + // fail to create uberspect with java 8
- "p =
new('org.apache.commons.jexl3.introspection.JexlPermissions$ClassPermissions',
[sys]);" +
- "c =
new('org.apache.commons.jexl3.internal.introspection.Uberspect', null, null,
p);" +
- "z = c.getMethod(sys,'currentTimeMillis').invoke(x,null);}"
- ).evaluate(new BrkContext());
+ Object result = new TemplateEngine(new Engine32(builder), false, 2,
'$', '#').createExpression("${x = new
('org.apache.commons.jexl3.internal.introspection.Uberspect', null, null,
UNRESTRICTED);" + "sys = x?.getClassLoader()?.loadClass('java.lang.System') ?:
SYSTEM;" + // fail to create uberspect with java 8
+ "p =
new('org.apache.commons.jexl3.introspection.JexlPermissions$ClassPermissions',
[sys]);" + "c =
new('org.apache.commons.jexl3.internal.introspection.Uberspect', null, null,
p);" + "z =
c.getMethod(sys,'currentTimeMillis').invoke(x,null);}").evaluate(new
BrkContext());
return result;
}
@Test
void test450() {
- assertNotNull(run450(JexlPermissions.UNRESTRICTED),
- "should be able to reach and invoke System::currentTimeMillis
with UNRESTRICTED");
- assertNotNull(run450(new
JexlPermissions.ClassPermissions(org.apache.commons.jexl3.internal.TemplateEngine.class)),
- "should be able to reach and invoke System::currentTimeMillis
with TemplateEngine permission");
- assertThrows(JexlException.Method.class,
- () -> run450(RESTRICTED),
- "should not be able to reach and invoke
System::currentTimeMillis with RESTRICTED");
+ assertNotNull(run450(JexlPermissions.UNRESTRICTED), "should be able to
reach and invoke System::currentTimeMillis with UNRESTRICTED");
+ assertNotNull(run450(new
JexlPermissions.ClassPermissions(org.apache.commons.jexl3.internal.TemplateEngine.class)),
"should be able to reach and invoke System::currentTimeMillis with
TemplateEngine permission");
+ assertThrows(JexlException.Method.class, () -> run450(RESTRICTED),
"should not be able to reach and invoke System::currentTimeMillis with
RESTRICTED");
}
public static class Engine33 extends Engine32 {
public Engine33() {
this(createBuilder());
}
+
public Engine33(JexlBuilder builder) {
super(builder);
}
+
static JexlBuilder createBuilder() {
- JexlPermissions perm = new JexlPermissions.ClassPermissions(
- Issues400Test.class.getClassLoader().getClass(),
- JexlPermissions.ClassPermissions.class,
- org.apache.commons.jexl3.internal.TemplateEngine.class,
-
org.apache.commons.jexl3.internal.introspection.Uberspect.class);
+ JexlPermissions perm = new
JexlPermissions.ClassPermissions(Issues400Test.class.getClassLoader().getClass(),
JexlPermissions.ClassPermissions.class,
org.apache.commons.jexl3.internal.TemplateEngine.class,
org.apache.commons.jexl3.internal.introspection.Uberspect.class);
return new
JexlBuilder().safe(false).silent(false).permissions(perm);
}
}
private static Object run450(JexlPermissions perm) {
JexlEngine jexl = new
JexlBuilder().silent(false).strict(true).safe(false).permissions(perm).create();
- return
jexl.createScript("new('org.apache.commons.jexl3.internal.TemplateEngine'," +
-
"new('org.apache.commons.jexl3.Issues400Test$Engine33'),false,256,'$'.charAt(0),'#'.charAt(0))"
+
- ".createExpression(" +
- "\"#{x = new
('org.apache.commons.jexl3.internal.introspection.Uberspect', null, null);" +
- "sys = x?.getClassLoader().loadClass('java.lang.System')
?: SYSTEM;" + // fail to load System on Java 8
- "p =
new('org.apache.commons.jexl3.introspection.JexlPermissions$ClassPermissions',
[sys]);" +
- "c =
new('org.apache.commons.jexl3.internal.introspection.Uberspect', null, null,
p);" +
- "z =
c.getMethod(sys,'currentTimeMillis').invoke(x,null);}\")" +
-
".evaluate(new('org.apache.commons.jexl3.Issues400Test$BrkContext'))").execute(null);
+ return
jexl.createScript("new('org.apache.commons.jexl3.internal.TemplateEngine'," +
"new('org.apache.commons.jexl3.Issues400Test$Engine33'),false,256,'$'.charAt(0),'#'.charAt(0))"
+ ".createExpression(" + "\"#{x = new
('org.apache.commons.jexl3.internal.introspection.Uberspect', null, null);" +
"sys = x?.getClassLoader().loadClass('java.lang.System') ?: SYSTEM;" + // fail
to load System on Java 8
+ "p =
new('org.apache.commons.jexl3.introspection.JexlPermissions$ClassPermissions',
[sys]);" + "c =
new('org.apache.commons.jexl3.internal.introspection.Uberspect', null, null,
p);" + "z = c.getMethod(sys,'currentTimeMillis').invoke(x,null);}\")" +
".evaluate(new('org.apache.commons.jexl3.Issues400Test$BrkContext'))").execute(null);
}
@Test
void test451() {
JexlEngine jexl = new JexlBuilder().create();
- assertEquals("42",
- jexl.createScript("o.toString()", "o").execute(null, "42"));
+ assertEquals("42", jexl.createScript("o.toString()",
"o").execute(null, "42"));
JexlPermissions perms = RESTRICTED.compose("java.lang { +Class {
getSimpleName(); } }");
JexlSandbox sandbox = new JexlSandbox(false, true);
sandbox.permissions(Object.class.getName(), true, true, false, false);
sandbox.allow(String.class.getName()).execute("toString");
final JexlEngine jexl451 = new
JexlBuilder().safe(false).silent(false).permissions(perms).sandbox(sandbox).create();
// sandbox allows String::toString
- assertEquals("42",
- jexl451.createScript("o.toString()", "o").execute(null, "42"));
+ assertEquals("42", jexl451.createScript("o.toString()",
"o").execute(null, "42"));
// sandbox forbids getClass
- assertThrows(JexlException.Method.class,
- () -> jexl451.createScript("oo.getClass()",
"oo").execute(null, "42"));
+ assertThrows(JexlException.Method.class, () ->
jexl451.createScript("oo.getClass()", "oo").execute(null, "42"));
// sandbox allows reading properties, permissions allow getClass
- assertEquals(String.class,
- jexl451.createScript("o.class", "o").execute(null, "42"));
+ assertEquals(String.class, jexl451.createScript("o.class",
"o").execute(null, "42"));
// sandbox allows reading properties, permissions allow getSimpleName
- assertEquals("Object",
- jexl451.createScript("o.class.simpleName", "o").execute(null,
new Object()));
+ assertEquals("Object", jexl451.createScript("o.class.simpleName",
"o").execute(null, new Object()));
// sandbox allows reading properties, permissions forbids
getClassLoader
- assertThrows(JexlException.Property.class,
- () -> jexl451.createScript("o.class.classLoader",
"o").execute(null, new Object()));
+ assertThrows(JexlException.Property.class, () ->
jexl451.createScript("o.class.classLoader", "o").execute(null, new Object()));
}
}