use of java.lang.invoke.MethodType in project groovy-core by groovy.
the class TypeTransformers method applyUnsharpFilter.
/**
* Apply a transformer as filter.
* The filter may not match exactly in the types. In this case needed
* additional type transformations are done by {@link MethodHandle#asType(MethodType)}
*/
public static MethodHandle applyUnsharpFilter(MethodHandle handle, int pos, MethodHandle transformer) {
MethodType type = transformer.type();
Class given = handle.type().parameterType(pos);
if (type.returnType() != given || type.parameterType(0) != given) {
transformer = transformer.asType(MethodType.methodType(given, type.parameterType(0)));
}
return MethodHandles.filterArguments(handle, pos, transformer);
}
use of java.lang.invoke.MethodType in project gravel by gravel-st.
the class BaseCallSite method getFallbackMethod.
private MethodHandle getFallbackMethod() {
try {
final MethodType fallbackType = MethodType.genericMethodType(type.parameterCount()).insertParameterTypes(0, BaseCallSite.class);
final MethodHandle fallbackHandle = MethodHandles.insertArguments(MethodHandles.lookup().findStatic(BaseCallSite.class, "invocationFallback", fallbackType), 0, this);
return fallbackHandle.asType(type);
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
use of java.lang.invoke.MethodType in project invokebinder by headius.
the class Signature method dropArg.
/**
* Drops the argument at the given index.
*
* @param index the index of the argument to drop
* @return a new signature
*/
public Signature dropArg(int index) {
assert index < argNames.length;
String[] newArgNames = new String[argNames.length - 1];
if (index > 0)
System.arraycopy(argNames, 0, newArgNames, 0, index);
if (index < argNames.length - 1)
System.arraycopy(argNames, index + 1, newArgNames, index, argNames.length - (index + 1));
MethodType newType = methodType.dropParameterTypes(index, index + 1);
return new Signature(newType, newArgNames);
}
use of java.lang.invoke.MethodType in project invokebinder by headius.
the class Signature method dropArg.
/**
* Drops the first argument with the given name.
*
* @param name the name of the argument to drop
* @return a new signature
*/
public Signature dropArg(String name) {
String[] newArgNames = new String[argNames.length - 1];
MethodType newType = methodType;
for (int i = 0, j = 0; i < argNames.length; i++) {
if (argNames[i].equals(name)) {
newType = newType.dropParameterTypes(j, j + 1);
continue;
}
newArgNames[j++] = argNames[i];
}
if (newType == null) {
// arg name not found; should we error?
return this;
}
return new Signature(newType, newArgNames);
}
use of java.lang.invoke.MethodType in project invokebinder by headius.
the class Signature method prependArgs.
/**
* Prepend arguments (names + types) to the signature.
*
* @param names the names of the arguments
* @param types the types of the arguments
* @return a new signature with the added arguments
*/
public Signature prependArgs(String[] names, Class<?>... types) {
String[] newArgNames = new String[argNames.length + names.length];
System.arraycopy(argNames, 0, newArgNames, names.length, argNames.length);
System.arraycopy(names, 0, newArgNames, 0, names.length);
MethodType newMethodType = methodType.insertParameterTypes(0, types);
return new Signature(newMethodType, newArgNames);
}
Aggregations