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