/** * Capitalizes the start of the given bean property name */ public static String capitalize(String name) { return MetaClassHelper.capitalize(name); }
@Override public MetaMethod retrieveConstructor(Object[] args) { Class[] params = MetaClassHelper.convertToTypeArray(args); MetaMethod method = pickMethod(GROOVY_CONSTRUCTOR, params); if (method!=null) return method; return super.retrieveConstructor(args); }
public static void logMethodCall(Object object, String methodName, Object[] arguments) { String className = getClassName(object); String logname = "methodCalls." + className + "." + methodName; Logger objLog = Logger.getLogger(logname); if (!objLog.isLoggable(Level.FINER)) return; StringBuilder msg = new StringBuilder(methodName); msg.append("("); if (arguments != null) { for (int i = 0; i < arguments.length;) { msg.append(normalizedValue(arguments[i])); if (++i < arguments.length) { msg.append(","); } } } msg.append(")"); objLog.logp(Level.FINER, className, msg.toString(), "called from MetaClass.invokeMethod"); }
private MetaMethod getNormalMethodWithCaching(Object[] arguments, MetaMethodIndex.Entry e) { MetaMethodIndex.CacheEntry cacheEntry; final Object methods = e.methods; if (methods == null) return null; cacheEntry = e.cachedMethod; if (cacheEntry != null && MetaClassHelper.sameClasses(cacheEntry.params, arguments, methods instanceof MetaMethod)) { MetaMethod method = cacheEntry.method; if (method!=null) return method; } final Class[] classes = MetaClassHelper.convertToTypeArray(arguments); cacheEntry = new MetaMethodIndex.CacheEntry (classes, (MetaMethod) chooseMethod(e.name, methods, classes)); e.cachedMethod = cacheEntry; return cacheEntry.method; }
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 Object chooseMethod(Class[] arguments, boolean coerce) { if (arguments.length == 0) { return MetaClassHelper.chooseEmptyMethodParams(methods); } else if (arguments.length == 1 && arguments[0] == null) { return MetaClassHelper.chooseMostGeneralMethodWith1NullParam(methods); } else { List matchingMethods = new ArrayList(); final Object[] data = methods.getArray(); for (int i = 0, len = methods.size(); i != len; ++i) { Object method = data[i]; // making this false helps find matches if (((ParameterTypes) method).isValidMethod(arguments)) { matchingMethods.add(method); } } int size = matchingMethods.size(); if (0 == size) { return null; } else if (1 == size) { return matchingMethods.get(0); } return chooseMostSpecificParams(CLOSURE_DO_CALL_METHOD, matchingMethods, arguments); } }
public static boolean parametersAreCompatible(Class[] arguments, Class[] parameters) { if (arguments.length != parameters.length) return false; for (int i = 0; i < arguments.length; i++) { if (!isAssignableFrom(parameters[i], arguments[i])) return false; } return true; }
int dist = getMaximumInterfaceDistance(argument, parameter.getTheClass()) << INTERFACE_SHIFT; if (dist>-1 || !(argument!=null && Closure.class.isAssignableFrom(argument))) { return dist; long pd = getPrimitiveDistance(parameter.getTheClass(), argument); if (pd != -1) return pd << PRIMITIVE_SHIFT;
private CachedConstructor createCachedConstructor(Object[] arguments) { if (arguments == null) arguments = EMPTY_ARGUMENTS; Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments); MetaClassHelper.unwrap(arguments); CachedConstructor constructor = (CachedConstructor) chooseMethod("<init>", constructors, argClasses); if (constructor == null) { constructor = (CachedConstructor) chooseMethod("<init>", constructors, argClasses); } if (constructor == null) { throw new GroovyRuntimeException( "Could not find matching constructor for: " + theClass.getName() + "(" + InvokerHelper.toTypeString(arguments) + ")"); } return constructor; }
public Object chooseMethod(Class[] arguments, boolean coerce) { if (arguments.length == 0) { return MetaClassHelper.chooseEmptyMethodParams(methods); } else if (arguments.length == 1 && arguments[0] == null) { return MetaClassHelper.chooseMostGeneralMethodWith1NullParam(methods); } else { List matchingMethods = new ArrayList(); final int len = methods.size(); final Object[] data = methods.getArray(); for (int i = 0; i != len; ++i) { Object method = data[i]; // making this false helps find matches if (MetaClassHelper.isValidMethod(method, arguments)) { matchingMethods.add(method); } } if (matchingMethods.isEmpty()) { return null; } else if (matchingMethods.size() == 1) { return matchingMethods.get(0); } return chooseMostSpecificParams(CLOSURE_DO_CALL_METHOD, matchingMethods, arguments); } }
protected static String shortName(Object object) { if (object == null || object.getClass() == null) return "unknownClass"; String name = getClassName(object); if (name == null) return "unknownClassName"; // *very* defensive... int lastDotPos = name.lastIndexOf('.'); if (lastDotPos < 0 || lastDotPos >= name.length() - 1) return name; return name.substring(lastDotPos + 1); }
ret += calculateParameterDistance(arguments[i], parameters[i]); ret += calculateParameterDistance(arguments[noVargsLength], baseType); } else if (arguments.length > parameters.length) { ret += calculateParameterDistance(arguments[i], vargsType);
/** * This method is called when an exception occurs while invoking this method. */ public final RuntimeException processDoMethodInvokeException (Exception e, Object object, Object [] argumentArray) { // if (e instanceof IllegalArgumentException) { // //TODO: test if this is OK with new MOP, should be changed! // // we don't want the exception being unwrapped if it is a IllegalArgumentException // // but in the case it is for example a IllegalThreadStateException, we want the unwrapping // // from the runtime // //Note: the reason we want unwrapping sometimes and sometimes not is that the method // // invocation tries to invoke the method with and then reacts with type transformation // // if the invocation failed here. This is OK for IllegalArgumentException, but it is // // possible that a Reflector will be used to execute the call and then an Exception from inside // // the method is not wrapped in a InvocationTargetException and we will end here. // boolean setReason = e.getClass() != IllegalArgumentException.class || this instanceof org.codehaus.groovy.reflection.GeneratedMetaMethod; // return MetaClassHelper.createExceptionText("failed to invoke method: ", this, object, argumentArray, e, setReason); // } if (e instanceof RuntimeException) return (RuntimeException) e; return MetaClassHelper.createExceptionText("failed to invoke method: ", this, object, argumentArray, e, true); }
private static int getMaximumInterfaceDistance(Class c, Class interfaceClass) { // -1 means a mismatch if (c == null) return -1; // 0 means a direct match if (c == interfaceClass) return 0; Class[] interfaces = c.getInterfaces(); int max = -1; for (Class anInterface : interfaces) { int sub = getMaximumInterfaceDistance(anInterface, interfaceClass); // we need to keep the -1 to track the mismatch, a +1 // by any means could let it look like a direct match // we want to add one, because there is an interface between // the interface we search for and the interface we are in. if (sub != -1) sub++; // we are interested in the longest path only max = Math.max(max, sub); } // we do not add one for super classes, only for interfaces int superClassMax = getMaximumInterfaceDistance(c.getSuperclass(), interfaceClass); if (superClassMax != -1) superClassMax++; return Math.max(max, superClassMax); }
accessible = at.getName().equals(constructor.getName()); } else if (Modifier.isProtected(modifiers)) { Boolean isAccessible = checkCompatiblePackages(at, constructor); if (isAccessible != null) { accessible = isAccessible; Boolean isAccessible = checkCompatiblePackages(at, constructor); if (isAccessible != null) { accessible = isAccessible;
for (Iterator iter = matchingMethods.iterator(); iter.hasNext();) { Object method = iter.next(); final ParameterTypes parameterTypes = MetaClassHelper.getParameterTypes(method); Class[] paramTypes = parameterTypes.getNativeParameterTypes(); if (!MetaClassHelper.parametersAreCompatible(arguments, paramTypes)) continue; long dist = MetaClassHelper.calculateParameterDistance(arguments, parameterTypes); if (dist == 0) return method; if (matches.isEmpty()) { msg += " due to overlapping prototypes between:"; for (Iterator iter = matches.iterator(); iter.hasNext();) { CachedClass[] types = MetaClassHelper.getParameterTypes(iter.next()).getParameterTypes(); msg += "\n\t" + InvokerHelper.toString(types);
answer = MetaClassHelper.chooseEmptyMethodParams(methods); } else { Object matchingMethods = null;
for (Iterator iter = matchingMethods.iterator(); iter.hasNext();) { Object method = iter.next(); ParameterTypes paramTypes = MetaClassHelper.getParameterTypes(method); long dist = MetaClassHelper.calculateParameterDistance(arguments, paramTypes); if (dist == 0) return method; if (matches.size() == 0) { msg += " due to overlapping prototypes between:"; for (Iterator iter = matches.iterator(); iter.hasNext();) { Class[] types = MetaClassHelper.getParameterTypes(iter.next()).getNativeParameterTypes(); msg += "\n\t" + InvokerHelper.toString(types);
public Object chooseMethod(Class[] arguments, boolean coerce) { if (arguments.length == 0) { return MetaClassHelper.chooseEmptyMethodParams(methods); } else if (arguments.length == 1 && arguments[0] == null) { return MetaClassHelper.chooseMostGeneralMethodWith1NullParam(methods); } else { List matchingMethods = new ArrayList(); final int len = methods.size(); final 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)) { matchingMethods.add(method); } } if (matchingMethods.isEmpty()) { return null; } else if (matchingMethods.size() == 1) { return matchingMethods.get(0); } return chooseMostSpecificParams(CLOSURE_DO_CALL_METHOD, matchingMethods, arguments); } }
private static boolean testComponentAssignable(Class toTestAgainst, Class toTest) { Class component = toTest.getComponentType(); if (component == null) return false; return MetaClassHelper.isAssignableFrom(toTestAgainst, component); }