001/* 002 * Copyright 2002-2019 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.expression.spel.ast; 018 019import java.lang.reflect.Method; 020import java.lang.reflect.Modifier; 021import java.util.StringJoiner; 022 023import org.springframework.asm.MethodVisitor; 024import org.springframework.core.MethodParameter; 025import org.springframework.core.convert.TypeDescriptor; 026import org.springframework.expression.EvaluationException; 027import org.springframework.expression.TypeConverter; 028import org.springframework.expression.TypedValue; 029import org.springframework.expression.spel.CodeFlow; 030import org.springframework.expression.spel.ExpressionState; 031import org.springframework.expression.spel.SpelEvaluationException; 032import org.springframework.expression.spel.SpelMessage; 033import org.springframework.expression.spel.support.ReflectionHelper; 034import org.springframework.lang.Nullable; 035import org.springframework.util.Assert; 036import org.springframework.util.ClassUtils; 037import org.springframework.util.ReflectionUtils; 038 039/** 040 * A function reference is of the form "#someFunction(a,b,c)". Functions may be defined 041 * in the context prior to the expression being evaluated. Functions may also be static 042 * Java methods, registered in the context prior to invocation of the expression. 043 * 044 * <p>Functions are very simplistic. The arguments are not part of the definition 045 * (right now), so the names must be unique. 046 * 047 * @author Andy Clement 048 * @author Juergen Hoeller 049 * @since 3.0 050 */ 051public class FunctionReference extends SpelNodeImpl { 052 053 private final String name; 054 055 // Captures the most recently used method for the function invocation *if* the method 056 // can safely be used for compilation (i.e. no argument conversion is going on) 057 @Nullable 058 private volatile Method method; 059 060 061 public FunctionReference(String functionName, int startPos, int endPos, SpelNodeImpl... arguments) { 062 super(startPos, endPos, arguments); 063 this.name = functionName; 064 } 065 066 067 @Override 068 public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { 069 TypedValue value = state.lookupVariable(this.name); 070 if (value == TypedValue.NULL) { 071 throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_NOT_DEFINED, this.name); 072 } 073 if (!(value.getValue() instanceof Method)) { 074 // Possibly a static Java method registered as a function 075 throw new SpelEvaluationException( 076 SpelMessage.FUNCTION_REFERENCE_CANNOT_BE_INVOKED, this.name, value.getClass()); 077 } 078 079 try { 080 return executeFunctionJLRMethod(state, (Method) value.getValue()); 081 } 082 catch (SpelEvaluationException ex) { 083 ex.setPosition(getStartPosition()); 084 throw ex; 085 } 086 } 087 088 /** 089 * Execute a function represented as a {@code java.lang.reflect.Method}. 090 * @param state the expression evaluation state 091 * @param method the method to invoke 092 * @return the return value of the invoked Java method 093 * @throws EvaluationException if there is any problem invoking the method 094 */ 095 private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException { 096 Object[] functionArgs = getArguments(state); 097 098 if (!method.isVarArgs()) { 099 int declaredParamCount = method.getParameterCount(); 100 if (declaredParamCount != functionArgs.length) { 101 throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, 102 functionArgs.length, declaredParamCount); 103 } 104 } 105 if (!Modifier.isStatic(method.getModifiers())) { 106 throw new SpelEvaluationException(getStartPosition(), 107 SpelMessage.FUNCTION_MUST_BE_STATIC, ClassUtils.getQualifiedMethodName(method), this.name); 108 } 109 110 // Convert arguments if necessary and remap them for varargs if required 111 TypeConverter converter = state.getEvaluationContext().getTypeConverter(); 112 boolean argumentConversionOccurred = ReflectionHelper.convertAllArguments(converter, functionArgs, method); 113 if (method.isVarArgs()) { 114 functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation( 115 method.getParameterTypes(), functionArgs); 116 } 117 boolean compilable = false; 118 119 try { 120 ReflectionUtils.makeAccessible(method); 121 Object result = method.invoke(method.getClass(), functionArgs); 122 compilable = !argumentConversionOccurred; 123 return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result)); 124 } 125 catch (Exception ex) { 126 throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL, 127 this.name, ex.getMessage()); 128 } 129 finally { 130 if (compilable) { 131 this.exitTypeDescriptor = CodeFlow.toDescriptor(method.getReturnType()); 132 this.method = method; 133 } 134 else { 135 this.exitTypeDescriptor = null; 136 this.method = null; 137 } 138 } 139 } 140 141 @Override 142 public String toStringAST() { 143 StringJoiner sj = new StringJoiner(",", "(", ")"); 144 for (int i = 0; i < getChildCount(); i++) { 145 sj.add(getChild(i).toStringAST()); 146 } 147 return '#' + this.name + sj.toString(); 148 } 149 150 /** 151 * Compute the arguments to the function, they are the children of this expression node. 152 * @return an array of argument values for the function call 153 */ 154 private Object[] getArguments(ExpressionState state) throws EvaluationException { 155 // Compute arguments to the function 156 Object[] arguments = new Object[getChildCount()]; 157 for (int i = 0; i < arguments.length; i++) { 158 arguments[i] = this.children[i].getValueInternal(state).getValue(); 159 } 160 return arguments; 161 } 162 163 @Override 164 public boolean isCompilable() { 165 Method method = this.method; 166 if (method == null) { 167 return false; 168 } 169 int methodModifiers = method.getModifiers(); 170 if (!Modifier.isStatic(methodModifiers) || !Modifier.isPublic(methodModifiers) || 171 !Modifier.isPublic(method.getDeclaringClass().getModifiers())) { 172 return false; 173 } 174 for (SpelNodeImpl child : this.children) { 175 if (!child.isCompilable()) { 176 return false; 177 } 178 } 179 return true; 180 } 181 182 @Override 183 public void generateCode(MethodVisitor mv, CodeFlow cf) { 184 Method method = this.method; 185 Assert.state(method != null, "No method handle"); 186 String classDesc = method.getDeclaringClass().getName().replace('.', '/'); 187 generateCodeForArguments(mv, cf, method, this.children); 188 mv.visitMethodInsn(INVOKESTATIC, classDesc, method.getName(), 189 CodeFlow.createSignatureDescriptor(method), false); 190 cf.pushDescriptor(this.exitTypeDescriptor); 191 } 192 193}