de.odysseus.el.TreeMethodExpression Maven / Gradle / Ivy
/*
* Copyright 2006-2009 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.el;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.el.ELContext;
import javax.el.ELException;
import javax.el.FunctionMapper;
import javax.el.MethodInfo;
import javax.el.VariableMapper;
import de.odysseus.el.misc.LocalMessages;
import de.odysseus.el.misc.TypeConverter;
import de.odysseus.el.tree.Bindings;
import de.odysseus.el.tree.ExpressionNode;
import de.odysseus.el.tree.Tree;
import de.odysseus.el.tree.TreeBuilder;
import de.odysseus.el.tree.TreeStore;
import de.odysseus.el.tree.NodePrinter;
/**
* A method expression is ready to be evaluated (by calling either
* {@link #invoke(ELContext, Object[])} or {@link #getMethodInfo(ELContext)}).
*
* Instances of this class are usually created using an {@link ExpressionFactoryImpl}.
*
* @author Christoph Beck
*/
public final class TreeMethodExpression extends javax.el.MethodExpression {
private static final long serialVersionUID = 1L;
private final TreeBuilder builder;
private final Bindings bindings;
private final String expr;
private final Class> type;
private final Class>[] types;
private final boolean deferred;
private transient ExpressionNode node;
private String structure;
/**
* Create a new method expression.
* The expression must be an lvalue expression or literal text.
* The expected return type may be null
, meaning "don't care".
* If it is an lvalue expression, the parameter types must not be null
.
* If it is literal text, the expected return type must not be void
.
* @param store used to get the parse tree from.
* @param functions the function mapper used to bind functions
* @param variables the variable mapper used to bind variables
* @param expr the expression string
* @param returnType the expected return type (may be null
)
* @param paramTypes the expected parameter types (must not be null
for lvalues)
*/
public TreeMethodExpression(TreeStore store, FunctionMapper functions, VariableMapper variables, TypeConverter converter, String expr, Class> returnType, Class>[] paramTypes) {
super();
Tree tree = store.get(expr);
this.builder = store.getBuilder();
this.bindings = tree.bind(functions, variables, converter);
this.expr = expr;
this.type = returnType;
this.types = paramTypes;
this.node = tree.getRoot();
this.deferred = tree.isDeferred();
if (node.isLiteralText()) {
if (returnType == void.class || returnType == Void.class) {
throw new ELException(LocalMessages.get("error.method.literal.void", expr));
}
} else if (!node.isMethodInvocation()) {
if (!node.isLeftValue()) {
throw new ELException(LocalMessages.get("error.method.invalid", expr));
}
if (paramTypes == null) {
throw new NullPointerException(LocalMessages.get("error.method.notypes")); // EL specification requires NPE
}
}
}
private String getStructuralId() {
if (structure == null) {
structure = node.getStructuralId(bindings);
}
return structure;
}
/**
* Evaluates the expression and answers information about the method
* @param context used to resolve properties (base.property
and base[property]
)
* @return method information or null
for literal expressions
* @throws ELException if evaluation fails (e.g. suitable method not found)
*/
@Override
public MethodInfo getMethodInfo(ELContext context) throws ELException {
return node.getMethodInfo(bindings, context, type, types);
}
@Override
public String getExpressionString() {
return expr;
}
/**
* Evaluates the expression and invokes the method.
* @param context used to resolve properties (base.property
and base[property]
)
* @param paramValues
* @return method result or null
if this is a literal text expression
* @throws ELException if evaluation fails (e.g. suitable method not found)
*/
@Override
public Object invoke(ELContext context, Object[] paramValues) throws ELException {
return node.invoke(bindings, context, type, types, paramValues);
}
/**
* @return true
if this is a literal text expression
*/
@Override
public boolean isLiteralText() {
return node.isLiteralText();
}
/**
* @return true
if this is a method invocation expression
*/
@Override
public boolean isParmetersProvided() {
return node.isMethodInvocation();
}
/**
* Answer true
if this is a deferred expression (starting with #{
)
*/
public boolean isDeferred() {
return deferred;
}
/**
* Expressions are compared using the concept of a structural id:
* variable and function names are anonymized such that two expressions with
* same tree structure will also have the same structural id and vice versa.
* Two method expressions are equal if
*
* - their builders are equal
* - their structural id's are equal
* - their bindings are equal
* - their expected types match
* - their parameter types are equal
*
*/
@Override
public boolean equals(Object obj) {
if (obj != null && obj.getClass() == getClass()) {
TreeMethodExpression other = (TreeMethodExpression)obj;
if (!builder.equals(other.builder)) {
return false;
}
if (type != other.type) {
return false;
}
if (!Arrays.equals(types, other.types)) {
return false;
}
return getStructuralId().equals(other.getStructuralId()) && bindings.equals(other.bindings);
}
return false;
}
@Override
public int hashCode() {
return getStructuralId().hashCode();
}
@Override
public String toString() {
return "TreeMethodExpression(" + expr + ")";
}
/**
* Print the parse tree.
* @param writer
*/
public void dump(PrintWriter writer) {
NodePrinter.dump(writer, node);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
try {
node = builder.build(expr).getRoot();
} catch (ELException e) {
throw new IOException(e.getMessage());
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy