I am doing some mapping right now using reflection. This is a bottleneck in my code so I'm aiming to improve the performance. I've created the following method for my builder setter:
private static <T, R, S> BiFunction<T, R, S> createBuilderSetter(Method builderSetterMethod) {
try {
builderSetterMethod.setAccessible(true);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle mh = lookup.unreflect(builderSetterMethod);
return (BiFunction<T, R, S>) LambdaMetafactory.metafactory(lookup, "apply", MethodType.methodType(BiFunction.class), mh.type().generic(), mh, mh.type())
.getTarget()
.invokeExact();
} catch (Throwable t) {
throw new IllegalStateException(t);
}
}
which is being used something like:
BiFunction<MyType.Builder, Object, MyType.Builder> setter = createBuilderSetter(...);
My question is around the middle generic type Object
. This is a generic setter (one of many) that I don't know the type of at compile time. Is the JVM going to be doing any additional type checking above and beyond what it usually does that will slow me down? And if so, can my solution be improved?