private Lookup getLookup(Class<?> declaringClass) { Lookup lookup = MethodHandles.lookup(); if (privateLookupIn != null) { try { return (Lookup) privateLookupIn.invoke(null, declaringClass, lookup); } catch (ReflectiveOperationException e) { return lookup; } } return lookup; }
@Override public <T> Class<?> defineClass(Class<T> iClass, String reflectorClassName, byte[] bytecode) { MethodHandles.Lookup lookup = MethodHandles.lookup(); try { // MethodHandles.Lookup privateLookup = MethodHandles.privateLookupIn(iClass, lookup); MethodHandles.Lookup privateLookup = (Lookup) privateLookupInMethod.invoke(lookup, iClass, lookup); // return privateLookup.defineClass(bytecode); return (Class<?>) defineClassMethod.invoke(privateLookup, bytecode); } catch (IllegalAccessException | InvocationTargetException e) { throw new AssertionError(e); } } }
private static MethodHandle getPositionLockGetter() { try { MethodHandles.Lookup lookup = MethodHandles.lookup(); Field field = FileChannelImpl.class.getDeclaredField( "positionLock" ); field.setAccessible( true ); return lookup.unreflectGetter( field ); } catch ( Exception e ) { return null; } }
private static MethodHandle lookupUnmapMethodHandle() { final MethodHandles.Lookup lookup = lookup(); try { if (Java.IS_JAVA9_COMPATIBLE) return unmapJava9(lookup); else return unmapJava7Or8(lookup); } catch (ReflectiveOperationException | RuntimeException e1) { throw new UnsupportedOperationException("Unmapping is not supported on this platform, because internal " + "Java APIs are not compatible with this Kafka version", e1); } }
private static Object globalCleaner() { MethodHandles.Lookup lookup = MethodHandles.lookup(); try { Class<?> newCleaner = Class.forName( "java.lang.ref.Cleaner" ); MethodHandle createInstance = lookup.findStatic( newCleaner, "create", MethodType.methodType( newCleaner ) ); return createInstance.invoke(); } catch ( Throwable throwable ) { return null; } }
/** * Returns a MethodHandle corresponding to the specified method. * <p> * Warning: The way Oracle JVM implements producing MethodHandle for a method involves creating * JNI global weak references. G1 processes such references serially. As a result, calling this * method in a tight loop can create significant GC pressure and significantly increase * application pause time. */ public static MethodHandle methodHandle(Class<?> clazz, String name, Class<?>... parameterTypes) { try { return MethodHandles.lookup().unreflect(clazz.getMethod(name, parameterTypes)); } catch (IllegalAccessException | NoSuchMethodException e) { throw new PrestoException(GENERIC_INTERNAL_ERROR, e); } }
/** * Returns a MethodHandle corresponding to the specified constructor. * <p> * Warning: The way Oracle JVM implements producing MethodHandle for a constructor involves * creating JNI global weak references. G1 processes such references serially. As a result, * calling this method in a tight loop can create significant GC pressure and significantly * increase application pause time. */ public static MethodHandle constructorMethodHandle(StandardErrorCode errorCode, Class<?> clazz, Class<?>... parameterTypes) { try { return MethodHandles.lookup().unreflectConstructor(clazz.getConstructor(parameterTypes)); } catch (IllegalAccessException | NoSuchMethodException e) { throw new PrestoException(errorCode, e); } }
/** * Returns a MethodHandle corresponding to the specified method. * <p> * Warning: The way Oracle JVM implements producing MethodHandle for a method involves creating * JNI global weak references. G1 processes such references serially. As a result, calling this * method in a tight loop can create significant GC pressure and significantly increase * application pause time. */ public static MethodHandle methodHandle(Class<?> clazz, String name, Class<?>... parameterTypes) { try { return MethodHandles.lookup().unreflect(clazz.getMethod(name, parameterTypes)); } catch (IllegalAccessException | NoSuchMethodException e) { throw new PrestoException(GENERIC_INTERNAL_ERROR, e); } }
/** * Returns a MethodHandle corresponding to the specified method. * <p> * Warning: The way Oracle JVM implements producing MethodHandle for a method involves creating * JNI global weak references. G1 processes such references serially. As a result, calling this * method in a tight loop can create significant GC pressure and significantly increase * application pause time. */ public static MethodHandle methodHandle(Class<?> clazz, String name, Class<?>... parameterTypes) { try { return MethodHandles.lookup().unreflect(clazz.getMethod(name, parameterTypes)); } catch (IllegalAccessException | NoSuchMethodException e) { throw new PrestoException(GENERIC_INTERNAL_ERROR, e); } }
private static CleanerHandles findCleanerHandles() { MethodHandles.Lookup lookup = MethodHandles.lookup(); return globalCleanerInstance == null ? findHandlesForOldCleaner( lookup ) : findHandlesForNewCleaner( lookup ); }
private UnaryOperator<Key> checkForCloneMethod(final Class<?> declType, final Class<?> returnType) { final MethodHandles.Lookup lookup = MethodHandles.lookup(); final MethodHandle handle = doPrivileged((PrivilegedAction<MethodHandle>) () -> { try { return lookup.findVirtual(declType, "clone", MethodType.methodType(returnType)); } catch (NoSuchMethodException | IllegalAccessException e) { return null; } }); return handle == null ? null : produceOp(handle); }
private UnaryOperator<Key> checkForCopyCtor(final Class<?> declType, final Class<?> paramType) { final MethodHandles.Lookup lookup = MethodHandles.lookup(); final MethodHandle handle = doPrivileged((PrivilegedAction<MethodHandle>) () -> { try { return lookup.findConstructor(declType, MethodType.methodType(void.class, paramType)); } catch (NoSuchMethodException | IllegalAccessException e) { return null; } }); return handle == null ? null : produceOp(handle); }
private static MethodHandle lookupNewJarFile() { try { return MethodHandles.lookup().findConstructor(java.util.jar.JarFile.class, MethodType.methodType(void.class, java.io.File.class, java.lang.Boolean.TYPE, Integer.TYPE, runtimeVersionClass())); } catch (Throwable t) { return null; } } private static Class<?> runtimeVersionClass() throws ClassNotFoundException {
private static MethodHandle lookupRuntimeVersion() { try { return MethodHandles.lookup().findStatic(java.lang.Runtime.class, "version", MethodType.methodType(runtimeVersionClass())); } catch (Throwable t) { return null; } } private static MethodHandle lookupRuntimeVersionMajor() {
private static MethodHandle lookupRuntimeVersionMajor() { try { return MethodHandles.lookup().findVirtual(runtimeVersionClass(), "major", MethodType.methodType(Integer.TYPE)); } catch (Throwable t) { return null; } } private static MethodHandle lookupNewJarFile() {
/** * Returns a MethodHandle corresponding to the specified method. * <p> * Warning: The way Oracle JVM implements producing MethodHandle for a method involves creating * JNI global weak references. G1 processes such references serially. As a result, calling this * method in a tight loop can create significant GC pressure and significantly increase * application pause time. */ public static MethodHandle methodHandle(StandardErrorCode errorCode, Method method) { try { return MethodHandles.lookup().unreflect(method); } catch (IllegalAccessException e) { throw new PrestoException(errorCode, e); } }
/** * Returns a MethodHandle corresponding to the specified constructor. * <p> * Warning: The way Oracle JVM implements producing MethodHandle for a constructor involves * creating JNI global weak references. G1 processes such references serially. As a result, * calling this method in a tight loop can create significant GC pressure and significantly * increase application pause time. */ public static MethodHandle constructorMethodHandle(StandardErrorCode errorCode, Constructor constructor) { try { return MethodHandles.lookup().unreflectConstructor(constructor); } catch (IllegalAccessException e) { throw new PrestoException(errorCode, e); } } }
private static MethodHandle lookupRuntimeVersionParse() { try { return MethodHandles.lookup().findStatic(runtimeVersionClass(), "parse", MethodType.methodType(runtimeVersionClass(), String.class)); } catch (Throwable t) { return null; } } private static MethodHandle lookupRuntimeVersion() {
private static MethodHandle constructor( Class<?> target, Class<?>... parameters ) throws Exception { return MethodHandles.lookup().unreflectConstructor( target.getConstructor( parameters ) ); }
private static MethodHandle method( Class<?> target, String name, Class<?>... parameters ) throws Exception { return MethodHandles.lookup().unreflect( target.getMethod( name, parameters ) ); }