@Override public MethodHandle getMethodHandle(String methodName, MethodType type) throws NoSuchMethodException, IllegalAccessException { MethodHandle nothing = constant(Void.class, null).asType(methodType(void.class)); if (type.parameterCount() != 0) { return dropArguments(nothing, 0, type.parameterArray()); } else { return nothing; } } }
private static MethodHandle cleanStackTraces(MethodHandle mh) { MethodType type = EXCEPTION_HANDLER.type().changeReturnType(mh.type().returnType()); return catchException(mh, Throwable.class, EXCEPTION_HANDLER.asType(type)); } }
@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)); } }
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; } }
private static MethodHandle unmapJava9(MethodHandles.Lookup lookup) throws ReflectiveOperationException { Class<?> unsafeClass = Class.forName("sun.misc.Unsafe"); MethodHandle unmapper = lookup.findVirtual(unsafeClass, "invokeCleaner", methodType(void.class, ByteBuffer.class)); Field f = unsafeClass.getDeclaredField("theUnsafe"); f.setAccessible(true); Object theUnsafe = f.get(null); return unmapper.bindTo(theUnsafe); }
@Override Object invokeDefaultMethod(Method method, Class<?> declaringClass, Object object, @Nullable Object... args) throws Throwable { // Because the service interface might not be public, we need to use a MethodHandle lookup // that ignores the visibility of the declaringClass. Constructor<Lookup> constructor = Lookup.class.getDeclaredConstructor(Class.class, int.class); constructor.setAccessible(true); return constructor.newInstance(declaringClass, -1 /* trusted */) .unreflectSpecial(method, declaringClass) .bindTo(object) .invokeWithArguments(args); }
/** * @param f (U, S1, S2, ..., Sm)R * @param g (T1, T2, ..., Tn)U * @return (T1, T2, ..., Tn, S1, S2, ..., Sm)R */ public static MethodHandle compose(MethodHandle f, MethodHandle g) { if (f.type().parameterType(0) != g.type().returnType()) { throw new IllegalArgumentException(format("f.parameter(0) != g.return(). f: %s g: %s", f.type(), g.type())); } // Semantics: f => f // Type: (U, S1, S2, ..., Sn)R => (U, T1, T2, ..., Tm, S1, S2, ..., Sn)R MethodHandle fUTS = MethodHandles.dropArguments(f, 1, g.type().parameterList()); // Semantics: f => fg // Type: (U, T1, T2, ..., Tm, S1, S2, ..., Sn)R => (T1, T2, ..., Tm, S1, S2, ..., Sn)R return MethodHandles.foldArguments(fUTS, g); }
@Override public Checksum create() { try { return (Checksum) CONSTRUCTOR.invoke(); } catch (Throwable throwable) { // Should never happen throw new RuntimeException(throwable); } } }
private static MethodHandle constructor( Class<?> target, Class<?>... parameters ) throws Exception { return MethodHandles.lookup().unreflectConstructor( target.getConstructor( parameters ) ); }
public static CallSite bootstrap(MethodHandles.Lookup callerLookup, String name, MethodType type, long bindingId) { ClassLoader classLoader = callerLookup.lookupClass().getClassLoader(); checkArgument(classLoader instanceof DynamicClassLoader, "Expected %s's classloader to be of type %s", callerLookup.lookupClass().getName(), DynamicClassLoader.class.getName()); DynamicClassLoader dynamicClassLoader = (DynamicClassLoader) classLoader; MethodHandle target = dynamicClassLoader.getCallSiteBindings().get(bindingId); checkArgument(target != null, "Binding %s for function %s%s not found", bindingId, name, type.parameterList()); return new ConstantCallSite(target); } }
private static MethodHandle findConstructor(String className, MethodType methodType) { try { return MethodHandles.publicLookup().findConstructor(Class.forName(className), methodType); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } }
private static void verifyMethodParameterType(MethodHandle method, int index, Class javaType, String sqlTypeDisplayName) { checkArgument(method.type().parameterArray()[index] == javaType, "Expected method %s parameter %s type to be %s (%s)", method, index, javaType.getName(), sqlTypeDisplayName); }
public static MethodHandle bindDependencies(MethodHandle handle, List<ImplementationDependency> dependencies, BoundVariables variables, TypeManager typeManager, FunctionRegistry functionRegistry) { for (ImplementationDependency dependency : dependencies) { handle = MethodHandles.insertArguments(handle, 0, dependency.resolve(variables, typeManager, functionRegistry)); } return handle; } }
@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 OutputStream wrapForOutput(ByteBufferOutputStream buffer, byte messageVersion) { try { return (OutputStream) SnappyConstructors.OUTPUT.invoke(buffer); } catch (Throwable e) { throw new KafkaException(e); } }
@Override public MethodHandle getMethodHandle(String methodName, MethodType type) throws NoSuchMethodException, IllegalAccessException { return lookup.findStatic(getClass(), "eldest", methodType(Object.class, LinkedHashMap.class)); } }
@Override public OutputStream wrapForOutput(ByteBufferOutputStream buffer, byte messageVersion) { try { return (OutputStream) ZstdConstructors.OUTPUT.invoke(buffer); } catch (Throwable e) { throw new KafkaException(e); } }
@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(), "adjustLanguageCode", methodType(String.class, String.class)); } }