Skip to content

Commit

Permalink
Invoke build system's methods via MethodHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jul 24, 2024
1 parent 0a4a366 commit 48bdbfc
Showing 1 changed file with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static java.util.Arrays.asList;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand All @@ -23,6 +24,7 @@
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -432,6 +434,7 @@ private static Consumer<BuildChainBuilder> loadStepsFromClass(Class<?> clazz,
final List<Method> methods = getMethods(clazz);
final Map<String, List<Method>> nameToMethods = methods.stream().collect(Collectors.groupingBy(m -> m.getName()));

MethodHandles.Lookup lookup = MethodHandles.publicLookup();
for (Method method : methods) {
final BuildStep buildStep = method.getAnnotation(BuildStep.class);
if (buildStep == null) {
Expand Down Expand Up @@ -846,17 +849,13 @@ public void execute(final BuildContext bc) {
}
Object result;
try {
result = method.invoke(instance, methodArgs);
result = lookup.unreflect(method).bindTo(instance).invokeWithArguments(methodArgs);
} catch (IllegalAccessException e) {
throw ReflectUtil.toError(e);
} catch (InvocationTargetException e) {
try {
throw e.getCause();
} catch (RuntimeException | Error e2) {
throw e2;
} catch (Throwable t) {
throw new IllegalStateException(t);
}
} catch (RuntimeException | Error e2) {
throw e2;
} catch (Throwable t) {
throw new UndeclaredThrowableException(t);
}
resultConsumer.accept(bc, result);
if (isRecorder) {
Expand Down

0 comments on commit 48bdbfc

Please sign in to comment.