/
MethodReference.java
440 lines (373 loc) · 15.1 KB
/
MethodReference.java
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright 2002-2019 the original author or authors.
*
* 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
*
* https://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 org.springframework.expression.spel.ast;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
import org.springframework.asm.Label;
import org.springframework.asm.MethodVisitor;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.ExpressionInvocationTargetException;
import org.springframework.expression.MethodExecutor;
import org.springframework.expression.MethodResolver;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.CodeFlow;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
import org.springframework.expression.spel.support.ReflectiveMethodExecutor;
import org.springframework.expression.spel.support.ReflectiveMethodResolver;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Expression language AST node that represents a method reference.
*
* @author Andy Clement
* @author Juergen Hoeller
* @since 3.0
*/
public class MethodReference extends SpelNodeImpl {
private final String name;
private final boolean nullSafe;
@Nullable
private String originalPrimitiveExitTypeDescriptor;
@Nullable
private volatile CachedMethodExecutor cachedExecutor;
public MethodReference(boolean nullSafe, String methodName, int startPos, int endPos, SpelNodeImpl... arguments) {
super(startPos, endPos, arguments);
this.name = methodName;
this.nullSafe = nullSafe;
}
public final String getName() {
return this.name;
}
@Override
protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
Object[] arguments = getArguments(state);
if (state.getActiveContextObject().getValue() == null) {
throwIfNotNullSafe(getArgumentTypes(arguments));
return ValueRef.NullValueRef.INSTANCE;
}
return new MethodValueRef(state, arguments);
}
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
EvaluationContext evaluationContext = state.getEvaluationContext();
Object value = state.getActiveContextObject().getValue();
TypeDescriptor targetType = state.getActiveContextObject().getTypeDescriptor();
Object[] arguments = getArguments(state);
TypedValue result = getValueInternal(evaluationContext, value, targetType, arguments);
updateExitTypeDescriptor();
return result;
}
private TypedValue getValueInternal(EvaluationContext evaluationContext,
@Nullable Object value, @Nullable TypeDescriptor targetType, Object[] arguments) {
List<TypeDescriptor> argumentTypes = getArgumentTypes(arguments);
if (value == null) {
throwIfNotNullSafe(argumentTypes);
return TypedValue.NULL;
}
MethodExecutor executorToUse = getCachedExecutor(evaluationContext, value, targetType, argumentTypes);
if (executorToUse != null) {
try {
return executorToUse.execute(evaluationContext, value, arguments);
}
catch (AccessException ex) {
// Two reasons this can occur:
// 1. the method invoked actually threw a real exception
// 2. the method invoked was not passed the arguments it expected and
// has become 'stale'
// In the first case we should not retry, in the second case we should see
// if there is a better suited method.
// To determine the situation, the AccessException will contain a cause.
// If the cause is an InvocationTargetException, a user exception was
// thrown inside the method. Otherwise the method could not be invoked.
throwSimpleExceptionIfPossible(value, ex);
// At this point we know it wasn't a user problem so worth a retry if a
// better candidate can be found.
this.cachedExecutor = null;
}
}
// either there was no accessor or it no longer existed
executorToUse = findAccessorForMethod(argumentTypes, value, evaluationContext);
this.cachedExecutor = new CachedMethodExecutor(
executorToUse, (value instanceof Class ? (Class<?>) value : null), targetType, argumentTypes);
try {
return executorToUse.execute(evaluationContext, value, arguments);
}
catch (AccessException ex) {
// Same unwrapping exception handling as above in above catch block
throwSimpleExceptionIfPossible(value, ex);
throw new SpelEvaluationException(getStartPosition(), ex,
SpelMessage.EXCEPTION_DURING_METHOD_INVOCATION, this.name,
value.getClass().getName(), ex.getMessage());
}
}
private void throwIfNotNullSafe(List<TypeDescriptor> argumentTypes) {
if (!this.nullSafe) {
throw new SpelEvaluationException(getStartPosition(),
SpelMessage.METHOD_CALL_ON_NULL_OBJECT_NOT_ALLOWED,
FormatHelper.formatMethodForMessage(this.name, argumentTypes));
}
}
private Object[] getArguments(ExpressionState state) {
Object[] arguments = new Object[getChildCount()];
for (int i = 0; i < arguments.length; i++) {
// Make the root object the active context again for evaluating the parameter expressions
try {
state.pushActiveContextObject(state.getScopeRootContextObject());
arguments[i] = this.children[i].getValueInternal(state).getValue();
}
finally {
state.popActiveContextObject();
}
}
return arguments;
}
private List<TypeDescriptor> getArgumentTypes(Object... arguments) {
List<TypeDescriptor> descriptors = new ArrayList<>(arguments.length);
for (Object argument : arguments) {
descriptors.add(TypeDescriptor.forObject(argument));
}
return Collections.unmodifiableList(descriptors);
}
@Nullable
private MethodExecutor getCachedExecutor(EvaluationContext evaluationContext, Object value,
@Nullable TypeDescriptor target, List<TypeDescriptor> argumentTypes) {
List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
if (methodResolvers.size() != 1 || !(methodResolvers.get(0) instanceof ReflectiveMethodResolver)) {
// Not a default ReflectiveMethodResolver - don't know whether caching is valid
return null;
}
CachedMethodExecutor executorToCheck = this.cachedExecutor;
if (executorToCheck != null && executorToCheck.isSuitable(value, target, argumentTypes)) {
return executorToCheck.get();
}
this.cachedExecutor = null;
return null;
}
private MethodExecutor findAccessorForMethod(List<TypeDescriptor> argumentTypes, Object targetObject,
EvaluationContext evaluationContext) throws SpelEvaluationException {
AccessException accessException = null;
List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
for (MethodResolver methodResolver : methodResolvers) {
try {
MethodExecutor methodExecutor = methodResolver.resolve(
evaluationContext, targetObject, this.name, argumentTypes);
if (methodExecutor != null) {
return methodExecutor;
}
}
catch (AccessException ex) {
accessException = ex;
break;
}
}
String method = FormatHelper.formatMethodForMessage(this.name, argumentTypes);
String className = FormatHelper.formatClassNameForMessage(
targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass());
if (accessException != null) {
throw new SpelEvaluationException(
getStartPosition(), accessException, SpelMessage.PROBLEM_LOCATING_METHOD, method, className);
}
else {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND, method, className);
}
}
/**
* Decode the AccessException, throwing a lightweight evaluation exception or,
* if the cause was a RuntimeException, throw the RuntimeException directly.
*/
private void throwSimpleExceptionIfPossible(Object value, AccessException ex) {
if (ex.getCause() instanceof InvocationTargetException) {
Throwable rootCause = ex.getCause().getCause();
if (rootCause instanceof RuntimeException) {
throw (RuntimeException) rootCause;
}
throw new ExpressionInvocationTargetException(getStartPosition(),
"A problem occurred when trying to execute method '" + this.name +
"' on object of type [" + value.getClass().getName() + "]", rootCause);
}
}
private void updateExitTypeDescriptor() {
CachedMethodExecutor executorToCheck = this.cachedExecutor;
if (executorToCheck != null && executorToCheck.get() instanceof ReflectiveMethodExecutor) {
Method method = ((ReflectiveMethodExecutor) executorToCheck.get()).getMethod();
String descriptor = CodeFlow.toDescriptor(method.getReturnType());
if (this.nullSafe && CodeFlow.isPrimitive(descriptor)) {
this.originalPrimitiveExitTypeDescriptor = descriptor;
this.exitTypeDescriptor = CodeFlow.toBoxedDescriptor(descriptor);
}
else {
this.exitTypeDescriptor = descriptor;
}
}
}
@Override
public String toStringAST() {
StringJoiner sj = new StringJoiner(",", "(", ")");
for (int i = 0; i < getChildCount(); i++) {
sj.add(getChild(i).toStringAST());
}
return this.name + sj.toString();
}
/**
* A method reference is compilable if it has been resolved to a reflectively accessible method
* and the child nodes (arguments to the method) are also compilable.
*/
@Override
public boolean isCompilable() {
CachedMethodExecutor executorToCheck = this.cachedExecutor;
if (executorToCheck == null || executorToCheck.hasProxyTarget() ||
!(executorToCheck.get() instanceof ReflectiveMethodExecutor)) {
return false;
}
for (SpelNodeImpl child : this.children) {
if (!child.isCompilable()) {
return false;
}
}
ReflectiveMethodExecutor executor = (ReflectiveMethodExecutor) executorToCheck.get();
if (executor.didArgumentConversionOccur()) {
return false;
}
Class<?> clazz = executor.getMethod().getDeclaringClass();
if (!Modifier.isPublic(clazz.getModifiers()) && executor.getPublicDeclaringClass() == null) {
return false;
}
return true;
}
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
CachedMethodExecutor executorToCheck = this.cachedExecutor;
if (executorToCheck == null || !(executorToCheck.get() instanceof ReflectiveMethodExecutor)) {
throw new IllegalStateException("No applicable cached executor found: " + executorToCheck);
}
ReflectiveMethodExecutor methodExecutor = (ReflectiveMethodExecutor) executorToCheck.get();
Method method = methodExecutor.getMethod();
boolean isStaticMethod = Modifier.isStatic(method.getModifiers());
String descriptor = cf.lastDescriptor();
Label skipIfNull = null;
if (descriptor == null && !isStaticMethod) {
// Nothing on the stack but something is needed
cf.loadTarget(mv);
}
if ((descriptor != null || !isStaticMethod) && this.nullSafe) {
mv.visitInsn(DUP);
skipIfNull = new Label();
Label continueLabel = new Label();
mv.visitJumpInsn(IFNONNULL, continueLabel);
CodeFlow.insertCheckCast(mv, this.exitTypeDescriptor);
mv.visitJumpInsn(GOTO, skipIfNull);
mv.visitLabel(continueLabel);
}
if (descriptor != null && isStaticMethod) {
// Something on the stack when nothing is needed
mv.visitInsn(POP);
}
if (CodeFlow.isPrimitive(descriptor)) {
CodeFlow.insertBoxIfNecessary(mv, descriptor.charAt(0));
}
String classDesc;
if (Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
classDesc = method.getDeclaringClass().getName().replace('.', '/');
}
else {
Class<?> publicDeclaringClass = methodExecutor.getPublicDeclaringClass();
Assert.state(publicDeclaringClass != null, "No public declaring class");
classDesc = publicDeclaringClass.getName().replace('.', '/');
}
if (!isStaticMethod && (descriptor == null || !descriptor.substring(1).equals(classDesc))) {
CodeFlow.insertCheckCast(mv, "L" + classDesc);
}
generateCodeForArguments(mv, cf, method, this.children);
mv.visitMethodInsn((isStaticMethod ? INVOKESTATIC : (method.isDefault() ? INVOKEINTERFACE : INVOKEVIRTUAL)),
classDesc, method.getName(),
CodeFlow.createSignatureDescriptor(method), method.getDeclaringClass().isInterface());
cf.pushDescriptor(this.exitTypeDescriptor);
if (this.originalPrimitiveExitTypeDescriptor != null) {
// The output of the accessor will be a primitive but from the block above it might be null,
// so to have a 'common stack' element at skipIfNull target we need to box the primitive
CodeFlow.insertBoxIfNecessary(mv, this.originalPrimitiveExitTypeDescriptor);
}
if (skipIfNull != null) {
mv.visitLabel(skipIfNull);
}
}
private class MethodValueRef implements ValueRef {
private final EvaluationContext evaluationContext;
@Nullable
private final Object value;
@Nullable
private final TypeDescriptor targetType;
private final Object[] arguments;
public MethodValueRef(ExpressionState state, Object[] arguments) {
this.evaluationContext = state.getEvaluationContext();
this.value = state.getActiveContextObject().getValue();
this.targetType = state.getActiveContextObject().getTypeDescriptor();
this.arguments = arguments;
}
@Override
public TypedValue getValue() {
TypedValue result = MethodReference.this.getValueInternal(
this.evaluationContext, this.value, this.targetType, this.arguments);
updateExitTypeDescriptor();
return result;
}
@Override
public void setValue(@Nullable Object newValue) {
throw new IllegalAccessError();
}
@Override
public boolean isWritable() {
return false;
}
}
private static class CachedMethodExecutor {
private final MethodExecutor methodExecutor;
@Nullable
private final Class<?> staticClass;
@Nullable
private final TypeDescriptor target;
private final List<TypeDescriptor> argumentTypes;
public CachedMethodExecutor(MethodExecutor methodExecutor, @Nullable Class<?> staticClass,
@Nullable TypeDescriptor target, List<TypeDescriptor> argumentTypes) {
this.methodExecutor = methodExecutor;
this.staticClass = staticClass;
this.target = target;
this.argumentTypes = argumentTypes;
}
public boolean isSuitable(Object value, @Nullable TypeDescriptor target, List<TypeDescriptor> argumentTypes) {
return ((this.staticClass == null || this.staticClass == value) &&
ObjectUtils.nullSafeEquals(this.target, target) && this.argumentTypes.equals(argumentTypes));
}
public boolean hasProxyTarget() {
return (this.target != null && Proxy.isProxyClass(this.target.getType()));
}
public MethodExecutor get() {
return this.methodExecutor;
}
}
}