bbeaudreault commented on code in PR #5488:
URL: https://github.com/apache/hbase/pull/5488#discussion_r1383390765
##########
hbase-common/src/main/java/org/apache/hadoop/hbase/util/ReflectionUtils.java:
##########
@@ -208,6 +214,30 @@ private static String getTaskName(long id, String name) {
return id + " (" + name + ")";
}
+ /**
+ * Creates a Function which can be called to performantly execute a
reflected static method. The
+ * creation of the Function itself may not be fast, but executing that
method thereafter should be
+ * much faster than {@link #invokeMethod(Object, String, Object...)}.
+ * @param lookupClazz the class to find the static method in
+ * @param methodName the method name
+ * @param argumentClazz the type of the argument
+ * @param returnValueClass the type of the return value
+ * @return a function which when called executes the requested static method.
+ * @throws Throwable exception types from the underlying reflection
+ */
+ public static <I, R> Function<I, R> getOneArgStaticMethodAsFunction(Class<?>
lookupClazz,
+ String methodName, Class<I> argumentClazz, Class<R> returnValueClass)
throws Throwable {
+ MethodHandles.Lookup lookup = MethodHandles.lookup();
+ MethodHandle methodHandle = lookup.findStatic(lookupClazz, methodName,
+ MethodType.methodType(returnValueClass, argumentClazz));
+ CallSite site =
+ LambdaMetafactory.metafactory(lookup, "apply",
MethodType.methodType(Function.class),
+ methodHandle.type().generic(), methodHandle, methodHandle.type());
+
+ return (Function<I, R>) site.getTarget().invokeExact();
Review Comment:
Correct, there's a good amount of content on google around
LambdaMetafactory. Basically it's a new feature in java 8 which exposes the
methods used by java for creating functional interfaces from lambdas, for us to
use in reflection. Once created, the invocation time is nearly as fast as
native java code.
So the code above is specifying the the signature of a Function's apply
method, which takes a byte[] and returns a Filter. I wrote it using generics so
that we could possibly plug this into other high-throughput reflection cases we
have.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]