public final Object[] coerceArgumentsToClasses(Object[] argumentArray) { // Uncomment if at some point this method can be called before parameterTypes initialized // getParameterTypes(); argumentArray = correctArguments(argumentArray); final CachedClass[] pt = parameterTypes; final int len = argumentArray.length; for (int i = 0; i < len; i++) { final Object argument = argumentArray[i]; if (argument != null) { argumentArray[i] = pt[i].coerceArgument(argument); } } return argumentArray; }
public Object[] correctArguments(Object[] argumentArray) { // correct argumentArray's length if (argumentArray == null) { return MetaClassHelper.EMPTY_ARRAY; } final CachedClass[] pt = getParameterTypes(); if (pt.length == 1 && argumentArray.length == 0) { if (isVargsMethod) return new Object[]{Array.newInstance(pt[0].getTheClass().getComponentType(), 0)}; else return MetaClassHelper.ARRAY_WITH_NULL; } if (isVargsMethod && isVargsMethod(argumentArray)) { return fitToVargs(argumentArray, pt); } return argumentArray; }
CachedClass[] paramTypes = getParameterTypes(); final int paramMinus1 = paramTypes.length - 1; if (paramTypes[i].isAssignableFrom(getArgClass(arguments[i]))) continue; return false; Class clazz = varg.getTheClass().getComponentType(); if (size == paramTypes.length && (varg.isAssignableFrom(getArgClass(arguments[paramMinus1])) || testComponentAssignable(clazz, getArgClass(arguments[paramMinus1])))) { return true; if (MetaClassHelper.isAssignableFrom(clazz, getArgClass(arguments[i]))) continue; return false; if (paramTypes[i].isAssignableFrom(getArgClass(arguments[i]))) continue; return false;
public boolean isValidMethod(Class[] arguments) { if (arguments == null) return true; final int size = arguments.length; CachedClass[] pt = getParameterTypes(); final int paramMinus1 = pt.length - 1; if (isVargsMethod && size >= paramMinus1) return isValidVarargsMethod(arguments, size, pt, paramMinus1); else if (pt.length == size) return isValidExactMethod(arguments, pt); else if (pt.length == 1 && size == 0 && !pt[0].isPrimitive) return true; return false; }
private synchronized void getParametersTypes0() { if (parameterTypes != null) return; Class[] npt = nativeParamTypes == null ? getPT() : nativeParamTypes; if (npt.length == 0) { nativeParamTypes = NO_PARAMETERS; setParametersTypes(CachedClass.EMPTY_ARRAY); } else { CachedClass[] pt = new CachedClass[npt.length]; for (int i = 0; i != npt.length; ++i) pt[i] = ReflectionCache.getCachedClass(npt[i]); nativeParamTypes = npt; setParametersTypes(pt); } }
/** * @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; }
Object method = iter.next(); final ParameterTypes parameterTypes = (ParameterTypes) method; Class[] paramTypes = parameterTypes.getNativeParameterTypes(); if (!MetaClassHelper.parametersAreCompatible(arguments, paramTypes)) continue; long dist = MetaClassHelper.calculateParameterDistance(arguments, parameterTypes); msg += " due to overlapping prototypes between:"; for (Iterator iter = matches.iterator(); iter.hasNext();) { CachedClass[] types = ((ParameterTypes) iter.next()).getParameterTypes(); msg += "\n\t" + InvokerHelper.toString(types);
public Object[] correctArguments(Object[] argumentArray) { // correct argumentArray's length if (argumentArray == null) { return MetaClassHelper.EMPTY_ARRAY; } final CachedClass[] pt = parameterTypes; if (pt.length == 1 && argumentArray.length == 0) { if (isVargsMethod) return new Object[]{Array.newInstance(pt[0].getTheClass().getComponentType(), 0)}; else return MetaClassHelper.ARRAY_WITH_NULL; } if (isVargsMethod && isVargsMethod(argumentArray)) { return fitToVargs(argumentArray, pt); } return argumentArray; }
static boolean noCoerce(ParameterTypes metaMethod, Object[] args) { final CachedClass[] paramClasses = metaMethod.getParameterTypes(); if (paramClasses.length != args.length) return false; for (int i = 0; i < paramClasses.length; i++) { CachedClass paramClass = paramClasses[i]; if (args[i] != null && !paramClass.isDirectlyAssignable(args[i])) return true; } return false; }
public CachedClass[] getParameterTypes() { if (parameterTypes == null) { getParametersTypes0(); } return parameterTypes; }
private Object chooseMethodInternal(String methodName, Object methodOrList, Class[] arguments) { if (methodOrList instanceof MetaMethod) { if (((ParameterTypes) methodOrList).isValidMethod(arguments)) { return methodOrList; } else if (methodCount == 1) { Object method = methods.get(0); if (((ParameterTypes) method).isValidMethod(arguments)) { return method; if (((ParameterTypes) method).isValidMethod(arguments)) { if (matchingMethods == null) matchingMethods = method;
private synchronized void getNativeParameterTypes0() { if (nativeParamTypes != null) return; Class[] npt; if (parameterTypes != null) { npt = new Class[parameterTypes.length]; for (int i = 0; i != parameterTypes.length; ++i) { npt[i] = parameterTypes[i].getTheClass(); } } else npt = getPT(); nativeParamTypes = npt; }
protected static Object doChooseMostSpecificParams(String theClassName, String name, List matchingMethods, Class[] arguments, boolean checkParametersCompatible) { long matchesDistance = -1; LinkedList matches = new LinkedList(); for (Object method : matchingMethods) { final ParameterTypes parameterTypes = (ParameterTypes) method; if (checkParametersCompatible && !MetaClassHelper.parametersAreCompatible(arguments, parameterTypes.getNativeParameterTypes())) continue; long dist = MetaClassHelper.calculateParameterDistance(arguments, parameterTypes); if (dist == 0) return method; matchesDistance = handleMatches(matchesDistance, matches, method, dist); } int size = matches.size(); if (1 == size) { return matches.getFirst(); } if (0 == size) { return null; } //more than one matching method found --> ambiguous! throw new GroovyRuntimeException(createErrorMessageForAmbiguity(theClassName, name, arguments, matches)); }
public Class[] getNativeParameterTypes() { if (nativeParamTypes == null) { getNativeParameterTypes0(); } return nativeParamTypes; }
public boolean isValidMethod(Class[] arguments) { if (arguments == null) return true; final int size = arguments.length; CachedClass[] pt = getParameterTypes(); final int paramMinus1 = pt.length-1; if (isVargsMethod && size >= paramMinus1) return isValidVarargsMethod(arguments, size, pt, paramMinus1); else if (pt.length == size) return isValidExactMethod(arguments, pt); else if (pt.length == 1 && size == 0) return true; return false; }
Object method = data[i]; final ParameterTypes pt = (ParameterTypes) method; CachedClass[] paramTypes = pt.getParameterTypes(); int paramLength = paramTypes.length; if (paramLength == 0 || paramLength > 2) continue; if (!pt.isVargsMethod(ARRAY_WITH_NULL)) continue; if (closestClass == null) { closestVargsClass = paramTypes[1];
private synchronized void getParametersTypes0() { if (parameterTypes != null) return; Class [] npt = nativeParamTypes == null ? getPT() : nativeParamTypes; CachedClass[] pt = new CachedClass [npt.length]; for (int i = 0; i != npt.length; ++i) pt[i] = ReflectionCache.getCachedClass(npt[i]); nativeParamTypes = npt; setParametersTypes(pt); }
Object method = iter.next(); final ParameterTypes parameterTypes = (ParameterTypes) method; Class[] paramTypes = parameterTypes.getNativeParameterTypes(); if (!MetaClassHelper.parametersAreCompatible(arguments, paramTypes)) continue; long dist = MetaClassHelper.calculateParameterDistance(arguments, parameterTypes); msg += " due to overlapping prototypes between:"; for (Object match : matches) { CachedClass[] types = ((ParameterTypes) match).getParameterTypes(); msg += "\n\t" + InvokerHelper.toString(types);
private Object[] correctArguments(Object[] argumentArray) { // correct argumentArray's length if (argumentArray == null) { return MetaClassHelper.EMPTY_ARRAY; } final CachedClass[] pt = parameterTypes; if (pt.length == 1 && argumentArray.length == 0) { if (isVargsMethod) return new Object[]{Array.newInstance(pt[0].getTheClass().getComponentType(), 0)}; else return MetaClassHelper.ARRAY_WITH_NULL; } if (isVargsMethod && isVargsMethod(argumentArray)) { return fitToVargs(argumentArray, pt); } return argumentArray; }
protected static String createErrorMessageForAmbiguity(String theClassName, String name, Class[] arguments, LinkedList matches) { StringBuilder msg = new StringBuilder("Ambiguous method overloading for method "); msg.append(theClassName).append("#").append(name) .append(".\nCannot resolve which method to invoke for ") .append(InvokerHelper.toString(arguments)) .append(" due to overlapping prototypes between:"); for (final Object match : matches) { CachedClass[] types = ((ParameterTypes) match).getParameterTypes(); msg.append("\n\t").append(InvokerHelper.toString(types)); } return msg.toString(); }