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
private MethodHandle isSetMethod(String name) { try { return MethodHandles.lookup().findVirtual(impl, name + "IsSet", MethodType.methodType(boolean.class)); } catch (NoSuchMethodException e) { // not optional field return alwaysSet(); } catch (IllegalAccessException e) { throw new IllegalArgumentException("Failed to find IsSet method for " + name, e); } }
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 MethodInvocationHandler createGetterInvocator(Field field, Class<?> convertedPropertyType) throws NoSuchFieldException { try { MethodHandle getter = lookup.unreflectGetter(field); if (isStatic(field.getModifiers())) { return new StaticGetter(field.getName(), getter, convertedPropertyType); } else { return new FieldGetter(field.getName(), getter, convertedPropertyType); } } catch (IllegalAccessException e) { throw new ReflectionFailedException(e); } }
private Object handleDefaultMethod(Object proxy, Method method, Object[] args) throws Throwable { MethodHandles.Lookup lookup = METHOD_LOOKUP_CTOR.newInstance( method.getDeclaringClass(), MethodHandles.Lookup.PRIVATE); return lookup.unreflectSpecial(method, method.getDeclaringClass()) .bindTo(proxy) .invokeWithArguments(args); } }
public MethodHandle staticField(String name) { try { Field field = targetClass.getField(name); return lookup.unreflectGetter(field); } catch (NoSuchFieldException e) { throw new RuntimeException("Could not get static field " + name + " on class " + targetClass.getName(), e); } catch (IllegalAccessException e) { throw new RuntimeException("Could not get static field " + name + " on class " + targetClass.getName(), e); } }
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); } } }
private MethodHandle findConstructor(Class<?> factoryType) { try { return LOOKUP.findConstructor(factoryType, CTOR_METHOD); } catch (NoSuchMethodException | IllegalAccessException e) { throw new RuntimeException(e); } }
MethodType methodType = MethodType.methodType(Integer.class, String.class); MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle handle = lookup.findConstructor( Integer.class, MethodType.methodType(void.class, String.class)); Function<String,Integer> f=(Function<String,Integer>) LambdaMetafactory.metafactory(lookup, "apply", MethodType.methodType(Function.class), methodType.generic(), handle, methodType).getTarget().invokeExact();
public static CallSite bootstrap(MethodHandles.Lookup lookup, String invokedName, MethodType invokedType, String value) throws Throwable { ClassLoader classLoader = lookup.lookupClass().getClassLoader(); MethodType type = MethodType.fromMethodDescriptorString("(Ljava/lang/Object;)Ljava/lang/Object;", classLoader); Class<?> symbolClass = Class.forName("scala.Symbol", false, classLoader); MethodHandle factoryMethod = lookup.findStatic(symbolClass, "apply", type); Object symbolValue = factoryMethod.invokeWithArguments(value); return new ConstantCallSite(MethodHandles.constant(symbolClass, symbolValue)); } }
import java.lang.invoke.MethodHandles; public class Main { private static final Class clazz = MethodHandles.lookup().lookupClass(); private static final String CLASSNAME = clazz.getSimpleName(); public static void main( String args[] ) { System.out.println( CLASSNAME ); } }
public static CallSite bootstrap(Lookup caller, String name, MethodType type, String moduleClass, int arity, int varargs) throws Throwable { Class<?> module = caller.lookupClass().getClassLoader().loadClass(moduleClass); Method function = module.getDeclaredMethod(name, genericMethodType(arity, varargs == 1).parameterArray()); function.setAccessible(true); return new ConstantCallSite(constant( FunctionReference.class, new FunctionReference(caller.unreflect(function), parameterNames(function)))); }
protected static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private Reloadable reloadable; private Supplier<T> configSupplier;
Map map = ((Map) a); metaModel.addAssociation( (Association) lookup.findConstructor( Class.forName((String) map.get(Association.CLASS)), methodType
this.field = field; try { this.getter = MethodHandles.lookup().unreflectGetter(field); this.setter = MethodHandles.lookup().unreflectSetter(field);
mh = lookup.unreflect(method); mh = lookup.unreflectConstructor(constructor); mh = lookup.unreflectSetter(field); mh = lookup.unreflectGetter(field);
import java.lang.invoke.MethodHandles; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class ReflectiveDefaultMethodCallExample { static interface Hello { default String hello() { return "Hello"; } } public static void main(String[] args) throws Throwable{ Hello target = //new Hello(){}; (Hello)Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),new Class[]{Hello.class}, (Object proxy, Method method, Object[] arguments) -> null); Method method = Hello.class.getMethod("hello"); Object result = MethodHandles.lookup() .in(method.getDeclaringClass()) .unreflectSpecial(method,method.getDeclaringClass()) .bindTo(target) .invokeWithArguments(); System.out.println(result); //Hello } }
public Among(String s, int substring_i, int result, String methodname, MethodHandles.Lookup methodobject) { this.s_size = s.length(); this.s = s.toCharArray(); this.substring_i = substring_i; this.result = result; if (methodname.isEmpty()) { this.method = null; } else { final Class<? extends SnowballProgram> clazz = methodobject.lookupClass().asSubclass(SnowballProgram.class); try { this.method = methodobject.findVirtual(clazz, methodname, MethodType.methodType(boolean.class)) .asType(MethodType.methodType(boolean.class, SnowballProgram.class)); } catch (NoSuchMethodException | IllegalAccessException e) { throw new RuntimeException(String.format(Locale.ENGLISH, "Snowball program '%s' is broken, cannot access method: boolean %s()", clazz.getSimpleName(), methodname ), e); } } }
public LambdaFunctionBootstrap getLmabdaBootstrap() { Expression expression = this.getResult(); this.endVisitClass(); byte[] bytes = this.classWriter.toByteArray(); try { Class<?> defineClass = null; if (ClassDefiner.isJDK7()) { defineClass = ClassDefiner.defineClassByClassLoader(className, bytes, classLoader); } else { defineClass = ClassDefiner.defineClass(this.className, LambdaFunction.class, bytes, this.classLoader); } Constructor<?> constructor = defineClass.getConstructor(List.class, Expression.class, Env.class); MethodHandle methodHandle = MethodHandles.lookup().unreflectConstructor(constructor); return new LambdaFunctionBootstrap(this.className, expression, methodHandle, arguments); } catch (Exception e) { throw new CompileExpressionErrorException("define lambda class error", e); } }
/** * Gets a constructor that has the specified types of arguments. * Throw an IllegalStateException if the class does not have such a constructor. */ public static MethodHandle getConstructor(Class<?> clazz, Class<?>... args) { try { // We use unreflect so that we can make the constructor accessible Constructor<?> ctor = clazz.getDeclaredConstructor(args); ctor.setAccessible(true); return MethodHandles.lookup().unreflectConstructor(ctor); } catch (NoSuchMethodException e) { throw new IllegalStateException(clazz.getName() + " has no constructor with args " + Arrays.toString(args), e); } catch (IllegalAccessException e) { throw new IllegalStateException("Problem getting constructor for " + clazz.getName() + " with args " + Arrays.toString(args), e); } }