use of org.codehaus.groovy.reflection.ParameterTypes in project groovy-core by groovy.
the class MetaClassHelper method chooseEmptyMethodParams.
/**
* @param methods the methods to choose from
* @return the method with 1 parameter which takes the most general type of
* object (e.g. Object)
*/
public static Object chooseEmptyMethodParams(FastArray methods) {
Object vargsMethod = null;
final int len = methods.size();
final Object[] data = methods.getArray();
for (int i = 0; i != len; ++i) {
Object method = data[i];
final ParameterTypes pt = (ParameterTypes) method;
CachedClass[] paramTypes = pt.getParameterTypes();
int paramLength = paramTypes.length;
if (paramLength == 0) {
return method;
} else if (paramLength == 1 && pt.isVargsMethod(EMPTY_ARRAY)) {
vargsMethod = method;
}
}
return vargsMethod;
}
use of org.codehaus.groovy.reflection.ParameterTypes in project groovy-core by groovy.
the class MetaClassImpl method chooseMethodInternal.
private Object chooseMethodInternal(String methodName, Object methodOrList, Class[] arguments) {
if (methodOrList instanceof MetaMethod) {
if (((ParameterTypes) methodOrList).isValidMethod(arguments)) {
return methodOrList;
}
return null;
}
FastArray methods = (FastArray) methodOrList;
if (methods == null)
return null;
int methodCount = methods.size();
if (methodCount <= 0) {
return null;
} else if (methodCount == 1) {
Object method = methods.get(0);
if (((ParameterTypes) method).isValidMethod(arguments)) {
return method;
}
return null;
}
Object answer;
if (arguments == null || arguments.length == 0) {
answer = MetaClassHelper.chooseEmptyMethodParams(methods);
} else {
Object matchingMethods = null;
final int len = methods.size;
Object[] data = methods.getArray();
for (int i = 0; i != len; ++i) {
Object method = data[i];
// making this false helps find matches
if (((ParameterTypes) method).isValidMethod(arguments)) {
if (matchingMethods == null)
matchingMethods = method;
else if (matchingMethods instanceof ArrayList)
((ArrayList) matchingMethods).add(method);
else {
List arr = new ArrayList(4);
arr.add(matchingMethods);
arr.add(method);
matchingMethods = arr;
}
}
}
if (matchingMethods == null) {
return null;
} else if (!(matchingMethods instanceof ArrayList)) {
return matchingMethods;
}
return chooseMostSpecificParams(methodName, (List) matchingMethods, arguments);
}
if (answer != null) {
return answer;
}
throw new MethodSelectionException(methodName, methods, arguments);
}
use of org.codehaus.groovy.reflection.ParameterTypes in project groovy-core by groovy.
the class MetaClassImpl method chooseMostSpecificParams.
private Object chooseMostSpecificParams(String name, List matchingMethods, Class[] arguments) {
long matchesDistance = -1;
LinkedList matches = new LinkedList();
for (Object method : matchingMethods) {
ParameterTypes paramTypes = (ParameterTypes) method;
long dist = MetaClassHelper.calculateParameterDistance(arguments, paramTypes);
if (dist == 0)
return method;
if (matches.size() == 0) {
matches.add(method);
matchesDistance = dist;
} else if (dist < matchesDistance) {
matchesDistance = dist;
matches.clear();
matches.add(method);
} else if (dist == matchesDistance) {
matches.add(method);
}
}
if (matches.size() == 1) {
return matches.getFirst();
}
if (matches.size() == 0) {
return null;
}
//more than one matching method found --> ambiguous!
StringBuilder msg = new StringBuilder("Ambiguous method overloading for method ");
msg.append(theClass.getName()).append("#").append(name).append(".\nCannot resolve which method to invoke for ").append(InvokerHelper.toString(arguments)).append(" due to overlapping prototypes between:");
for (final Object matche : matches) {
Class[] types = ((ParameterTypes) matche).getNativeParameterTypes();
msg.append("\n\t").append(InvokerHelper.toString(types));
}
throw new GroovyRuntimeException(msg.toString());
}
Aggregations