Skip to content

Package: AstFunction

AstFunction

nameinstructionbranchcomplexitylinemethod
AstFunction(int)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
findValue(EvaluationContext, String)
M: 41 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
getLocalName()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getOutputName()
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getPrefix()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getType(EvaluationContext)
M: 31 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getValue(EvaluationContext)
M: 208 C: 0
0%
M: 26 C: 0
0%
M: 14 C: 0
0%
M: 43 C: 0
0%
M: 1 C: 0
0%
setLocalName(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setPrefix(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
toString()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package com.sun.el.parser;
18:
19: import java.lang.reflect.InvocationTargetException;
20: import java.lang.reflect.Method;
21:
22: import jakarta.el.ELClass;
23: import jakarta.el.ELException;
24: import jakarta.el.FunctionMapper;
25: import jakarta.el.LambdaExpression;
26: import jakarta.el.ValueExpression;
27: import jakarta.el.VariableMapper;
28:
29: import com.sun.el.lang.EvaluationContext;
30: import com.sun.el.util.MessageFactory;
31:
32: /**
33: * @author Jacob Hookom [jacob@hookom.net]
34: * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: kchung $
35: */
36: public final class AstFunction extends SimpleNode {
37:
38: protected String localName = "";
39:
40: protected String prefix = "";
41:
42: public AstFunction(int id) {
43: super(id);
44: }
45:
46: public String getLocalName() {
47: return localName;
48: }
49:
50: public String getOutputName() {
51:• if (this.prefix.length() == 0) {
52: return this.localName;
53: } else {
54: return this.prefix + ":" + this.localName;
55: }
56: }
57:
58: public String getPrefix() {
59: return prefix;
60: }
61:
62: @Override
63: public Class getType(EvaluationContext ctx) throws ELException {
64:
65: FunctionMapper fnMapper = ctx.getFunctionMapper();
66:
67: // quickly validate again for this request
68:• if (fnMapper == null) {
69: throw new ELException(MessageFactory.get("error.fnMapper.null"));
70: }
71: Method m = fnMapper.resolveFunction(this.prefix, this.localName);
72:• if (m == null) {
73: throw new ELException(MessageFactory.get("error.fnMapper.method", this.getOutputName()));
74: }
75: return m.getReturnType();
76: }
77:
78: /*
79: * Find the object associated with the given name. Return null if the there is no such object.
80: */
81: private Object findValue(EvaluationContext ctx, String name) {
82: // First check if this is a Lambda argument
83:• if (ctx.isLambdaArgument(name)) {
84: return ctx.getLambdaArgument(name);
85: }
86:
87: // Next check if this an Jakarta Expression variable
88: VariableMapper varMapper = ctx.getVariableMapper();
89:• if (varMapper != null) {
90: ValueExpression expr = varMapper.resolveVariable(name);
91:• if (expr != null) {
92: return expr.getValue(ctx.getELContext());
93: }
94: }
95: // Check if this is resolvable by an ELResolver
96: ctx.setPropertyResolved(false);
97: Object ret = ctx.getELResolver().getValue(ctx, null, name);
98:• if (ctx.isPropertyResolved()) {
99: return ret;
100: }
101: return null;
102: }
103:
104: @Override
105: public Object getValue(EvaluationContext ctx) throws ELException {
106:
107: // Check to see if a function is a bean that is a Lambdaexpression.
108: // If so, invoke it. Also allow for the case that a Lambda expression
109: // can return another Lambda expression.
110:• if (prefix.length() == 0) {
111: Object val = findValue(ctx, this.localName);
112: // Check the case of repeated lambda invocation, such as f()()()
113:
114:• if ((val != null) && (val instanceof LambdaExpression)) {
115:• for (int i = 0; i < this.children.length; i++) {
116: Object[] params = ((AstMethodArguments) this.children[i]).getParameters(ctx);
117:• if (!(val instanceof LambdaExpression)) {
118: throw new ELException(MessageFactory.get("error.function.syntax", getOutputName()));
119: }
120: val = ((LambdaExpression) val).invoke(ctx, params);
121: }
122: return val;
123: }
124: }
125:
126: FunctionMapper fnMapper = ctx.getFunctionMapper();
127:
128: Method m = null;
129:• if (fnMapper != null) {
130: m = fnMapper.resolveFunction(this.prefix, this.localName);
131: }
132:• if (m == null) {
133:• if (this.prefix.length() == 0 && ctx.getImportHandler() != null) {
134: Class<?> c = null;
135: ;
136: // Check if this is a constructor call for an imported class
137: c = ctx.getImportHandler().resolveClass(this.localName);
138: String methodName = null;
139:• if (c != null) {
140: methodName = "<init>";
141: } else {
142: // Check if this is a imported static method
143: c = ctx.getImportHandler().resolveStatic(this.localName);
144: methodName = this.localName;
145: ;
146: }
147:• if (c != null) {
148: // Use StaticFieldELResolver to invoke the constructor or the
149: // static method.
150: Object[] params = ((AstMethodArguments) this.children[0]).getParameters(ctx);
151: return ctx.getELResolver().invoke(ctx, new ELClass(c), methodName, null, params);
152: }
153: }
154: // quickly validate for this request
155:• if (fnMapper == null) {
156: throw new ELException(MessageFactory.get("error.fnMapper.null"));
157: }
158: throw new ELException(MessageFactory.get("error.fnMapper.method", this.getOutputName()));
159: }
160:
161: Class[] paramTypes = m.getParameterTypes();
162: Object[] params = ((AstMethodArguments) this.children[0]).getParameters(ctx);
163: Object result = null;
164:• for (int i = 0; i < params.length; i++) {
165: try {
166: params[i] = ctx.convertToType(params[i], paramTypes[i]);
167: } catch (ELException ele) {
168: throw new ELException(MessageFactory.get("error.function", this.getOutputName()), ele);
169: }
170: }
171: try {
172: result = m.invoke(null, params);
173: } catch (IllegalAccessException iae) {
174: throw new ELException(MessageFactory.get("error.function", this.getOutputName()), iae);
175: } catch (InvocationTargetException ite) {
176: throw new ELException(MessageFactory.get("error.function", this.getOutputName()), ite.getCause());
177: }
178: return result;
179: }
180:
181: public void setLocalName(String localName) {
182: this.localName = localName;
183: }
184:
185: public void setPrefix(String prefix) {
186: this.prefix = prefix;
187: }
188:
189: @Override
190: public String toString() {
191: return ELParserTreeConstants.jjtNodeName[id] + "[" + this.getOutputName() + "]";
192: }
193: }