use of java.lang.invoke.MethodType in project invokebinder by headius.
the class Signature method insertArgs.
/**
* Insert arguments (names + types) into the signature.
*
* @param index the index at which to insert
* @param names the names of the new arguments
* @param types the types of the new arguments
* @return a new signature with the added arguments
*/
public Signature insertArgs(int index, String[] names, Class<?>... types) {
assert names.length == types.length : "names and types must be of the same length";
String[] newArgNames = new String[argNames.length + names.length];
System.arraycopy(names, 0, newArgNames, index, names.length);
if (index != 0)
System.arraycopy(argNames, 0, newArgNames, 0, index);
if (argNames.length - index != 0)
System.arraycopy(argNames, index, newArgNames, index + names.length, argNames.length - index);
MethodType newMethodType = methodType.insertParameterTypes(index, types);
return new Signature(newMethodType, newArgNames);
}
use of java.lang.invoke.MethodType in project invokebinder by headius.
the class Signature method prependArg.
/**
* Prepend 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 prependArg(String name, Class<?> type) {
String[] newArgNames = new String[argNames.length + 1];
System.arraycopy(argNames, 0, newArgNames, 1, argNames.length);
newArgNames[0] = name;
MethodType newMethodType = methodType.insertParameterTypes(0, type);
return new Signature(newMethodType, newArgNames);
}
use of java.lang.invoke.MethodType in project invokebinder by headius.
the class Signature method replaceArg.
/**
* Replace the named argument with a new name and type.
*
* @param oldName the old name of the argument
* @param newName the new name of the argument; can be the same as old
* @param newType the new type of the argument; can be the same as old
* @return a new signature with the modified argument
*/
public Signature replaceArg(String oldName, String newName, Class<?> newType) {
int offset = argOffset(oldName);
String[] newArgNames = argNames;
if (!oldName.equals(newName)) {
newArgNames = Arrays.copyOf(argNames, argNames.length);
newArgNames[offset] = newName;
}
Class<?> oldType = methodType.parameterType(offset);
MethodType newMethodType = methodType;
if (!oldType.equals(newType))
newMethodType = methodType.changeParameterType(offset, newType);
return new Signature(newMethodType, newArgNames);
}
use of java.lang.invoke.MethodType in project invokebinder by headius.
the class Signature method collect.
/**
* Collect sequential arguments matching pattern into an array. They must have the same type.
*
* @param newName the name of the new array argument
* @param oldPattern the pattern of arguments to collect
* @return a new signature with an array argument where the collected arguments were
*/
public Signature collect(String newName, String oldPattern) {
int start = -1;
int newCount = 0;
int gatherCount = 0;
Class<?> type = null;
Pattern pattern = Pattern.compile(oldPattern);
MethodType newType = type();
for (int i = 0; i < argNames.length; i++) {
if (pattern.matcher(argName(i)).matches()) {
gatherCount++;
newType = newType.dropParameterTypes(newCount, newCount + 1);
Class<?> argType = argType(i);
if (start == -1)
start = i;
if (type == null) {
type = argType;
} else {
if (argType != type) {
throw new InvalidTransformException("arguments matching " + pattern + " are not all of the same type");
}
}
} else {
newCount++;
}
}
if (start != -1) {
String[] newNames = new String[newCount + 1];
// pre
System.arraycopy(argNames, 0, newNames, 0, start);
// vararg
newNames[start] = newName;
newType = newType.insertParameterTypes(start, Array.newInstance(type, 0).getClass());
// post
if (newCount + 1 > start) {
// args not at end
System.arraycopy(argNames, start + gatherCount, newNames, start + 1, newCount - start);
}
return new Signature(newType, newNames);
}
return this;
}
use of java.lang.invoke.MethodType in project invokebinder by headius.
the class Signature method appendArgs.
/**
* Append an argument (name + type) to the signature.
*
* @param names the names of the arguments
* @param types the types of the argument
* @return a new signature with the added arguments
*/
public Signature appendArgs(String[] names, Class<?>... types) {
assert names.length == types.length : "names and types must be of the same length";
String[] newArgNames = new String[argNames.length + names.length];
System.arraycopy(argNames, 0, newArgNames, 0, argNames.length);
System.arraycopy(names, 0, newArgNames, argNames.length, names.length);
MethodType newMethodType = methodType.appendParameterTypes(types);
return new Signature(newMethodType, newArgNames);
}
Aggregations