use of java.lang.invoke.MethodType in project quasar by puniverse.
the class DynamicMethodHandleRecord method fixMethodHandleType.
private static MethodHandle fixMethodHandleType(Field field, MethodHandle mh) throws IllegalAccessException {
if (mh == null)
return null;
final MethodType origType = mh.type();
final Class<?>[] params = origType.parameterArray();
params[0] = Object.class;
for (int i = 1; i < params.length; i++) {
if (!params[i].isPrimitive())
params[i] = Object.class;
}
Class<?> rtype = origType.returnType();
if (field instanceof Field.ArrayField && rtype.isArray()) {
if (!rtype.getComponentType().isPrimitive())
rtype = Object[].class;
} else if (!rtype.isPrimitive())
rtype = Object.class;
final MethodType mt = MethodType.methodType(rtype, params);
return mh.asType(mt);
}
use of java.lang.invoke.MethodType in project es6draft by anba.
the class NativeCalls method getNativeMethodHandle.
private static MethodHandle getNativeMethodHandle(String name) {
if (!name.startsWith("native:")) {
throw new IllegalArgumentException();
}
String methodName = name.substring("native:".length());
for (Class<?> lookupClass : lookupClasses) {
MethodLookup lookup = new MethodLookup(MethodHandles.publicLookup().in(lookupClass));
Method m = findMethod(lookup, methodName);
if (m == null) {
continue;
}
MethodHandle mh;
try {
mh = lookup.getLookup().unreflect(m);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException();
}
// Allow to omit execution context argument.
MethodType type = mh.type();
if (type.parameterCount() == 0 || !type.parameterType(0).equals(ExecutionContext.class)) {
mh = MethodHandles.dropArguments(mh, 0, ExecutionContext.class);
}
// Allow void return type.
if (type.returnType() == void.class) {
mh = MethodHandles.filterReturnValue(mh, MethodHandles.constant(Object.class, UNDEFINED));
}
return mh;
}
throw new IllegalArgumentException();
}
use of java.lang.invoke.MethodType in project es6draft by anba.
the class Properties method toCanonical.
private static MethodHandle toCanonical(MethodHandle handle, int fixedArguments, boolean varargs, Method method) {
assert !handle.isVarargsCollector();
MethodType type = handle.type();
int actual = type.parameterCount() - fixedArguments - (varargs ? 1 : 0);
Object[] defaults = method != null ? methodDefaults(method, fixedArguments, actual) : null;
MethodHandle filter = Parameters.filter(actual, varargs, defaults);
MethodHandle spreader = MethodHandles.spreadInvoker(type, fixedArguments);
spreader = MethodHandles.insertArguments(spreader, 0, handle);
spreader = MethodHandles.filterArguments(spreader, fixedArguments, filter);
return spreader;
}
use of java.lang.invoke.MethodType in project groovy-core by groovy.
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.MethodType in project groovy-core by groovy.
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;
}
Aggregations