1 | package pro.verron.officestamper.core; | |
2 | ||
3 | import org.springframework.expression.AccessException; | |
4 | import org.springframework.expression.EvaluationContext; | |
5 | import org.springframework.expression.MethodExecutor; | |
6 | import org.springframework.expression.TypedValue; | |
7 | ||
8 | import java.lang.reflect.InvocationTargetException; | |
9 | import java.lang.reflect.Method; | |
10 | import java.util.Arrays; | |
11 | ||
12 | /// A record encapsulating an object and a method, and providing functionality to execute the method on the given | |
13 | /// object using reflection. | |
14 | /// This record implements the [MethodExecutor] interface and serves as a mechanism to invoke methods dynamically. | |
15 | public record ReflectionExecutor(Object object, Method method) | |
16 | implements MethodExecutor { | |
17 | ||
18 | /// Executes the provided method on the given object using the specified arguments. | |
19 | /// This method utilizes reflection to invoke the target method dynamically. | |
20 | /// | |
21 | /// @param context the evaluation context in which this execution occurs. | |
22 | /// @param target the target object on which the method should be invoked. | |
23 | /// @param arguments the arguments to be passed to the method during invocation. | |
24 | /// | |
25 | /// @return a TypedValue wrapping the result of the invoked method. | |
26 | /// | |
27 | /// @throws AccessException if the method cannot be accessed or invoked, or if an error occurs during invocation. | |
28 | @Override | |
29 | public TypedValue execute(EvaluationContext context, Object target, Object... arguments) | |
30 | throws AccessException { | |
31 | try { | |
32 | var value = method.invoke(object, arguments); | |
33 |
1
1. execute : replaced return value with null for pro/verron/officestamper/core/ReflectionExecutor::execute → KILLED |
return new TypedValue(value); |
34 | } catch (InvocationTargetException | IllegalAccessException e) { | |
35 | var message = "Failed to invoke method %s with arguments [%s] from object %s".formatted(method, | |
36 | Arrays.toString(arguments), | |
37 | object); | |
38 | throw new AccessException(message, e); | |
39 | } | |
40 | } | |
41 | } | |
Mutations | ||
33 |
1.1 |