Author: henrib
Date: Fri Mar 10 14:32:47 2017
New Revision: 1786350
URL: http://svn.apache.org/viewvc?rev=1786350&view=rev
Log:
JEXL-211:
Protect various executors from null properties in tryInvoke
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Script.java
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/BooleanGetExecutor.java
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/DuckGetExecutor.java
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/DuckSetExecutor.java
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/IndexedType.java
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/MapGetExecutor.java
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/MapSetExecutor.java
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Script.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Script.java?rev=1786350&r1=1786349&r2=1786350&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Script.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Script.java
Fri Mar 10 14:32:47 2017
@@ -74,7 +74,10 @@ public class Script implements JexlScrip
protected void checkCacheVersion() {
int uberVersion = jexl.getUberspect().getVersion();
if (version != uberVersion) {
- script.clearCache();
+ // version 0 of the uberSpect is an illusion due to order of
construction; no need to clear cache
+ if (version > 0) {
+ script.clearCache();
+ }
version = uberVersion;
}
}
@@ -176,13 +179,7 @@ public class Script implements JexlScrip
*/
@Override
public Object evaluate(JexlContext context) {
- if (script.jjtGetNumChildren() < 1) {
- return null;
- }
- checkCacheVersion();
- Scope.Frame frame = createFrame((Object[]) null);
- Interpreter interpreter = createInterpreter(context, frame);
- return interpreter.interpret(script.jjtGetChild(0));
+ return execute(context);
}
/**
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/BooleanGetExecutor.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/BooleanGetExecutor.java?rev=1786350&r1=1786349&r2=1786350&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/BooleanGetExecutor.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/BooleanGetExecutor.java
Fri Mar 10 14:32:47 2017
@@ -35,12 +35,13 @@ public final class BooleanGetExecutor ex
* @return the executor if found, null otherwise
*/
public static BooleanGetExecutor discover(Introspector is, final Class<?>
clazz, String property) {
- java.lang.reflect.Method m = PropertyGetExecutor.discoverGet(is, "is",
clazz, property);
- if (m != null && (m.getReturnType() == Boolean.TYPE ||
m.getReturnType() == Boolean.class)) {
- return new BooleanGetExecutor(clazz, m, property);
- } else {
- return null;
+ if (property != null && !property.isEmpty()) {
+ java.lang.reflect.Method m = PropertyGetExecutor.discoverGet(is,
"is", clazz, property);
+ if (m != null && (m.getReturnType() == Boolean.TYPE ||
m.getReturnType() == Boolean.class)) {
+ return new BooleanGetExecutor(clazz, m, property);
+ }
}
+ return null;
}
/**
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/DuckGetExecutor.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/DuckGetExecutor.java?rev=1786350&r1=1786349&r2=1786350&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/DuckGetExecutor.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/DuckGetExecutor.java
Fri Mar 10 14:32:47 2017
@@ -30,7 +30,7 @@ import java.lang.reflect.InvocationTarge
* @since 2.0
*/
public final class DuckGetExecutor extends AbstractExecutor.Get {
- /** The property. */
+ /** The property, may be null. */
private final Object property;
/**
@@ -69,10 +69,12 @@ public final class DuckGetExecutor exten
@Override
public Object tryInvoke(Object obj, Object key) {
- if (obj != null && method != null
- // ensure method name matches the property name
- && property.equals(key)
- && objectClass.equals(obj.getClass())) {
+ if (obj != null
+ && objectClass.equals(obj.getClass())
+ // ensure method name matches the property name
+ && method != null
+ && ((property == null && key == null)
+ || (property != null && property.equals(key)))) {
try {
Object[] args = {property};
return method.invoke(obj, args);
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/DuckSetExecutor.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/DuckSetExecutor.java?rev=1786350&r1=1786349&r2=1786350&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/DuckSetExecutor.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/DuckSetExecutor.java
Fri Mar 10 14:32:47 2017
@@ -36,7 +36,7 @@ import java.lang.reflect.InvocationTarge
* @since 2.0
*/
public final class DuckSetExecutor extends AbstractExecutor.Set {
- /** The property. */
+ /** The property, may be null. */
private final Object property;
/**
@@ -83,10 +83,11 @@ public final class DuckSetExecutor exten
@Override
public Object tryInvoke(Object obj, Object key, Object value) {
- if (obj != null && method != null
- // ensure method name matches the property name
- && property.equals(key)
- && objectClass.equals(obj.getClass())) {
+ if (obj != null
+ && objectClass.equals(obj.getClass())
+ && method != null
+ && ((property != null && property.equals(key))
+ || (property == null && key == null))) {
try {
Object[] args = {property, value};
method.invoke(obj, args);
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/IndexedType.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/IndexedType.java?rev=1786350&r1=1786349&r2=1786350&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/IndexedType.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/IndexedType.java
Fri Mar 10 14:32:47 2017
@@ -46,10 +46,10 @@ public final class IndexedType implement
* @param is the introspector
* @param object the object
* @param name the container name
- * @return a JexlPropertyGet is successfull, null otherwise
+ * @return a JexlPropertyGet is successful, null otherwise
*/
public static JexlPropertyGet discover(Introspector is, Object object,
String name) {
- if (object != null && name != null) {
+ if (object != null && name != null && !name.isEmpty()) {
String base = name.substring(0, 1).toUpperCase() +
name.substring(1);
final String container = name;
final Class<?> clazz = object.getClass();
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/MapGetExecutor.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/MapGetExecutor.java?rev=1786350&r1=1786349&r2=1786350&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/MapGetExecutor.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/MapGetExecutor.java
Fri Mar 10 14:32:47 2017
@@ -62,7 +62,6 @@ public final class MapGetExecutor extend
return property;
}
-
@Override
public Object invoke(final Object obj) {
@SuppressWarnings("unchecked") // ctor only allows Map instances - see
discover() method
@@ -72,9 +71,11 @@ public final class MapGetExecutor extend
@Override
public Object tryInvoke(final Object obj, Object key) {
- if (obj != null && method != null
- && objectClass.equals(obj.getClass())
- && (key == null ||
property.getClass().equals(key.getClass()))) {
+ if (obj != null
+ && method != null
+ && objectClass.equals(obj.getClass())
+ && ((property == null && key == null)
+ || (property != null && key != null &&
property.getClass().equals(key.getClass())))) {
@SuppressWarnings("unchecked") // ctor only allows Map instances -
see discover() method
final Map<Object, ?> map = (Map<Object, ?>) obj;
return map.get(key);
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/MapSetExecutor.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/MapSetExecutor.java?rev=1786350&r1=1786349&r2=1786350&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/MapSetExecutor.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/introspection/MapSetExecutor.java
Fri Mar 10 14:32:47 2017
@@ -72,9 +72,11 @@ public final class MapSetExecutor extend
@Override
public Object tryInvoke(final Object obj, Object key, Object value) {
- if (obj != null && method != null
+ if (obj != null
+ && method != null
&& objectClass.equals(obj.getClass())
- && (key == null || property.getClass().equals(key.getClass()))) {
+ && ((property == null && key == null)
+ || (property != null && key != null &&
property.getClass().equals(key.getClass())))) {
@SuppressWarnings("unchecked") // ctor only allows Map instances -
see discover() method
final Map<Object,Object> map = ((Map<Object, Object>) obj);
map.put(key, value);
Modified:
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java?rev=1786350&r1=1786349&r2=1786350&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java
(original)
+++
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java
Fri Mar 10 14:32:47 2017
@@ -1233,7 +1233,7 @@ public class IssuesTest extends JexlTest
public class T210 {
public void npe() {
- throw new NullPointerException("NPE");
+ throw new NullPointerException("NPE210");
}
}
@@ -1241,26 +1241,14 @@ public class IssuesTest extends JexlTest
public void test210() throws Exception {
JexlContext jc = new MapContext();
jc.set("v210", new T210());
- JexlEngine jexl;
- JexlScript e;
- Object r;
- jexl = new JexlBuilder().strict(true).silent(false).create();
- e = jexl.createScript("v210.npe()");
- try {
- r = e.execute(jc);
- Assert.fail("should have thrown an exception");
- } catch(JexlException xjexl) {
- Throwable th = xjexl.getCause();
-
Assert.assertTrue(NullPointerException.class.equals(th.getClass()));
- }
- jexl = new JexlBuilder().strict(false).silent(false).create();
- e = jexl.createScript("v210.npe()");
+ JexlEngine jexl = new
JexlBuilder().strict(false).silent(false).create();
+ JexlScript e = jexl.createScript("v210.npe()");
try {
- r = e.execute(jc);
+ e.execute(jc);
Assert.fail("should have thrown an exception");
} catch(JexlException xjexl) {
Throwable th = xjexl.getCause();
-
Assert.assertTrue(NullPointerException.class.equals(th.getClass()));
+ Assert.assertEquals("NPE210", th.getMessage());
}
}
@@ -1272,10 +1260,10 @@ public class IssuesTest extends JexlTest
JexlScript e;
Object r;
jexl = new JexlBuilder().strict(false).silent(false).create();
- e = jexl.createScript("foo[3]");
+ e = jexl.createScript("foo[3]");
r = e.execute(jc);
Assert.assertEquals(42, r);
-
+
// cache and fail?
jc.set("foo", new int[]{0, 1});
jc.setStrict(true);
@@ -1291,4 +1279,22 @@ public class IssuesTest extends JexlTest
r = e.execute(jc);
Assert.assertNull("oob adverted", r);
}
+
+
+ @Test
+ public void test221() throws Exception {
+ JexlEvalContext jc = new JexlEvalContext();
+ Map<String, Integer> map = new HashMap<String, Integer>();
+ map.put("one", 1);
+ jc.set("map", map);
+ JexlEngine jexl = new JexlBuilder().cache(256).create();
+ JexlScript e = jexl.createScript("(x)->{ map[x] }");
+ Object r;
+ r = e.execute(jc, null);
+ Assert.assertEquals(null, r);
+ r = e.execute(jc, null);
+ Assert.assertEquals(null, r);
+ r = e.execute(jc, "one");
+ Assert.assertEquals(1, r);
+ }
}