use of java.lang.invoke.MethodHandle in project groovy by apache.
the class TypeTransformers method createSAMTransform.
/**
* creates a method handle able to transform the given Closure into a SAM type
* if the given parameter is a SAM type
*/
private static MethodHandle createSAMTransform(Object arg, Class parameter) {
Method method = CachedSAMClass.getSAMMethod(parameter);
if (method == null)
return null;
// TODO: have to think about how to optimize this!
if (parameter.isInterface()) {
if (Traits.isTrait(parameter)) {
// the following code will basically do this:
// Map<String,Closure> impl = Collections.singletonMap(method.getName(),arg);
// return ProxyGenerator.INSTANCE.instantiateAggregate(impl,Collections.singletonList(clazz));
// TO_SAMTRAIT_PROXY is a handle (Object,Object,ProxyGenerator,Class)GroovyObject
// where the second object is the input closure, everything else
// needs to be provide and is in remaining order: method name,
// ProxyGenerator.INSTANCE and singletonList(parameter)
MethodHandle ret = TO_SAMTRAIT_PROXY;
ret = MethodHandles.insertArguments(ret, 2, ProxyGenerator.INSTANCE, Collections.singletonList(parameter));
ret = MethodHandles.insertArguments(ret, 0, method.getName());
return ret;
}
// the following code will basically do this:
// return Proxy.newProxyInstance(
// arg.getClass().getClassLoader(),
// new Class[]{parameter},
// new ConvertedClosure((Closure) arg));
// TO_REFLECTIVE_PROXY will do that for us, though
// input is the closure, the method name, the class loader and the
// class[]. All of that but the closure must be provided here
MethodHandle ret = TO_REFLECTIVE_PROXY;
ret = MethodHandles.insertArguments(ret, 1, method.getName(), arg.getClass().getClassLoader(), new Class[] { parameter });
return ret;
} else {
// the following code will basically do this:
//Map<String, Object> m = Collections.singletonMap(method.getName(), arg);
//return ProxyGenerator.INSTANCE.
// instantiateAggregateFromBaseClass(m, parameter);
// TO_GENERATED_PROXY is a handle (Object,Object,ProxyGenerator,Class)GroovyObject
// where the second object is the input closure, everything else
// needs to be provide and is in remaining order: method name,
// ProxyGenerator.INSTANCE and parameter
MethodHandle ret = TO_GENERATED_PROXY;
ret = MethodHandles.insertArguments(ret, 2, ProxyGenerator.INSTANCE, parameter);
ret = MethodHandles.insertArguments(ret, 0, method.getName());
return ret;
}
}
use of java.lang.invoke.MethodHandle in project groovy by apache.
the class IndyArrayAccess method buildGetter.
private static MethodHandle buildGetter(Class arrayClass) {
MethodHandle get = MethodHandles.arrayElementGetter(arrayClass);
MethodHandle fallback = MethodHandles.explicitCastArguments(get, get.type().changeParameterType(0, Object.class));
fallback = MethodHandles.dropArguments(fallback, 2, int.class);
MethodType reorderType = fallback.type().insertParameterTypes(0, int.class).dropParameterTypes(2, 3);
fallback = MethodHandles.permuteArguments(fallback, reorderType, 1, 0, 0);
fallback = MethodHandles.foldArguments(fallback, normalizeIndex);
fallback = MethodHandles.explicitCastArguments(fallback, get.type());
MethodHandle guard = MethodHandles.dropArguments(notNegative, 0, arrayClass);
MethodHandle handle = MethodHandles.guardWithTest(guard, get, fallback);
return handle;
}
use of java.lang.invoke.MethodHandle in project groovy by apache.
the class IndyArrayAccess method buildSetter.
private static MethodHandle buildSetter(Class arrayClass) {
MethodHandle set = MethodHandles.arrayElementSetter(arrayClass);
MethodHandle fallback = MethodHandles.explicitCastArguments(set, set.type().changeParameterType(0, Object.class));
fallback = MethodHandles.dropArguments(fallback, 3, int.class);
MethodType reorderType = fallback.type().insertParameterTypes(0, int.class).dropParameterTypes(4, 5);
fallback = MethodHandles.permuteArguments(fallback, reorderType, 1, 0, 3, 0);
fallback = MethodHandles.foldArguments(fallback, normalizeIndex);
fallback = MethodHandles.explicitCastArguments(fallback, set.type());
MethodHandle guard = MethodHandles.dropArguments(notNegative, 0, arrayClass);
MethodHandle handle = MethodHandles.guardWithTest(guard, set, fallback);
return handle;
}
use of java.lang.invoke.MethodHandle in project groovy by apache.
the class IndyArrayAccess method arraySet.
public static MethodHandle arraySet(MethodType type) {
Class key = type.parameterType(0);
MethodHandle res = setterMap.get(key);
if (res != null)
return res;
res = buildSetter(key);
res = MethodHandles.explicitCastArguments(res, type);
return res;
}
use of java.lang.invoke.MethodHandle in project groovy by apache.
the class ReevaluatingReference method replacePayLoad.
private T replacePayLoad() {
T payload = valueSupplier.get();
MethodHandle ref = MethodHandles.constant(clazzRef.get(), payload);
SwitchPoint sp = validationSupplier.apply(payload);
returnRef = sp.guardWithTest(ref, FALLBACK_HANDLE);
return payload;
}
Aggregations