private static MethodHandle arrayEncode() { // Because we need to be able to compile on IBM's JVM, we can't // depend on ArrayEncoder. Unfortunately, ArrayEncoders encode method // is twoish orders of magnitude faster than regular encoders for ascii // so we go through the hurdle of calling that encode method via // a MethodHandle. MethodHandles.Lookup lookup = MethodHandles.lookup(); try { return lookup.unreflect( Class.forName( "sun.nio.cs.ArrayEncoder" ) .getMethod( "encode", char[].class, int.class, int.class, byte[].class ) ); } catch ( Throwable e ) { throw new AssertionError( "This encoder depends on sun.nio.cs.ArrayEncoder, which failed to load: " + e.getMessage(), e ); } }
public MethodHandle run() throws IllegalAccessException, NoSuchMethodException, SecurityException { Method rmet = clazz.getDeclaredMethod(name, params); rmet.setAccessible(true); MethodHandle meth = MethodHandles.lookup().unreflect(rmet); rmet.setAccessible(false); return meth; } });
ImmutablePojoProperties(Type type, Class<T> defn, Supplier<B> builder) { super(type, defn, null, builder); try { builderBuild = MethodHandles.lookup().unreflect(builder.get().getClass().getMethod("build")); } catch (NoSuchMethodException | IllegalAccessException e) { throw new IllegalArgumentException("Failed to inspect Immutables " + defn, e); } } @Override
@Override protected ImmutablesPojoProperty<T> createProperty(String name, Method m) { final Class<?> builderClass = builder.get().getClass(); try { final Type propertyType = GenericTypes.resolveType(m.getGenericReturnType(), getType()); return new ImmutablesPojoProperty<T>( name, QualifiedType.of(propertyType).with(Qualifiers.getQualifiers(m)), m, alwaysSet(), MethodHandles.lookup().unreflect(m), findBuilderSetter(builderClass, name, propertyType)); } catch (IllegalAccessException | NoSuchMethodException e) { throw new IllegalArgumentException("Failed to inspect method " + m, e); } }
@Override protected ImmutablesPojoProperty<T> createProperty(String name, Method m) { final Type propertyType = GenericTypes.resolveType(m.getGenericReturnType(), getType()); try { return new ImmutablesPojoProperty<T>( name, QualifiedType.of(propertyType).with(Qualifiers.getQualifiers(m)), m, isSetMethod(name), MethodHandles.lookup().unreflect(m), MethodHandles.lookup().findVirtual(impl, setterName(name), MethodType.methodType(impl, GenericTypes.getErasedType(propertyType)))); } catch (IllegalAccessException | NoSuchMethodException e) { throw new IllegalArgumentException("Failed to inspect method " + m, e); } }
@SuppressWarnings("ReferenceEquality") @Override public MethodHandle findShadowMethodHandle(Class<?> definingClass, String name, MethodType methodType, boolean isStatic) throws IllegalAccessException { return PerfStatsCollector.getInstance().measure("find shadow method handle", () -> { MethodType actualType = isStatic ? methodType : methodType.dropParameterTypes(0, 1); Class<?>[] paramTypes = actualType.parameterArray(); Method shadowMethod = pickShadowMethod(definingClass, name, paramTypes); if (shadowMethod == CALL_REAL_CODE) { return null; } else if (shadowMethod == DO_NOTHING_METHOD) { return DO_NOTHING; } shadowMethod.setAccessible(true); MethodHandle mh = LOOKUP.unreflect(shadowMethod); // Robolectric doesn't actually look for static, this for example happens // in MessageQueue.nativeInit() which used to be void non-static in 4.2. if (!isStatic && Modifier.isStatic(shadowMethod.getModifiers())) { return dropArguments(mh, 0, Object.class); } else { return mh; } }); }
try { Method method = clazz.getMethod("arrayConstructor", stackTypes.toArray(new Class<?>[stackTypes.size()])); methodHandle = lookup().unreflect(method);
private MethodHandle correctClassForNameAndUnReflectOtherwise(Method m) throws IllegalAccessException { if (m.getDeclaringClass()==Class.class && m.getName().equals("forName") && m.getParameterTypes().length==1) { return MethodHandles.insertArguments(CLASS_FOR_NAME, 1, true, sender.getClassLoader()); } else { return LOOKUP.unreflect(m); } }
private static Optional<Supplier<?>> nullaryMethodOf(Class<?> impl, String methodName) { try { return Optional.of(Unchecked.supplier(MethodHandles.lookup() .unreflect(impl.getMethod(methodName))::invoke)); } catch (ReflectiveOperationException e) { return Optional.empty(); } }
private static Optional<MethodHandle> unreflect(Method method) { try { return Optional.of(MethodHandles.publicLookup().unreflect(method)); } catch (IllegalAccessException ex) { LOGGER.log(Level.FINER, ex, () -> "Access checking fails on " + method.getDeclaringClass() + " class, method '" + method.getName() + "' with parameters " + Arrays.asList(method.getParameters()) + "."); } return Optional.empty(); }
public MethodHandle run() throws IllegalAccessException, NoSuchMethodException, SecurityException { Method rmet = clazz.getDeclaredMethod(name, params); rmet.setAccessible(true); MethodHandle meth = MethodHandles.lookup().unreflect(rmet); rmet.setAccessible(false); return meth; } });
private static MethodHandle unreflect(Class<?> sqlObjectType, Method m) { try { return DefaultMethodHandler.lookupFor(sqlObjectType).unreflect(m); } catch (IllegalAccessException e) { throw new UnableToCreateSqlObjectException("Bridge handler couldn't unreflect " + sqlObjectType + " " + m, e); } } }
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; }
/** * 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); } }
/** * 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(StandardErrorCode errorCode, Method method) { try { return MethodHandles.lookup().unreflect(method); } catch (IllegalAccessException e) { throw new PrestoException(errorCode, e); } }
private static MethodHandle findCleanMethod( MethodHandles.Lookup lookup, Class<?> cleaner ) throws IllegalAccessException, NoSuchMethodException { return lookup.unreflect( cleaner.getDeclaredMethod( "clean" ) ); }
private static MethodHandle method( Class<?> target, String name, Class<?>... parameters ) throws Exception { return MethodHandles.lookup().unreflect( target.getMethod( name, parameters ) ); }
private static MethodHandle findCreationMethod( String methodName, MethodHandles.Lookup lookup, Class<?> cleaner ) throws IllegalAccessException, NoSuchMethodException { return lookup.unreflect( cleaner.getDeclaredMethod( methodName, Object.class, Runnable.class ) ); }