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