io.qt.internal.JavaMethodHandles Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qtjambi Show documentation
Show all versions of qtjambi Show documentation
QtJambi base module containing QtCore, QtGui and QtWidgets.
package io.qt.internal;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandleProxies;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import io.qt.core.QDataStream;
import io.qt.core.QMetaObject;
/**
* @hidden
*/
final class JavaMethodHandles implements ReflectionUtility.MethodInvocationHandler{
public final boolean useMethodHandleProxies;
JavaMethodHandles() {
boolean _useMethodHandleProxies = false;
try {
java.lang.invoke.MethodHandleProxies.class.hashCode();
_useMethodHandleProxies = true;
}catch(Throwable t) {}
useMethodHandleProxies = _useMethodHandleProxies;
}
@Override
@SuppressWarnings("unchecked")
public Function functionFromMethod(Method method){
if(method==null)
return null;
try {
Lookup lookup = ReflectionUtility.privateLookup(method.getDeclaringClass());
MethodHandle handle = lookup.unreflect(method);
if(useMethodHandleProxies) {
try {
return MethodHandleProxies.asInterfaceInstance(Function.class, handle);
} catch (Throwable e) {
}
}
return a->{
try {
return (B)handle.invoke(a);
} catch (RuntimeException | Error e) {
throw e;
}catch(Throwable t) {
throw new RuntimeException(t);
}
};
} catch (IllegalAccessException e) {
return null;
}
}
@Override
@SuppressWarnings("unchecked")
public BiConsumer