use of javax.el.ELException in project tomcat70 by apache.
the class ELSupport method compare.
/**
* Compare two objects, after coercing to the same type if appropriate.
*
* If the objects are identical, or they are equal according to
* {@link #equals(Object, Object)} then return 0.
*
* If either object is a BigDecimal, then coerce both to BigDecimal first.
* Similarly for Double(Float), BigInteger, and Long(Integer, Char, Short, Byte).
*
* Otherwise, check that the first object is an instance of Comparable, and compare
* against the second object. If that is null, return 1, otherwise
* return the result of comparing against the second object.
*
* Similarly, if the second object is Comparable, if the first is null, return -1,
* else return the result of comparing against the first object.
*
* A null object is considered as:
* <ul>
* <li>ZERO when compared with Numbers</li>
* <li>the empty string for String compares</li>
* <li>Otherwise null is considered to be lower than anything else.</li>
* </ul>
*
* @param obj0 first object
* @param obj1 second object
* @return -1, 0, or 1 if this object is less than, equal to, or greater than val.
* @throws ELException if neither object is Comparable
* @throws ClassCastException if the objects are not mutually comparable
*/
public static final int compare(final Object obj0, final Object obj1) throws ELException {
if (obj0 == obj1 || equals(obj0, obj1)) {
return 0;
}
if (isBigDecimalOp(obj0, obj1)) {
BigDecimal bd0 = (BigDecimal) coerceToNumber(obj0, BigDecimal.class);
BigDecimal bd1 = (BigDecimal) coerceToNumber(obj1, BigDecimal.class);
return bd0.compareTo(bd1);
}
if (isDoubleOp(obj0, obj1)) {
Double d0 = (Double) coerceToNumber(obj0, Double.class);
Double d1 = (Double) coerceToNumber(obj1, Double.class);
return d0.compareTo(d1);
}
if (isBigIntegerOp(obj0, obj1)) {
BigInteger bi0 = (BigInteger) coerceToNumber(obj0, BigInteger.class);
BigInteger bi1 = (BigInteger) coerceToNumber(obj1, BigInteger.class);
return bi0.compareTo(bi1);
}
if (isLongOp(obj0, obj1)) {
Long l0 = (Long) coerceToNumber(obj0, Long.class);
Long l1 = (Long) coerceToNumber(obj1, Long.class);
return l0.compareTo(l1);
}
if (obj0 instanceof String || obj1 instanceof String) {
return coerceToString(obj0).compareTo(coerceToString(obj1));
}
if (obj0 instanceof Comparable<?>) {
// checked above
@SuppressWarnings("unchecked") final Comparable<Object> comparable = (Comparable<Object>) obj0;
return (obj1 != null) ? comparable.compareTo(obj1) : 1;
}
if (obj1 instanceof Comparable<?>) {
// checked above
@SuppressWarnings("unchecked") final Comparable<Object> comparable = (Comparable<Object>) obj1;
return (obj0 != null) ? -comparable.compareTo(obj0) : -1;
}
throw new ELException(MessageFactory.get("error.compare", obj0, obj1));
}
use of javax.el.ELException in project tomcat70 by apache.
the class ExpressionBuilder method createNodeInternal.
private static final Node createNodeInternal(String expr) throws ELException {
if (expr == null) {
throw new ELException(MessageFactory.get("error.null"));
}
Node n = cache.get(expr);
if (n == null) {
try {
n = (new ELParser(new StringReader(expr))).CompositeExpression();
// validate composite expression
int numChildren = n.jjtGetNumChildren();
if (numChildren == 1) {
n = n.jjtGetChild(0);
} else {
Class<?> type = null;
Node child = null;
for (int i = 0; i < numChildren; i++) {
child = n.jjtGetChild(i);
if (child instanceof AstLiteralExpression)
continue;
if (type == null)
type = child.getClass();
else {
if (!type.equals(child.getClass())) {
throw new ELException(MessageFactory.get("error.mixed", expr));
}
}
}
}
if (n instanceof AstDeferredExpression || n instanceof AstDynamicExpression) {
n = n.jjtGetChild(0);
}
cache.put(expr, n);
} catch (Exception e) {
throw new ELException(MessageFactory.get("error.parseFail", expr), e);
}
}
return n;
}
use of javax.el.ELException in project tomcat70 by apache.
the class AstValue method invoke.
@Override
public // Interface el.parser.Node uses a raw type (and is auto-generated)
Object invoke(EvaluationContext ctx, @SuppressWarnings("rawtypes") Class[] paramTypes, Object[] paramValues) throws ELException {
Target t = getTarget(ctx);
Method m = null;
Object[] values = null;
Class<?>[] types = null;
if (isParametersProvided()) {
values = ((AstMethodParameters) this.jjtGetChild(this.jjtGetNumChildren() - 1)).getParameters(ctx);
types = getTypesFromValues(values);
} else {
values = paramValues;
types = paramTypes;
}
m = ReflectionUtil.getMethod(t.base, t.property, types, values);
// Handle varArgs and any coercion required
values = convertArgs(values, m);
Object result = null;
try {
result = m.invoke(t.base, values);
} catch (IllegalAccessException iae) {
throw new ELException(iae);
} catch (IllegalArgumentException iae) {
throw new ELException(iae);
} catch (InvocationTargetException ite) {
Throwable cause = ite.getCause();
if (cause instanceof ThreadDeath) {
throw (ThreadDeath) cause;
}
if (cause instanceof VirtualMachineError) {
throw (VirtualMachineError) cause;
}
throw new ELException(cause);
}
return result;
}
use of javax.el.ELException in project tomcat70 by apache.
the class TestELEvaluation method testMixedTypes.
/**
* Test mixing ${...} and #{...} in the same expression.
*/
@Test
public void testMixedTypes() {
// Mixing types should throw an error
Exception e = null;
try {
evaluateExpression("${1+1}#{1+1}");
} catch (ELException el) {
e = el;
}
Assert.assertNotNull(e);
}
use of javax.el.ELException in project tomcat70 by apache.
the class TestELParser method testJavaKeyWordSuffix.
@Test
public void testJavaKeyWordSuffix() {
ExpressionFactory factory = ExpressionFactory.newInstance();
ELContext context = new ELContextImpl();
TesterBeanA beanA = new TesterBeanA();
beanA.setInt("five");
ValueExpression var = factory.createValueExpression(beanA, TesterBeanA.class);
context.getVariableMapper().setVariable("beanA", var);
// Should fail
Exception e = null;
try {
factory.createValueExpression(context, "${beanA.int}", String.class);
} catch (ELException ele) {
e = ele;
}
Assert.assertNotNull(e);
}
Aggregations