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 23, 2024
1 parent fc6adf5 commit 4f31cab
Showing 1 changed file with 5 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 Down Expand Up @@ -432,6 +433,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.lookup();
for (Method method : methods) {
final BuildStep buildStep = method.getAnnotation(BuildStep.class);
if (buildStep == null) {
Expand Down Expand Up @@ -846,17 +848,11 @@ 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 (Throwable t) {
throw new IllegalStateException(t);
}
resultConsumer.accept(bc, result);
if (isRecorder) {
Expand Down

0 comments on commit 4f31cab

Please sign in to comment.