use of jakarta.el.ELException in project tomcat by apache.
the class ExpressionBuilder method visit.
/*
* (non-Javadoc)
*
* @see com.sun.el.parser.NodeVisitor#visit(com.sun.el.parser.Node)
*/
@Override
public void visit(Node node) throws ELException {
if (node instanceof AstFunction) {
AstFunction funcNode = (AstFunction) node;
Method m = null;
if (this.fnMapper != null) {
m = fnMapper.resolveFunction(funcNode.getPrefix(), funcNode.getLocalName());
}
// need to treat it as a variable rather than a function.
if (m == null && this.varMapper != null && funcNode.getPrefix().length() == 0) {
this.varMapper.resolveVariable(funcNode.getLocalName());
return;
}
if (this.fnMapper == null) {
throw new ELException(MessageFactory.get("error.fnMapper.null"));
}
if (m == null) {
throw new ELException(MessageFactory.get("error.fnMapper.method", funcNode.getOutputName()));
}
int methodParameterCount = m.getParameterTypes().length;
// AstFunction->MethodParameters->Parameters()
int inputParameterCount = node.jjtGetChild(0).jjtGetNumChildren();
if (m.isVarArgs() && inputParameterCount < methodParameterCount - 1 || !m.isVarArgs() && inputParameterCount != methodParameterCount) {
throw new ELException(MessageFactory.get("error.fnMapper.paramcount", funcNode.getOutputName(), "" + methodParameterCount, "" + node.jjtGetChild(0).jjtGetNumChildren()));
}
} else if (node instanceof AstIdentifier && this.varMapper != null) {
String variable = node.getImage();
// simply capture it
this.varMapper.resolveVariable(variable);
}
}
use of jakarta.el.ELException in project tomcat by apache.
the class AstIdentifier method getValue.
@Override
public Object getValue(EvaluationContext ctx) throws ELException {
// Lambda parameters
if (ctx.isLambdaArgument(this.image)) {
return ctx.getLambdaArgument(this.image);
}
// Variable mapper
VariableMapper varMapper = ctx.getVariableMapper();
if (varMapper != null) {
ValueExpression expr = varMapper.resolveVariable(this.image);
if (expr != null) {
return expr.getValue(ctx.getELContext());
}
}
// EL Resolvers
ctx.setPropertyResolved(false);
Object result;
/* Putting the Boolean into the ELContext is part of a performance
* optimisation for ScopedAttributeELResolver. When looking up "foo",
* the resolver can't differentiate between ${ foo } and ${ foo.bar }.
* This is important because the expensive class lookup only needs to
* be performed in the later case. This flag tells the resolver if the
* lookup can be skipped.
*/
if (parent instanceof AstValue) {
ctx.putContext(this.getClass(), Boolean.FALSE);
} else {
ctx.putContext(this.getClass(), Boolean.TRUE);
}
try {
result = ctx.getELResolver().getValue(ctx, null, this.image);
} finally {
// Always reset the flag to false so the optimisation is not applied
// inappropriately
ctx.putContext(this.getClass(), Boolean.FALSE);
}
if (ctx.isPropertyResolved()) {
return result;
}
// Import
result = ctx.getImportHandler().resolveClass(this.image);
if (result != null) {
return new ELClass((Class<?>) result);
}
result = ctx.getImportHandler().resolveStatic(this.image);
if (result != null) {
try {
return ((Class<?>) result).getField(this.image).get(null);
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
throw new ELException(e);
}
}
throw new PropertyNotFoundException(MessageFactory.get("error.resolver.unhandled.null", this.image));
}
use of jakarta.el.ELException in project tomcat by apache.
the class AstFunction method getType.
@Override
public Class<?> getType(EvaluationContext ctx) throws ELException {
FunctionMapper fnMapper = ctx.getFunctionMapper();
// quickly validate again for this request
if (fnMapper == null) {
throw new ELException(MessageFactory.get("error.fnMapper.null"));
}
Method m = fnMapper.resolveFunction(this.prefix, this.localName);
if (m == null) {
throw new ELException(MessageFactory.get("error.fnMapper.method", this.getOutputName()));
}
return m.getReturnType();
}
use of jakarta.el.ELException in project tomcat by apache.
the class AstLambdaExpression method getValue.
@Override
public Object getValue(EvaluationContext ctx) throws ELException {
// Correct evaluation requires knowledge of the whole set of nested
// expressions, not just the current expression
NestedState state = getNestedState();
// Check that there are not more sets of parameters than there are
// nested expressions.
int methodParameterSetCount = jjtGetNumChildren() - 2;
if (methodParameterSetCount > state.getNestingCount()) {
throw new ELException(MessageFactory.get("error.lambda.tooManyMethodParameterSets"));
}
// First child is always parameters even if there aren't any
AstLambdaParameters formalParametersNode = (AstLambdaParameters) children[0];
Node[] formalParamNodes = formalParametersNode.children;
// Second child is a value expression
ValueExpressionImpl ve = new ValueExpressionImpl("", children[1], ctx.getFunctionMapper(), ctx.getVariableMapper(), null);
// Build a LambdaExpression
List<String> formalParameters = new ArrayList<>();
if (formalParamNodes != null) {
for (Node formalParamNode : formalParamNodes) {
formalParameters.add(formalParamNode.getImage());
}
}
LambdaExpression le = new LambdaExpression(formalParameters, ve);
le.setELContext(ctx);
if (jjtGetNumChildren() == 2) {
// in the nesting declare parameters
if (state.getHasFormalParameters()) {
return le;
} else {
return le.invoke(ctx, (Object[]) null);
}
}
/*
* This is a (possibly nested) lambda expression with one or more sets
* of parameters provided.
*
* If there are more nested expressions than sets of parameters this may
* return a LambdaExpression.
*
* If there are more sets of parameters than nested expressions an
* ELException will have been thrown by the check at the start of this
* method.
*/
// Always have to invoke the outer-most expression
int methodParameterIndex = 2;
Object result = le.invoke(((AstMethodParameters) children[methodParameterIndex]).getParameters(ctx));
methodParameterIndex++;
while (result instanceof LambdaExpression && methodParameterIndex < jjtGetNumChildren()) {
result = ((LambdaExpression) result).invoke(((AstMethodParameters) children[methodParameterIndex]).getParameters(ctx));
methodParameterIndex++;
}
return result;
}
use of jakarta.el.ELException in project tomcat 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);
}
Aggregations