1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
/* Generated By:JJTree: Do not edit this line. AstFunction.java */
package org.apache.el.parser;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import jakarta.el.ELClass;
import jakarta.el.ELException;
import jakarta.el.FunctionMapper;
import jakarta.el.LambdaExpression;
import jakarta.el.ValueExpression;
import jakarta.el.VariableMapper;
import org.apache.el.lang.ELSupport;
import org.apache.el.lang.EvaluationContext;
import org.apache.el.util.MessageFactory;
/**
* @author Jacob Hookom [jacob@hookom.net]
*/
public final class AstFunction extends SimpleNode {
private String localName = "";
private String prefix = "";
public AstFunction(int id) {
super(id);
}
public String getLocalName() {
return localName;
}
public String getOutputName() {
if (this.prefix == null) {
return this.localName;
} else {
return this.prefix + ":" + this.localName;
}
}
public String getPrefix() {
return prefix;
}
@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();
}
@Override
public Object getValue(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 && this.prefix.isEmpty()) {
// TODO: Do we need to think about precedence of the various ways
// a lambda expression may be obtained from something that
// the parser thinks is a function?
Object obj = null;
if (ctx.isLambdaArgument(this.localName)) {
obj = ctx.getLambdaArgument(this.localName);
}
if (obj == null) {
VariableMapper varMapper = ctx.getVariableMapper();
if (varMapper != null) {
obj = varMapper.resolveVariable(this.localName);
if (obj != null) {
// See if this returns a LambdaExpression
obj = ((ValueExpression) obj).getValue(ctx);
}
}
}
if (obj == null) {
obj = ctx.getELResolver().getValue(ctx, null, this.localName);
}
if (obj instanceof LambdaExpression) {
// Build arguments
int i = 0;
while (obj instanceof LambdaExpression && i < jjtGetNumChildren()) {
Node args = jjtGetChild(i);
obj = ((LambdaExpression) obj).invoke(((AstMethodParameters) args).getParameters(ctx));
i++;
}
if (i < jjtGetNumChildren()) {
// Haven't consumed all the sets of parameters therefore
// there were too many sets of parameters
throw new ELException(MessageFactory.get("error.lambda.tooManyMethodParameterSets"));
}
return obj;
}
// Call to a constructor or a static method
obj = ctx.getImportHandler().resolveClass(this.localName);
if (obj != null) {
return ctx.getELResolver().invoke(ctx, new ELClass((Class<?>) obj), "<init>", null,
((AstMethodParameters) this.children[0]).getParameters(ctx));
}
obj = ctx.getImportHandler().resolveStatic(this.localName);
if (obj != null) {
return ctx.getELResolver().invoke(ctx, new ELClass((Class<?>) obj), this.localName, null,
((AstMethodParameters) this.children[0]).getParameters(ctx));
}
}
if (m == null) {
throw new ELException(MessageFactory.get("error.fnMapper.method", this.getOutputName()));
}
// Not a lambda expression so must be a function. Check there is just a
// single set of method parameters
if (this.jjtGetNumChildren() != 1) {
throw new ELException(MessageFactory.get("error.function.tooManyMethodParameterSets", getOutputName()));
}
Node parameters = jjtGetChild(0);
Class<?>[] paramTypes = m.getParameterTypes();
Object[] params = null;
int inputParameterCount = parameters.jjtGetNumChildren();
int methodParameterCount = paramTypes.length;
if (inputParameterCount == 0 && methodParameterCount == 1 && m.isVarArgs()) {
params = new Object[] { null };
} else if (inputParameterCount > 0) {
params = new Object[methodParameterCount];
try {
for (int i = 0; i < methodParameterCount; i++) {
if (m.isVarArgs() && i == methodParameterCount - 1) {
if (inputParameterCount < methodParameterCount) {
params[i] = new Object[] { null };
} else if (inputParameterCount == methodParameterCount &&
isArray(parameters.jjtGetChild(i).getValue(ctx))) {
params[i] = parameters.jjtGetChild(i).getValue(ctx);
} else {
Object[] varargs = new Object[inputParameterCount - methodParameterCount + 1];
Class<?> target = paramTypes[i].getComponentType();
for (int j = i; j < inputParameterCount; j++) {
varargs[j - i] = parameters.jjtGetChild(j).getValue(ctx);
varargs[j - i] = ELSupport.coerceToType(ctx, varargs[j - i], target);
}
params[i] = varargs;
}
} else {
params[i] = parameters.jjtGetChild(i).getValue(ctx);
}
params[i] = ELSupport.coerceToType(ctx, params[i], paramTypes[i]);
}
} catch (ELException ele) {
throw new ELException(MessageFactory.get("error.function", this.getOutputName()), ele);
}
}
Object result;
try {
result = m.invoke(null, params);
} catch (IllegalAccessException iae) {
throw new ELException(MessageFactory.get("error.function", this.getOutputName()), iae);
} catch (InvocationTargetException ite) {
Throwable cause = ite.getCause();
if (cause instanceof VirtualMachineError) {
throw (VirtualMachineError) cause;
}
throw new ELException(MessageFactory.get("error.function", this.getOutputName()), cause);
}
return result;
}
private boolean isArray(Object obj) {
if (obj == null) {
return false;
}
return obj.getClass().isArray();
}
public void setLocalName(String localName) {
this.localName = localName;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
@Override
public String toString() {
return ELParserTreeConstants.jjtNodeName[id] + "[" + this.getOutputName() + "]";
}
}
|