@Override public MethodHandle getMethodHandle(String methodName, MethodType type) throws NoSuchMethodException, IllegalAccessException { return lookup.findStatic(getClass(), "logE", methodType(void.class, Object[].class)); } }
@Override public MethodHandle getMethodHandle(String methodName, MethodType type) throws NoSuchMethodException, IllegalAccessException { return lookup.findStatic(getClass(), "eldest", methodType(Object.class, LinkedHashMap.class)); } }
@Override public MethodHandle getMethodHandle(String methodName, MethodType type) throws NoSuchMethodException, IllegalAccessException { return lookup.findStatic(getClass(), "adjustLanguageCode", methodType(String.class, String.class)); } }
final MethodHandle nonNullTest = lookup.findStatic(Objects.class, "nonNull", methodType(boolean.class, Object.class)) .asType(methodType(boolean.class, cleanerClass));
final MethodHandle nonNullTest = lookup.findStatic(Objects.class, "nonNull", methodType(boolean.class, Object.class)) .asType(methodType(boolean.class, cleanerClass)); final MethodHandle noop = dropArguments(constant(Void.class, null).asType(methodType(void.class)), 0, cleanerClass);
final MethodHandle nonNullTest = lookup.findStatic(Objects.class, "nonNull", methodType(boolean.class, Object.class)) .asType(methodType(boolean.class, cleanerClass)); final MethodHandle noop = dropArguments(constant(Void.class, null).asType(methodType(void.class)), 0, cleanerClass);
@Test public void testComplexMethodHandleUpdated() throws Throwable { assert __version__() == 0; MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findVirtual(A.class, "method", MethodType.methodType(int.class)); MethodHandle filter = lookup.findVirtual(A.class, "filter", MethodType.methodType(int.class, int.class)); MethodHandle staticFilter = lookup.findStatic(A.class, "staticFilter", MethodType.methodType(int.class, int.class)); A a = new A(3); MethodHandle boundFilter = filter.bindTo(a); handle = MethodHandles.filterReturnValue(handle, staticFilter); handle = MethodHandles.filterReturnValue(handle, boundFilter); assertEquals(1011, handle.invoke(a)); __toVersion__(1); assertEquals(10102, handle.invoke(a)); __toVersion__(0); assert __version__() == 0; }
@Test public void testStaticInterfaceMethods() throws Throwable { assert __version__() == 0; MethodHandle handle = MethodHandles.publicLookup().findStatic(A.class, "staticMethod", MethodType.methodType(int.class)); assertEquals(1, A.staticMethod()); assertEquals(1, handle.invoke()); __toVersion__(1); assertEquals(2, A.staticMethod()); assertEquals(2, handle.invoke()); __toVersion__(0); } }
private static void makeMapEntry(String method, MethodType[] keys, MethodType[] values) throws NoSuchMethodException, IllegalAccessException { Map<MethodType,MethodHandle> xMap = new HashMap(); methods.put(method, xMap); for (int i=0; i<keys.length; i++) { xMap.put(keys[i], LOOKUP.findStatic(IndyMath.class, method, values[i])); } }
@Test public void testStaticMethodHandleUpdated() throws Throwable { assert __version__() == 0; MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findStatic(A.class, "staticMethod", MethodType.methodType(int.class)); assertEquals(3, handle.invoke()); __toVersion__(1); assertEquals(4, handle.invoke()); __toVersion__(0); assert __version__() == 0; }
public void upgradeIndex( Path indexPath ) throws Throwable { // since lucene use ServiceLocator to load services, context class loader need to be replaced as well ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { if ( mainMethod == null ) { luceneLoader = jarLoaderSupplier.get(); Class upgrader = luceneLoader.loadEmbeddedClass( LUCENE_INDEX_UPGRADER_CLASS_NAME ); MethodHandles.Lookup lookup = MethodHandles.lookup(); mainMethod = lookup.findStatic( upgrader, "main", MethodType.methodType( void.class, String[].class ) ); } Thread.currentThread().setContextClassLoader( luceneLoader.getJarsClassLoader() ); mainMethod.invokeExact( new String[]{indexPath.toString()} ); } finally { Thread.currentThread().setContextClassLoader( contextClassLoader ); } }
private MethodHandle privateLookupIn() { try { return lookup.findStatic(MethodHandles.class, "privateLookupIn", MethodType.methodType(MethodHandles.Lookup.class, Class.class, MethodHandles.Lookup.class)); } catch (NoSuchMethodException | IllegalAccessException e) { throw new ObjenesisException(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() {
@Override public MethodHandle getMethodHandle(String methodName, MethodType type) throws NoSuchMethodException, IllegalAccessException { switch (methodName) { case "create": return lookup.findStatic(getClass(), "create", methodType(Object.class, Object.class, Runnable.class)); case "clean": return lookup.findStatic(getClass(), "clean", methodType(void.class, Object.class)); default: throw new IllegalStateException(); } } }
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 unmapJava7Or8(MethodHandles.Lookup lookup) throws ReflectiveOperationException { /* "Compile" a MethodHandle that is roughly equivalent to the following lambda: * * (ByteBuffer buffer) -> { * sun.misc.Cleaner cleaner = ((java.nio.DirectByteBuffer) byteBuffer).cleaner(); * if (nonNull(cleaner)) * cleaner.clean(); * else * noop(cleaner); // the noop is needed because MethodHandles#guardWithTest always needs both if and else * } */ Class<?> directBufferClass = Class.forName("java.nio.DirectByteBuffer"); Method m = directBufferClass.getMethod("cleaner"); m.setAccessible(true); MethodHandle directBufferCleanerMethod = lookup.unreflect(m); Class<?> cleanerClass = directBufferCleanerMethod.type().returnType(); MethodHandle cleanMethod = lookup.findVirtual(cleanerClass, "clean", methodType(void.class)); MethodHandle nonNullTest = lookup.findStatic(MappedByteBuffers.class, "nonNull", methodType(boolean.class, Object.class)).asType(methodType(boolean.class, cleanerClass)); MethodHandle noop = dropArguments(constant(Void.class, null).asType(methodType(void.class)), 0, cleanerClass); MethodHandle unmapper = filterReturnValue(directBufferCleanerMethod, guardWithTest(nonNullTest, cleanMethod, noop)) .asType(methodType(void.class, ByteBuffer.class)); return unmapper; }
@Override public MethodHandle getMethodHandle(String methodName, MethodType type) throws NoSuchMethodException, IllegalAccessException { switch (methodName) { case "nanoTime": return lookup.findStatic(ShadowSystemClock.class, "nanoTime", methodType(long.class)); case "currentTimeMillis": return lookup.findStatic(ShadowSystemClock.class, "currentTimeMillis", methodType(long.class)); } throw new UnsupportedOperationException(); } }
@Override public MethodHandle getMethodHandle(String methodName, MethodType type) throws NoSuchMethodException, IllegalAccessException { return lookup.findStatic(System.class, "arraycopy", methodType(void.class, Object.class, int.class, Object.class, int.class, int.class)); } }
@Override public MethodHandle getMethodHandle(String methodName, MethodType type) throws NoSuchMethodException, IllegalAccessException { Class<?> shadowWindowClass; try { shadowWindowClass = type.returnType().getClassLoader().loadClass(ShadowWindow.class.getName()); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } return lookup.in(type.returnType()).findStatic(shadowWindowClass, "create", type); } }
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; } }