use of java.lang.invoke.MethodType in project gravel by gravel-st.
the class JavaSystemMappingCompilerTools method runAstInit_.
@Override
public SystemMappingCompilerTools runAstInit_(JVMClass jvmClass) {
JVMDefinedObjectType key = jvmClass.type();
CompiledClass current = jvmClasses.get(key);
Object[] astConstants = jvmClass.astConstants();
if (astConstants.length == 0)
return this;
MethodType type = MethodType.genericMethodType(astConstants.length).changeReturnType(void.class);
MethodHandle findStatic;
try {
findStatic = MethodHandles.lookup().findStatic(current.javaClass, "_astinit", type);
findStatic.invokeWithArguments(astConstants);
} catch (Throwable e) {
throw new RuntimeException(e);
}
return this;
}
use of java.lang.invoke.MethodType in project invokebinder by headius.
the class Signature method spread.
/**
* Spread the trailing [] argument into its component type assigning given names.
*
* @param names names to use for the decomposed arguments
* @param types types to use for the decomposed arguments
* @return a new signature with decomposed arguments in place of the trailing array
*/
public Signature spread(String[] names, Class<?>... types) {
assert names.length == types.length : "names and types must be of the same length";
String[] newArgNames = new String[argNames.length - 1 + names.length];
System.arraycopy(names, 0, newArgNames, newArgNames.length - names.length, names.length);
System.arraycopy(argNames, 0, newArgNames, 0, argNames.length - 1);
MethodType newMethodType = methodType.dropParameterTypes(methodType.parameterCount() - 1, methodType.parameterCount()).appendParameterTypes(types);
return new Signature(newMethodType, newArgNames);
}
use of java.lang.invoke.MethodType in project invokebinder by headius.
the class Signature method appendArg.
/**
* Append an argument (name + type) to the signature.
*
* @param name the name of the argument
* @param type the type of the argument
* @return a new signature with the added arguments
*/
public Signature appendArg(String name, Class<?> type) {
String[] newArgNames = new String[argNames.length + 1];
System.arraycopy(argNames, 0, newArgNames, 0, argNames.length);
newArgNames[argNames.length] = name;
MethodType newMethodType = methodType.appendParameterTypes(type);
return new Signature(newMethodType, newArgNames);
}
use of java.lang.invoke.MethodType in project es6draft by anba.
the class Properties method getStaticMethodHandle.
private static MethodHandle getStaticMethodHandle(Lookup lookup, Method method) throws IllegalAccessException {
MethodHandle handle = lookup.unreflect(method);
MethodType type = handle.type();
Class<?>[] params = type.parameterArray();
int p = 0, pcount = type.parameterCount();
boolean callerContext = false;
// First three parameters are (ExecutionContext, ExecutionContext?, Object=ThisValue)
if (!(p < pcount && ExecutionContext.class.equals(params[p++]))) {
throw new IllegalArgumentException(type.toString());
}
if (p < pcount && ExecutionContext.class.equals(params[p])) {
callerContext = true;
p++;
}
if (!(p < pcount && Object.class.equals(params[p++]))) {
throw new IllegalArgumentException(type.toString());
}
// Always required to return Object (for now at least)
if (!Object.class.equals(type.returnType())) {
throw new IllegalArgumentException(type.toString());
}
// Collect remaining arguments into Object[]
if (!(p + 1 == pcount && Object[].class.equals(params[p]))) {
final int fixedArguments = callerContext ? 3 : 2;
final boolean varargs = handle.isVarargsCollector();
// Otherwise all trailing arguments need to be of type Object or Object[]
for (; p < pcount; ++p) {
if (Object.class.equals(params[p])) {
continue;
}
if (p + 1 == pcount && Object[].class.equals(params[p]) && varargs) {
continue;
}
throw new IllegalArgumentException(type.toString());
}
// Trailing Object[] arguments are no longer spread in var-args methods (jdk8u40, jdk9).
if (varargs) {
handle = handle.asFixedArity();
}
// Convert to (ExecutionContext, Object, ...) -> Object handle
handle = toCanonical(handle, fixedArguments, varargs, method);
}
if (!callerContext) {
handle = MethodHandles.dropArguments(handle, 1, ExecutionContext.class);
}
return handle;
}
use of java.lang.invoke.MethodType in project es6draft by anba.
the class Properties method getComputedValueMethodHandle.
private static MethodHandle getComputedValueMethodHandle(Lookup lookup, Method method) throws IllegalAccessException {
// check: (ExecutionContext) -> Object
MethodHandle handle = lookup.unreflect(method);
MethodType type = handle.type();
if (type.parameterCount() != 1 || !ExecutionContext.class.equals(type.parameterType(0))) {
throw new IllegalArgumentException(handle.toString());
}
if (!Object.class.equals(type.returnType())) {
throw new IllegalArgumentException(handle.toString());
}
return handle;
}
Aggregations