Author: henrib
Date: Thu Nov 12 17:22:06 2009
New Revision: 835458
URL: http://svn.apache.org/viewvc?rev=835458&view=rev
Log:
Changed exception handling; ParseException is no longer exposed through public
APIs, only JexlException and UnifiedJEXL.Exception are used, the API now only
uses unchecked exceptions.
Added newInstance to JexlEngine to complete the invokeMethod &
{set,get}Property.
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/ExpressionFactory.java
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/JexlEngine.java
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/JexlException.java
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/Main.java
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/UnifiedJEXL.java
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/scripting/JexlScriptEngine.java
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/AssignTest.java
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/IssuesTest.java
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/JexlTest.java
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/JexlTestCase.java
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/MethodTest.java
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/ParseFailuresTest.java
commons/proper/jexl/trunk/xdocs/changes.xml
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/ExpressionFactory.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/ExpressionFactory.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/ExpressionFactory.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/ExpressionFactory.java
Thu Nov 12 17:22:06 2009
@@ -16,8 +16,6 @@
*/
package org.apache.commons.jexl;
-import org.apache.commons.jexl.parser.ParseException;
-
/**
* Creates Expression objects.
* <p>
@@ -59,14 +57,13 @@
* must contain either a reference or an expression.
* @param expression A String containing valid JEXL syntax
* @return An Expression object which can be evaluated with a JexlContext
- * @throws ParseException An exception can be thrown if there is a problem
+ * @throws JexlException An exception can be thrown if there is a problem
* parsing this expression, or if the expression is neither an
* expression or a reference.
* @deprecated Create a JexlEngine and use createExpression() on that
*/
@Deprecated
- public static Expression createExpression(String expression)
- throws ParseException {
+ public static Expression createExpression(String expression) {
return ScriptFactory.getInstance().createExpression(expression);
}
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/JexlEngine.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/JexlEngine.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/JexlEngine.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/JexlEngine.java
Thu Nov 12 17:22:06 2009
@@ -24,6 +24,7 @@
import java.io.StringReader;
import java.io.Reader;
import java.lang.ref.SoftReference;
+import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.Collections;
import java.net.URL;
@@ -39,7 +40,6 @@
import org.apache.commons.jexl.parser.TokenMgrError;
import org.apache.commons.jexl.parser.ASTJexlScript;
import org.apache.commons.jexl.util.Introspector;
-import org.apache.commons.jexl.util.introspection.DebugInfo;
import org.apache.commons.jexl.util.introspection.Uberspect;
import org.apache.commons.jexl.util.introspection.Info;
import org.apache.commons.jexl.util.introspection.JexlMethod;
@@ -302,12 +302,11 @@
* must contain either a reference or an expression.
* @param expression A String containing valid JEXL syntax
* @return An Expression object which can be evaluated with a JexlContext
- * @throws ParseException An exception can be thrown if there is a problem
+ * @throws JexlException An exception can be thrown if there is a problem
* parsing this expression, or if the expression is neither an
* expression nor a reference.
*/
- public Expression createExpression(String expression)
- throws ParseException {
+ public Expression createExpression(String expression) {
return createExpression(expression, null);
}
@@ -318,12 +317,11 @@
* @param expression A String containing valid JEXL syntax
* @return An Expression object which can be evaluated with a JexlContext
* @param info An info structure to carry debugging information if needed
- * @throws ParseException An exception can be thrown if there is a problem
+ * @throws JexlException An exception can be thrown if there is a problem
* parsing this expression, or if the expression is neither an
* expression or a reference.
*/
- public Expression createExpression(String expression, Info info)
- throws ParseException {
+ public Expression createExpression(String expression, Info info) {
// Parse the expression
ASTJexlScript tree = parse(expression, info);
if (tree.jjtGetNumChildren() > 1) {
@@ -338,9 +336,9 @@
*
* @param scriptText A String containing valid JEXL syntax
* @return A {...@link Script} which can be executed using a {...@link
JexlContext}.
- * @throws ParseException if there is a problem parsing the script.
+ * @throws JexlException if there is a problem parsing the script.
*/
- public Script createScript(String scriptText) throws ParseException {
+ public Script createScript(String scriptText) {
return createScript(scriptText, null);
}
@@ -351,9 +349,9 @@
* @param scriptText A String containing valid JEXL syntax
* @param info An info structure to carry debugging information if needed
* @return A {...@link Script} which can be executed using a {...@link
JexlContext}.
- * @throws ParseException if there is a problem parsing the script.
+ * @throws JexlException if there is a problem parsing the script.
*/
- public Script createScript(String scriptText, Info info) throws
ParseException {
+ public Script createScript(String scriptText, Info info) {
if (scriptText == null) {
throw new NullPointerException("scriptText is null");
}
@@ -371,9 +369,9 @@
* @return A {...@link Script} which can be executed with a
* {...@link JexlContext}.
* @throws IOException if there is a problem reading the script.
- * @throws ParseException if there is a problem parsing the script.
+ * @throws JexlException if there is a problem parsing the script.
*/
- public Script createScript(File scriptFile) throws ParseException,
IOException {
+ public Script createScript(File scriptFile) throws IOException {
if (scriptFile == null) {
throw new NullPointerException("scriptFile is null");
}
@@ -398,9 +396,9 @@
* @return A {...@link Script} which can be executed with a
* {...@link JexlContext}.
* @throws IOException if there is a problem reading the script.
- * @throws ParseException if there is a problem parsing the script.
+ * @throws JexlException if there is a problem parsing the script.
*/
- public Script createScript(URL scriptUrl) throws ParseException,
IOException {
+ public Script createScript(URL scriptUrl) throws IOException {
if (scriptUrl == null) {
throw new NullPointerException("scriptUrl is null");
}
@@ -469,12 +467,6 @@
return null;
}
throw xjexl;
- } catch (ParseException xparse) {
- if (silent) {
- logger.warn(xparse.getMessage(), xparse.getCause());
- return null;
- }
- throw new JexlException(null, "parsing error", xparse);
}
}
@@ -536,12 +528,6 @@
return;
}
throw xjexl;
- } catch (ParseException xparse) {
- if (silent) {
- logger.warn(xparse.getMessage(), xparse.getCause());
- return;
- }
- throw new JexlException(null, "parsing error", xparse);
}
}
@@ -556,18 +542,77 @@
public Object invokeMethod(Object obj, String meth, Object... args) {
JexlException xjexl = null;
Object result = null;
+ Info info = debugInfo();
try {
- JexlMethod method = uberspect.getMethod(obj, meth, args,
DebugInfo.NONE);
+ JexlMethod method = uberspect.getMethod(obj, meth, args, info);
if (method == null && arithmetic.narrowArguments(args)) {
- method = uberspect.getMethod(obj, meth, args, DebugInfo.NONE);
+ method = uberspect.getMethod(obj, meth, args, info);
}
if (method != null) {
result = method.invoke(obj, args);
} else {
- xjexl = new JexlException(null, "failed finding method " +
meth);
+ xjexl = new JexlException(info, "failed finding method " +
meth);
+ }
+ } catch (Exception xany) {
+ xjexl = new JexlException(info, "failed executing method " + meth,
xany);
+ } finally {
+ if (xjexl != null) {
+ if (silent) {
+ logger.warn(xjexl.getMessage(), xjexl.getCause());
+ return null;
+ }
+ throw xjexl;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Creates a new instance of an object using the most appropriate
constructor
+ * based on the arguments.
+ * @param <T> the type of object
+ * @param clazz the class to instantiate
+ * @param args the constructor arguments
+ * @return the created object instance or null on failure when silent
+ */
+ public <T> T newInstance(Class<? extends T> clazz, Object...args) {
+ return clazz.cast(doCreateInstance(clazz, args));
+ }
+
+ /**
+ * Creates a new instance of an object using the most appropriate
constructor
+ * based on the arguments.
+ * @param clazz the name of the class to instantiate resolved through this
engine's class loader
+ * @param args the constructor arguments
+ * @return the created object instance or null on failure when silent
+ */
+ public Object newInstance(String clazz, Object...args) {
+ return doCreateInstance(clazz, args);
+ }
+
+ /**
+ * Creates a new instance of an object using the most appropriate
constructor
+ * based on the arguments.
+ * @param clazz the class to instantiate
+ * @param args the constructor arguments
+ * @return the created object instance or null on failure when silent
+ */
+ protected Object doCreateInstance(Object clazz, Object...args) {
+ JexlException xjexl = null;
+ Object result = null;
+ Info info = debugInfo();
+ try {
+ Constructor<?> ctor = uberspect.getConstructor(clazz, args, info);
+ if (ctor == null && arithmetic.narrowArguments(args)) {
+ ctor = uberspect.getConstructor(clazz, args, info);
+ }
+ if (ctor != null) {
+ result = ctor.newInstance(args);
+ } else {
+ xjexl = new JexlException(info, "failed finding constructor
for " + clazz.toString());
}
} catch (Exception xany) {
- xjexl = new JexlException(null, "failed executing method " + meth,
xany);
+ xjexl = new JexlException(info, "failed executing constructor for
" + clazz.toString(), xany);
} finally {
if (xjexl != null) {
if (silent) {
@@ -684,13 +729,12 @@
* @param expression the expression to parse
* @param info debug information structure
* @return the parsed tree
- * @throws ParseException if any error occured during parsing
+ * @throws JexlException if any error occured during parsing
*/
- protected ASTJexlScript parse(CharSequence expression, Info info) throws
ParseException {
+ protected ASTJexlScript parse(CharSequence expression, Info info) {
String expr = cleanExpression(expression);
ASTJexlScript tree = null;
synchronized (parser) {
- logger.debug("Parsing expression: " + expression);
if (cache != null) {
tree = cache.get(expr);
if (tree != null) {
@@ -700,48 +744,62 @@
try {
Reader reader = new StringReader(expr);
// use first calling method of JexlEngine as debug info
- if (info == null && debug) {
- Throwable xinfo = new Throwable();
- xinfo.fillInStackTrace();
- StackTraceElement[] stack = xinfo.getStackTrace();
- StackTraceElement se = null;
- Class<?> clazz = getClass();
- for (int s = 1; s < stack.length; ++s, se = null) {
- se = stack[s];
- String className = se.getClassName();
- if (!className.equals(clazz.getName())) {
- // go deeper if called from JexlEngine,
UnifiedJEXL or a Factory
- if (className.equals(JexlEngine.class.getName())) {
- clazz = JexlEngine.class;
- } else if
(className.equals(UnifiedJEXL.class.getName())) {
- clazz = UnifiedJEXL.class;
- } else if
(className.equals(ScriptFactory.class.getName())) {
- clazz = ScriptFactory.class;
- } else if
(className.equals(ExpressionFactory.class.getName())) {
- clazz = ExpressionFactory.class;
- } else {
- break;
- }
- }
- }
- if (se != null) {
- info = new Info(se.getClassName() + "." +
se.getMethodName(), se.getLineNumber(), 0);
- }
+ if (info == null) {
+ info = debugInfo();
}
tree = parser.parse(reader, info);
if (cache != null) {
cache.put(expr, tree);
}
- } catch (TokenMgrError tme) {
- throw new ParseException(tme.getMessage());
- } catch (ParseException e) {
- throw e;
+ } catch (TokenMgrError xtme) {
+ throw new JexlException(info, "parsing failed", xtme);
+ } catch (ParseException xparse) {
+ throw new JexlException(info, "pasing failed", xparse);
}
}
return tree;
}
/**
+ * Creates and fills up debugging information.
+ * <p>This gathers the class, method and line number of the first calling
method
+ * not owned by JexlEngine, UnifiedJEXL or {Script,Expression}Factory.</p>
+ * @return an Info if debug is set, null otherwise
+ */
+ protected Info debugInfo() {
+ Info info = null;
+ if (debug) {
+ Throwable xinfo = new Throwable();
+ xinfo.fillInStackTrace();
+ StackTraceElement[] stack = xinfo.getStackTrace();
+ StackTraceElement se = null;
+ Class<?> clazz = getClass();
+ for (int s = 1; s < stack.length; ++s, se = null) {
+ se = stack[s];
+ String className = se.getClassName();
+ if (!className.equals(clazz.getName())) {
+ // go deeper if called from JexlEngine, UnifiedJEXL or a
Factory
+ if (className.equals(JexlEngine.class.getName())) {
+ clazz = JexlEngine.class;
+ } else if (className.equals(UnifiedJEXL.class.getName())) {
+ clazz = UnifiedJEXL.class;
+ } else if
(className.equals(ScriptFactory.class.getName())) {
+ clazz = ScriptFactory.class;
+ } else if
(className.equals(ExpressionFactory.class.getName())) {
+ clazz = ExpressionFactory.class;
+ } else {
+ break;
+ }
+ }
+ }
+ if (se != null) {
+ info = new Info(se.getClassName() + "." + se.getMethodName(),
se.getLineNumber(), 0);
+ }
+ }
+ return info;
+ }
+
+ /**
* Trims the expression from front & ending spaces.
* @param str expression to clean
* @return trimmed expression ending in a semi-colon
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/JexlException.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/JexlException.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/JexlException.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/JexlException.java
Thu Nov 12 17:22:06 2009
@@ -17,6 +17,7 @@
package org.apache.commons.jexl;
import org.apache.commons.jexl.parser.JexlNode;
+import org.apache.commons.jexl.util.introspection.Info;
/**
* Wraps any error that might occur during interpretation of a script or
expression.
@@ -26,7 +27,9 @@
/** Serial version UID. */
private static final long serialVersionUID = 2690666400232612395L;
/** The point of origin for this exception. */
- protected JexlNode mark;
+ protected final JexlNode mark;
+ /** The debug info */
+ protected final Info info;
/** A marker to use in NPEs stating a null operand error. */
public static final String NULL_OPERAND = "jexl.null";
/**
@@ -37,7 +40,10 @@
public JexlException(JexlNode node, String msg) {
super(msg);
mark = node;
+ info = node != null? node.getInfo() : null;
+
}
+
/**
* Creates a new JexlException.
* @param node the node causing the error
@@ -47,6 +53,30 @@
public JexlException(JexlNode node, String msg, Throwable cause) {
super(msg, cause);
mark = node;
+ info = node != null? node.getInfo() : null;
+ }
+
+ /**
+ * Creates a new JexlException.
+ * @param dbg the debugging information associated
+ * @param msg the error message
+ */
+ public JexlException(Info dbg, String msg) {
+ super(msg);
+ mark = null;
+ info = dbg;
+ }
+
+ /**
+ * Creates a new JexlException.
+ * @param dbg the debugging information associated
+ * @param msg the error message
+ * @param cause the exception causing the error
+ */
+ public JexlException(Info dbg, String msg, Throwable cause) {
+ super(msg, cause);
+ mark = null;
+ info = dbg;
}
/**
@@ -84,8 +114,10 @@
public String getMessage() {
Debugger dbg = new Debugger();
StringBuilder msg = new StringBuilder();
+ if (info != null) {
+ msg.append(info.debugString());
+ }
if (dbg.debug(mark)) {
- msg.append(mark.debugString());
msg.append("![");
msg.append(dbg.start());
msg.append(",");
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/Main.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/Main.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/Main.java
(original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/Main.java
Thu Nov 12 17:22:06 2009
@@ -22,7 +22,6 @@
import java.io.InputStreamReader;
import org.apache.commons.jexl.context.HashMapContext;
-import org.apache.commons.jexl.parser.ParseException;
/**
* Test application for JEXL.
@@ -61,8 +60,6 @@
Expression expression = engine.createExpression(line);
Object value = expression.evaluate(context);
System.out.println("Return value: " + value);
- } catch (ParseException e) {
- System.out.println(e.getLocalizedMessage());
} catch (JexlException e) {
System.out.println(e.getLocalizedMessage());
}
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/UnifiedJEXL.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/UnifiedJEXL.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/UnifiedJEXL.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/UnifiedJEXL.java
Thu Nov 12 17:22:06 2009
@@ -18,12 +18,11 @@
import java.util.ArrayList;
import org.apache.commons.jexl.parser.JexlNode;
-import org.apache.commons.jexl.parser.ParseException;
import org.apache.commons.jexl.parser.StringParser;
import org.apache.commons.jexl.util.introspection.Info;
/**
- * An evaluator similar to the unified EL evaluator used in JSP/JSF based on
JEXL.
+ * An evaluator similar to the Unified EL evaluator used in JSP/JSF based on
JEXL.
* It is intended to be used in configuration modules, XML based frameworks or
JSP taglibs
* and facilitate the implementation of expression evaluation.
* <p>
@@ -80,7 +79,7 @@
/** The expression cache. */
private final JexlEngine.SoftCache<String,Expression> cache;
/** The default cache size. */
- private static final int CACHE_SIZE = 512;
+ private static final int CACHE_SIZE = 256;
/**
* Creates a new instance of UnifiedJEXL with a default size cache.
* @param aJexl the JexlEngine to use.
@@ -325,17 +324,17 @@
* Prepares a sub-expression for interpretation.
* @param interpreter a JEXL interpreter
* @return a prepared expression
- * @throws ParseException (only for nested & composite)
+ * @throws JexlException (only for nested & composite)
*/
- abstract Expression prepare(Interpreter interpreter) throws
ParseException;
+ abstract Expression prepare(Interpreter interpreter);
/**
* Intreprets a sub-expression.
* @param interpreter a JEXL interpreter
* @return the result of interpretation
- * @throws ParseException (only for nested & composite)
+ * @throws JexlException (only for nested & composite)
*/
- abstract Object evaluate(Interpreter interpreter) throws
ParseException;
+ abstract Object evaluate(Interpreter interpreter);
}
@@ -404,7 +403,7 @@
/** {...@inheritdoc} */
@Override
- Expression prepare(Interpreter interpreter) throws ParseException {
+ Expression prepare(Interpreter interpreter) {
return this;
}
@@ -416,7 +415,7 @@
/** {...@inheritdoc} */
@Override
- Object evaluate(Interpreter interpreter) throws ParseException {
+ Object evaluate(Interpreter interpreter) {
return value;
}
}
@@ -475,7 +474,7 @@
/** {...@inheritdoc} */
@Override
- Expression prepare(Interpreter interpreter) throws ParseException {
+ Expression prepare(Interpreter interpreter) {
return this;
}
@@ -487,7 +486,7 @@
/** {...@inheritdoc} */
@Override
- Object evaluate(Interpreter interpreter) throws ParseException {
+ Object evaluate(Interpreter interpreter) {
return interpreter.interpret(node);
}
}
@@ -582,7 +581,7 @@
/** {...@inheritdoc} */
@Override
- public Expression prepare(Interpreter interpreter) throws
ParseException {
+ public Expression prepare(Interpreter interpreter) {
String value = interpreter.interpret(node).toString();
JexlNode dnode = toNode(value, jexl.isDebug()? node.getInfo() :
null);
return new DeferredExpression(value, dnode, this);
@@ -590,7 +589,7 @@
/** {...@inheritdoc} */
@Override
- public Object evaluate(Interpreter interpreter) throws ParseException {
+ public Object evaluate(Interpreter interpreter) {
return prepare(interpreter).evaluate(interpreter);
}
}
@@ -644,7 +643,7 @@
/** {...@inheritdoc} */
@Override
- Expression prepare(Interpreter interpreter) throws ParseException {
+ Expression prepare(Interpreter interpreter) {
// if this composite is not its own source, it is already prepared
if (source != this) {
return this;
@@ -683,7 +682,7 @@
/** {...@inheritdoc} */
@Override
- Object evaluate(Interpreter interpreter) throws ParseException {
+ Object evaluate(Interpreter interpreter) {
final int size = exprs.length;
Object value = null;
// common case: evaluate all expressions & concatenate them as a
string
@@ -709,34 +708,34 @@
* @throws UnifiedJEXL.Exception if an error occurs and the {...@link
JexlEngine} is not silent
*/
public Expression parse(String expression) {
+ Exception xuel = null;
+ Expression stmt = null;
try {
if (cache == null) {
- return parseExpression(expression);
+ stmt = parseExpression(expression);
} else {
synchronized (cache) {
- Expression stmt = cache.get(expression);
+ stmt = cache.get(expression);
if (stmt == null) {
stmt = parseExpression(expression);
cache.put(expression, stmt);
}
- return stmt;
}
}
} catch (JexlException xjexl) {
- Exception xuel = new Exception("failed to parse '" + expression +
"'", xjexl);
- if (jexl.isSilent()) {
- jexl.logger.warn(xuel.getMessage(), xuel.getCause());
- return null;
- }
- throw xuel;
- } catch (ParseException xparse) {
- Exception xuel = new Exception("failed to parse '" + expression +
"'", xparse);
- if (jexl.isSilent()) {
- jexl.logger.warn(xuel.getMessage(), xuel.getCause());
- return null;
+ xuel = new Exception("failed to parse '" + expression + "'",
xjexl);
+ } catch (Exception xuel$) {
+ xuel = xuel$;
+ } finally {
+ if (xuel != null) {
+ if (jexl.isSilent()) {
+ jexl.logger.warn(xuel.getMessage(), xuel.getCause());
+ return null;
+ }
+ throw xuel;
}
- throw xuel;
}
+ return stmt;
}
/**
@@ -759,13 +758,6 @@
return null;
}
throw xuel;
- } catch (ParseException xparse) {
- Exception xuel = createException("prepare", expr, xparse);
- if (jexl.isSilent()) {
- jexl.logger.warn(xuel.getMessage(), xuel.getCause());
- return null;
- }
- throw xuel;
}
}
@@ -789,13 +781,6 @@
return null;
}
throw xuel;
- } catch (ParseException xparse) {
- Exception xuel = createException("evaluate", expr, xparse);
- if (jexl.isSilent()) {
- jexl.logger.warn(xuel.getMessage(), xuel.getCause());
- return null;
- }
- throw xuel;
}
}
@@ -803,9 +788,9 @@
* Use the JEXL parser to create the AST for an expression.
* @param expression the expression to parse
* @return the AST
- * @throws ParseException if an error occur during parsing
+ * @throws JexlException if an error occur during parsing
*/
- private JexlNode toNode(CharSequence expression) throws ParseException {
+ private JexlNode toNode(CharSequence expression) {
return jexl.parse(expression, null);
}
@@ -814,9 +799,9 @@
* @param expression the expression to parse
* @param info debug information
* @return the AST
- * @throws ParseException if an error occur during parsing
+ * @throws JexlException if an error occur during parsing
*/
- private JexlNode toNode(CharSequence expression, Info info) throws
ParseException {
+ private JexlNode toNode(CharSequence expression, Info info) {
return jexl.parse(expression, info);
}
@@ -865,9 +850,9 @@
* Parses a unified expression.
* @param expr the string expression
* @return the expression instance
- * @throws ParseException if an error occur during parsing
+ * @throws JexlException if an error occur during parsing
*/
- private Expression parseExpression(String expr) throws ParseException {
+ private Expression parseExpression(String expr) {
final int size = expr.length();
ExpressionBuilder builder = new ExpressionBuilder(0);
StringBuilder strb = new StringBuilder(size);
@@ -990,7 +975,7 @@
}
// we should be in that state
if (state != ParseState.CONST) {
- throw new ParseException("malformed expression: " + expr);
+ throw new Exception("malformed expression: " + expr, null);
}
// if any chars were buffered, add them as a constant
if (strb.length() > 0) {
Modified:
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/scripting/JexlScriptEngine.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/scripting/JexlScriptEngine.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/scripting/JexlScriptEngine.java
(original)
+++
commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl/scripting/JexlScriptEngine.java
Thu Nov 12 17:22:06 2009
@@ -35,9 +35,6 @@
import org.apache.commons.jexl.JexlEngine;
import org.apache.commons.jexl.Script;
-// Note: this is a generated class, so won't be present until JavaCC has been
run
-import org.apache.commons.jexl.parser.ParseException;
-
/**
* Implements the Jexl ScriptEngine for JSF-223.
* <p>
@@ -151,8 +148,6 @@
}
};
return script.execute(ctxt);
- } catch (ParseException e) {
- throw new ScriptException(e.toString());
} catch (Exception e) {
throw new ScriptException(e.toString());
}
Modified:
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/AssignTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/AssignTest.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/AssignTest.java
(original)
+++
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/AssignTest.java
Thu Nov 12 17:22:06 2009
@@ -130,11 +130,14 @@
}
public void testMore() throws Exception {
+ JexlContext jc = JexlHelper.createContext();
+ jc.getVars().put("quuxClass", Quux.class);
+ Expression create = ENGINE.createExpression("quux = new(quuxClass,
'xuuq', 100)");
Expression assign = ENGINE.createExpression("quux.froboz.value = 10");
Expression check = ENGINE.createExpression("quux[\"froboz\"].value");
- JexlContext jc = JexlHelper.createContext();
- Quux quux = new Quux("xuuq", 100);
- jc.getVars().put("quux", quux);
+
+ Quux quux = (Quux) create.evaluate(jc);
+ assertNotNull("quux is null", quux);
Object o = assign.evaluate(jc);
assertEquals("Result is not 10", new Integer(10), o);
o = check.evaluate(jc);
@@ -142,7 +145,7 @@
}
public void testUtil() throws Exception {
- Quux quux = new Quux("xuuq", 100);
+ Quux quux = ENGINE.newInstance(Quux.class, "xuuq", 100);
ENGINE.setProperty(quux, "froboz.value", Integer.valueOf(100));
Object o = ENGINE.getProperty(quux, "froboz.value");
assertEquals("Result is not 100", new Integer(100), o);
Modified:
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/IssuesTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/IssuesTest.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/IssuesTest.java
(original)
+++
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/IssuesTest.java
Thu Nov 12 17:22:06 2009
@@ -16,7 +16,6 @@
*/
package org.apache.commons.jexl;
-import org.apache.commons.jexl.parser.ParseException;
import java.util.Map;
/**
@@ -103,7 +102,7 @@
}
// JEXL-42: NullPointerException evaluating an expression
- // fixed in JexlArithmetic by allowing add to deal with string, null
+ // fixed in JexlArithmetic by allowing add operator to deal with string,
null
public void test42() throws Exception {
JexlEngine jexl = new JexlEngine();
UnifiedJEXL uel = new UnifiedJEXL(jexl);
@@ -280,7 +279,7 @@
try {
jexl.createScript(fexprs[f]);
fail(fexprs[f] + ": Should have failed in parse");
- } catch (ParseException xany) {
+ } catch (JexlException xany) {
// expected to fail in parse
}
}
Modified:
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/JexlTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/JexlTest.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/JexlTest.java
(original)
+++
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/JexlTest.java
Thu Nov 12 17:22:06 2009
@@ -679,7 +679,7 @@
assertExpression(JexlHelper.createContext(), "empty()", null);
fail("Bad expression didn't throw ParseException");
}
- catch (ParseException pe)
+ catch (JexlException pe)
{
// expected behaviour
}
Modified:
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/JexlTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/JexlTestCase.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/JexlTestCase.java
(original)
+++
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/JexlTestCase.java
Thu Nov 12 17:22:06 2009
@@ -26,7 +26,6 @@
import org.apache.commons.jexl.parser.JexlNode;
import org.apache.commons.jexl.parser.ASTJexlScript;
-import org.apache.commons.jexl.parser.ParseException;
import junit.framework.TestCase;
@@ -79,28 +78,20 @@
// recreate expr string from AST
dbg.debug(node);
String expressiondbg = dbg.data();
- try {
- // recreate expr from string
- Expression exprdbg = jdbg.createExpression(expressiondbg);
- // make arg cause become the root cause
- JexlNode root = ((ExpressionImpl) exprdbg).script;
- while (root.jjtGetParent() != null) {
- root = root.jjtGetParent();
- }
- // test equality
- String reason = JexlTestCase.checkEquals(root, node);
- if (reason != null) {
- throw new RuntimeException("debugger equal failed: "
- + expressiondbg
- +" /**** " +reason+" **** */ "
- + entry.getKey());
- }
- }
- catch(ParseException xparse) {
- throw new RuntimeException("debugger parse failed: "
- + expressiondbg
- +" /**** != **** */ "
- + entry.getKey());
+ // recreate expr from string
+ Expression exprdbg = jdbg.createExpression(expressiondbg);
+ // make arg cause become the root cause
+ JexlNode root = ((ExpressionImpl) exprdbg).script;
+ while (root.jjtGetParent() != null) {
+ root = root.jjtGetParent();
+ }
+ // test equality
+ String reason = JexlTestCase.checkEquals(root, node);
+ if (reason != null) {
+ throw new RuntimeException("debugger equal failed: "
+ + expressiondbg
+ +" /**** " +reason+" **** */ "
+ + entry.getKey());
}
}
}
Modified:
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/MethodTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/MethodTest.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/MethodTest.java
(original)
+++
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/MethodTest.java
Thu Nov 12 17:22:06 2009
@@ -120,25 +120,36 @@
asserter.assertExpression("Boolean.valueOf('true')", Boolean.TRUE);
}
+ public static class MyMath {
+ public double cos(double x) {
+ return Math.cos(x);
+ }
+ }
+
public void testTopLevelCall() throws Exception {
java.util.Map<String, Object> funcs = new java.util.HashMap<String,
Object>();
funcs.put(null, new Functor());
+ funcs.put("math", new MyMath());
JEXL.setFunctions(funcs);
- Expression e = JEXL.createExpression("ten()");
JexlContext jc = JexlHelper.createContext();
+
+ Expression e = JEXL.createExpression("ten()");
Object o = e.evaluate(jc);
assertEquals("Result is not 10", new Integer(10), o);
e = JEXL.createExpression("plus10(10)");
- jc = JexlHelper.createContext();
o = e.evaluate(jc);
assertEquals("Result is not 20", new Integer(20), o);
e = JEXL.createExpression("plus10(ten())");
- jc = JexlHelper.createContext();
o = e.evaluate(jc);
assertEquals("Result is not 20", new Integer(20), o);
+
+ jc.getVars().put("pi", Math.PI);
+ e = JEXL.createExpression("math:cos(pi)");
+ o = e.evaluate(jc);
+ assertEquals(Double.valueOf(-1),o);
}
public void testNamespaceCall() throws Exception {
Modified:
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/ParseFailuresTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/ParseFailuresTest.java?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
---
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/ParseFailuresTest.java
(original)
+++
commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl/ParseFailuresTest.java
Thu Nov 12 17:22:06 2009
@@ -16,12 +16,10 @@
*/
package org.apache.commons.jexl;
-import org.apache.commons.jexl.parser.ParseException;
-
/**
* Tests for malformed expressions and scripts.
* ({...@link JexlEngine#createExpression(String)} and {...@link
JexlEngine#createScript(String)} should throw
- * {...@link ParseException}s).
+ * {...@link JexlException}s).
*
* @since 1.1
*/
@@ -34,65 +32,66 @@
*/
public ParseFailuresTest(String testName) {
super(testName);
+ JEXL.setSilent(false);
}
public void testMalformedExpression1() throws Exception {
- // this will throw a ParseException
+ // this will throw a JexlException
String badExpression = "eq";
try {
JEXL.createExpression(badExpression);
fail("Parsing \"" + badExpression
- + "\" should result in a ParseException");
- } catch (ParseException pe) {
+ + "\" should result in a JexlException");
+ } catch (JexlException pe) {
// expected
}
}
public void testMalformedExpression2() throws Exception {
- // this will throw a TokenMgrErr, which we rethrow as a ParseException
+ // this will throw a TokenMgrErr, which we rethrow as a JexlException
String badExpression = "?";
try {
JEXL.createExpression(badExpression);
fail("Parsing \"" + badExpression
- + "\" should result in a ParseException");
- } catch (ParseException pe) {
+ + "\" should result in a JexlException");
+ } catch (JexlException pe) {
// expected
}
}
public void testMalformedScript1() throws Exception {
- // this will throw a TokenMgrErr, which we rethrow as a ParseException
+ // this will throw a TokenMgrErr, which we rethrow as a JexlException
String badScript = "eq";
try {
JEXL.createScript(badScript);
fail("Parsing \"" + badScript
- + "\" should result in a ParseException");
- } catch (ParseException pe) {
+ + "\" should result in a JexlException");
+ } catch (JexlException pe) {
// expected
}
}
public void testMalformedScript2() throws Exception {
- // this will throw a TokenMgrErr, which we rethrow as a ParseException
+ // this will throw a TokenMgrErr, which we rethrow as a JexlException
String badScript = "?";
try {
JEXL.createScript(badScript);
fail("Parsing \"" + badScript
- + "\" should result in a ParseException");
- } catch (ParseException pe) {
+ + "\" should result in a JexlException");
+ } catch (JexlException pe) {
// expected
}
}
public void testMalformedScript3() throws Exception {
- // this will throw a TokenMgrErr, which we rethrow as a ParseException
+ // this will throw a TokenMgrErr, which we rethrow as a JexlException
String badScript = "foo=1;bar=2;a?b:c:d;";
try {
JEXL.createScript(badScript);
fail("Parsing \"" + badScript
- + "\" should result in a ParseException");
- } catch (ParseException pe) {
+ + "\" should result in a JexlException");
+ } catch (JexlException pe) {
// expected
}
}
Modified: commons/proper/jexl/trunk/xdocs/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/jexl/trunk/xdocs/changes.xml?rev=835458&r1=835457&r2=835458&view=diff
==============================================================================
--- commons/proper/jexl/trunk/xdocs/changes.xml (original)
+++ commons/proper/jexl/trunk/xdocs/changes.xml Thu Nov 12 17:22:06 2009
@@ -26,7 +26,7 @@
</properties>
<body>
<release version="2.0" date="unreleased">
- <action dev="henrib" type="add" issue="JEXL-27" due-to="Weikuo
Liaw">Bean-ish & ant-ish like assignment</action>
+ <action dev="henrib" type="add" issue="JEXL-27" due-to="Weikuo
Liaw">Bean-ish & ant-ish like assignment</action>
<action dev="henrib" type="add" issue="JEXL-19" due-to="Jesse
Glick">Ternary operator support</action>
<action dev="henrib" type="add" issue="JEXL-41" due-to="Alejandro
Torras">Support for ${...} and #{...} expressions</action>
<action dev="henrib" type="add" issue="JEXL-15" due-to="Paul
Libbrecht">User definable functions</action>