org.codehaus.groovy.runtime.MetaClassHelper Java Examples
The following examples show how to use
org.codehaus.groovy.runtime.MetaClassHelper.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParameterTypes.java From groovy with Apache License 2.0 | 6 votes |
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; }
Example #2
Source File: ParameterTypes.java From groovy with Apache License 2.0 | 6 votes |
private static boolean isValidVarargsMethod(Class[] arguments, int size, CachedClass[] pt, int paramMinus1) { // first check normal number of parameters for (int i = 0; i < paramMinus1; i++) { if (pt[i].isAssignableFrom(arguments[i])) continue; return false; } // check direct match CachedClass varg = pt[paramMinus1]; Class clazz = varg.getTheClass().getComponentType(); if (size == pt.length && (varg.isAssignableFrom(arguments[paramMinus1]) || testComponentAssignable(clazz, arguments[paramMinus1]))) { return true; } // check varged for (int i = paramMinus1; i < size; i++) { if (MetaClassHelper.isAssignableFrom(clazz, arguments[i])) continue; return false; } return true; }
Example #3
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 6 votes |
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)); }
Example #4
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 6 votes |
private Object invokeConstructor(Class at, Object[] arguments) { checkInitalised(); if (arguments == null) arguments = EMPTY_ARGUMENTS; Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments); MetaClassHelper.unwrap(arguments); CachedConstructor constructor = (CachedConstructor) chooseMethod("<init>", constructors, argClasses); if (constructor != null) { return constructor.doConstructorInvoke(arguments); } if (arguments.length == 1) { Object firstArgument = arguments[0]; if (firstArgument instanceof Map) { constructor = (CachedConstructor) chooseMethod("<init>", constructors, MetaClassHelper.EMPTY_TYPE_ARRAY); if (constructor != null) { Object bean = constructor.doConstructorInvoke(MetaClassHelper.EMPTY_ARRAY); setProperties(bean, ((Map) firstArgument)); return bean; } } } throw new GroovyRuntimeException( "Could not find matching constructor for: " + theClass.getName() + "(" + InvokerHelper.toTypeString(arguments) + ")"); }
Example #5
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 6 votes |
/** * This is a helper method added in Groovy 2.1.0, which is used only by indy. * This method is for internal use only. * * @since Groovy 2.1.0 */ public MetaMethod retrieveConstructor(Object[] arguments) { checkInitalised(); if (arguments == null) arguments = EMPTY_ARGUMENTS; Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments); MetaClassHelper.unwrap(arguments); Object res = chooseMethod("<init>", constructors, argClasses); if (res instanceof MetaMethod) return (MetaMethod) res; CachedConstructor constructor = (CachedConstructor) res; if (constructor != null) return new MetaConstructor(constructor, false); if (arguments.length == 1 && arguments[0] instanceof Map) { res = chooseMethod("<init>", constructors, MetaClassHelper.EMPTY_TYPE_ARRAY); } else if ( arguments.length == 2 && arguments[1] instanceof Map && theClass.getEnclosingClass() != null && theClass.getEnclosingClass().isAssignableFrom(argClasses[0])) { res = chooseMethod("<init>", constructors, new Class[]{argClasses[0]}); } if (res instanceof MetaMethod) return (MetaMethod) res; constructor = (CachedConstructor) res; if (constructor != null) return new MetaConstructor(constructor, true); return null; }
Example #6
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 6 votes |
/** * Create a CallSite */ public CallSite createPogoCallSite(CallSite site, Object[] args) { if (!GroovyCategorySupport.hasCategoryInCurrentThread() && !(this instanceof AdaptingMetaClass)) { Class[] params = MetaClassHelper.convertToTypeArray(args); CallSite tempSite = site; if (site.getName().equals("call") && GeneratedClosure.class.isAssignableFrom(theClass)) { // here, we want to point to a method named "doCall" instead of "call" // but we don't want to replace the original call site name, otherwise // we loose the fact that the original method name was "call" so instead // we will point to a metamethod called "doCall" // see GROOVY-5806 for details tempSite = new AbstractCallSite(site.getArray(), site.getIndex(), "doCall"); } MetaMethod metaMethod = getMethodWithCachingInternal(theClass, tempSite, params); if (metaMethod != null) return PogoMetaMethodSite.createPogoMetaMethodSite(site, this, metaMethod, params, args); } return new PogoMetaClassSite(site, this); }
Example #7
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 6 votes |
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; }
Example #8
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 6 votes |
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; }
Example #9
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 6 votes |
public MetaMethod retrieveStaticMethod(String methodName, Object[] arguments) { final MetaMethodIndex.Entry e = metaMethodIndex.getMethods(theClass, methodName); MetaMethodIndex.CacheEntry cacheEntry; if (e != null) { cacheEntry = e.cachedStaticMethod; if (cacheEntry != null && MetaClassHelper.sameClasses(cacheEntry.params, arguments, e.staticMethods instanceof MetaMethod)) { return cacheEntry.method; } final Class[] classes = MetaClassHelper.convertToTypeArray(arguments); cacheEntry = new MetaMethodIndex.CacheEntry(classes, pickStaticMethod(methodName, classes)); e.cachedStaticMethod = cacheEntry; return cacheEntry.method; } return pickStaticMethod(methodName, MetaClassHelper.convertToTypeArray(arguments)); }
Example #10
Source File: MetaMethod.java From groovy with Apache License 2.0 | 6 votes |
/** * 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); }
Example #11
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 6 votes |
private MetaMethod pickStaticMethod(String methodName, Class[] arguments) { MetaMethod method = null; MethodSelectionException mse = null; Object methods = getStaticMethods(theClass, methodName); if (!(methods instanceof FastArray) || !((FastArray) methods).isEmpty()) { try { method = (MetaMethod) chooseMethod(methodName, methods, arguments); } catch (MethodSelectionException msex) { mse = msex; } } if (method == null && theClass != Class.class) { MetaClass classMetaClass = registry.getMetaClass(Class.class); method = classMetaClass.pickMethod(methodName, arguments); } if (method == null) { method = (MetaMethod) chooseMethod(methodName, methods, MetaClassHelper.convertToTypeArray(arguments)); } if (method == null && mse != null) { throw mse; } else { return method; } }
Example #12
Source File: MetaBeanProperty.java From groovy with Apache License 2.0 | 5 votes |
/** * Get the property of the given object. * * @param object which to be got * @return the property of the given object * @throws RuntimeException if the property could not be evaluated */ public Object getProperty(Object object) { MetaMethod getter = getGetter(); if (getter == null) { if (field != null) return field.getProperty(object); //TODO: create a WriteOnlyException class? throw new GroovyRuntimeException("Cannot read write-only property: " + name); } return getter.invoke(object, MetaClassHelper.EMPTY_ARRAY); }
Example #13
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 5 votes |
/** * Create a CallSite */ public CallSite createPojoCallSite(CallSite site, Object receiver, Object[] args) { if (!(this instanceof AdaptingMetaClass)) { Class[] params = MetaClassHelper.convertToTypeArray(args); MetaMethod metaMethod = getMethodWithCachingInternal(getTheClass(), site, params); if (metaMethod != null) return PojoMetaMethodSite.createPojoMetaMethodSite(site, this, metaMethod, params, receiver, args); } return new PojoMetaClassSite(site, this); }
Example #14
Source File: ExpandoMetaClass.java From groovy with Apache License 2.0 | 5 votes |
public CallSite createConstructorSite(CallSite site, Object[] args) { Class[] params = MetaClassHelper.convertToTypeArray(args); MetaMethod method = pickMethod(GROOVY_CONSTRUCTOR, params); if (method != null && method.getParameterTypes().length == args.length) { if (method.getDeclaringClass().getTheClass().equals(getTheClass())) { return new ConstructorMetaMethodSite(site, this, method, params); } } return super.createConstructorSite(site, args); }
Example #15
Source File: ExpandoMetaClass.java From groovy with Apache License 2.0 | 5 votes |
@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); }
Example #16
Source File: ExpandoMetaClass.java From groovy with Apache License 2.0 | 5 votes |
/** * Returns a property name equivalent for the given setter name or null if it is not a getter * * @param setterName The setter name * @return The property name equivalent */ public String getPropertyForSetter(String setterName) { if (setterName == null || setterName.length() == 0) return null; if (setterName.startsWith("set")) { String prop = setterName.substring(3); return MetaClassHelper.convertPropertyName(prop); } return null; }
Example #17
Source File: ExpandoMetaClass.java From groovy with Apache License 2.0 | 5 votes |
/** * Overrides default implementation just in case a static invoke method has been set on ExpandoMetaClass * * @see MetaClassImpl#invokeStaticMethod(Object, String, Object[]) */ public Object invokeStaticMethod(Object object, String methodName, Object[] arguments) { if (invokeStaticMethodMethod != null) { MetaClassHelper.unwrap(arguments); return invokeStaticMethodMethod.invoke(object, new Object[]{methodName, arguments}); } return super.invokeStaticMethod(object, methodName, arguments); }
Example #18
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 5 votes |
/** * Create a CallSite */ public CallSite createConstructorSite(CallSite site, Object[] args) { if (!(this instanceof AdaptingMetaClass)) { Class[] params = MetaClassHelper.convertToTypeArray(args); CachedConstructor constructor = (CachedConstructor) chooseMethod("<init>", constructors, params); if (constructor != null) { return ConstructorSite.createConstructorSite(site, this, constructor, params, args); } else { if (args.length == 1 && args[0] instanceof Map) { constructor = (CachedConstructor) chooseMethod("<init>", constructors, MetaClassHelper.EMPTY_TYPE_ARRAY); if (constructor != null) { return new ConstructorSite.NoParamSite(site, this, constructor, params); } } else if (args.length == 2 && theClass.getEnclosingClass() != null && args[1] instanceof Map) { Class enclosingClass = theClass.getEnclosingClass(); String enclosingInstanceParamType = args[0] != null ? args[0].getClass().getName() : ""; if (enclosingClass.getName().equals(enclosingInstanceParamType)) { constructor = (CachedConstructor) chooseMethod("<init>", constructors, new Class[]{enclosingClass}); if (constructor != null) { return new ConstructorSite.NoParamSiteInnerClass(site, this, constructor, params); } } } } } return new MetaClassConstructorSite(site, this); }
Example #19
Source File: MixinInMetaClass.java From groovy with Apache License 2.0 | 5 votes |
public synchronized Object getMixinInstance(Object object) { Object mixinInstance = get(object); if (mixinInstance == null) { mixinInstance = constructor.invoke(MetaClassHelper.EMPTY_ARRAY); new MixedInMetaClass(mixinInstance, object); put(object, mixinInstance); } return mixinInstance; }
Example #20
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 5 votes |
/** * Create a CallSite */ public CallSite createPogoCallCurrentSite(CallSite site, Class sender, Object[] args) { if (!GroovyCategorySupport.hasCategoryInCurrentThread() && !(this instanceof AdaptingMetaClass)) { Class[] params = MetaClassHelper.convertToTypeArray(args); MetaMethod metaMethod = getMethodWithCachingInternal(sender, site, params); if (metaMethod != null) return PogoMetaMethodSite.createPogoMetaMethodSite(site, this, metaMethod, params, args); } return new PogoMetaClassSite(site, this); }
Example #21
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 5 votes |
/** * Create a CallSite */ public CallSite createStaticSite(CallSite site, Object[] args) { if (!(this instanceof AdaptingMetaClass)) { Class[] params = MetaClassHelper.convertToTypeArray(args); MetaMethod metaMethod = retrieveStaticMethod(site.getName(), args); if (metaMethod != null) return StaticMetaMethodSite.createStaticMetaMethodSite(site, this, metaMethod, params, args); } return new StaticMetaClassSite(site, this); }
Example #22
Source File: GrailsDataBinder.java From AlgoTrader with GNU General Public License v2.0 | 5 votes |
/** * Hack because of bug in ThreadManagedMetaBeanProperty, http://jira.codehaus.org/browse/GROOVY-3723 , fixed since 1.6.5 * * @param metaProperty * @param delegate * @return */ private Object getMetaPropertyValue(MetaProperty metaProperty, Object delegate) { if (metaProperty instanceof ThreadManagedMetaBeanProperty) { return ((ThreadManagedMetaBeanProperty) metaProperty).getGetter().invoke(delegate, MetaClassHelper.EMPTY_ARRAY); } return metaProperty.getProperty(delegate); }
Example #23
Source File: ExpandoMetaClass.java From groovy with Apache License 2.0 | 5 votes |
/** * Overrides default implementation just in case invokeMethod has been overridden by ExpandoMetaClass * * @see groovy.lang.MetaClassImpl#invokeMethod(Class, Object, String, Object[], boolean, boolean) */ public Object invokeMethod(Class sender, Object object, String methodName, Object[] originalArguments, boolean isCallToSuper, boolean fromInsideClass) { if (invokeMethodMethod != null) { MetaClassHelper.unwrap(originalArguments); return invokeMethodMethod.invoke(object, new Object[]{methodName, originalArguments}); } return super.invokeMethod(sender, object, methodName, originalArguments, isCallToSuper, fromInsideClass); }
Example #24
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 5 votes |
public MetaMethod getMethodWithCaching(Class sender, String methodName, Object[] arguments, boolean isCallToSuper) { // let's try use the cache to find the method if (!isCallToSuper && GroovyCategorySupport.hasCategoryInCurrentThread()) { return getMethodWithoutCaching(sender, methodName, MetaClassHelper.convertToTypeArray(arguments), isCallToSuper); } final MetaMethodIndex.Entry e = metaMethodIndex.getMethods(sender, methodName); if (e == null) { return null; } return isCallToSuper ? getSuperMethodWithCaching(arguments, e) : getNormalMethodWithCaching(arguments, e); }
Example #25
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 5 votes |
private MetaMethod getMetaMethod(Class sender, Object object, String methodName, boolean isCallToSuper, Object... arguments) { MetaMethod method = null; if (CLOSURE_CALL_METHOD.equals(methodName) && object instanceof GeneratedClosure) { method = getMethodWithCaching(sender, "doCall", arguments, isCallToSuper); } if (method == null) { method = getMethodWithCaching(sender, methodName, arguments, isCallToSuper); } MetaClassHelper.unwrap(arguments); if (method == null) method = tryListParamMetaMethod(sender, methodName, isCallToSuper, arguments); return method; }
Example #26
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 5 votes |
/** * Invoke a method on the given object with the given arguments. * * @param object The object the method should be invoked on. * @param methodName The name of the method to invoke. * @param arguments The arguments to the invoked method as null, a Tuple, an array or a single argument of any type. * @return The result of the method invocation. */ public Object invokeMethod(Object object, String methodName, Object arguments) { if (arguments == null) { return invokeMethod(object, methodName, MetaClassHelper.EMPTY_ARRAY); } if (arguments instanceof Tuple) { Tuple tuple = (Tuple) arguments; return invokeMethod(object, methodName, tuple.toArray()); } if (arguments instanceof Object[]) { return invokeMethod(object, methodName, (Object[]) arguments); } return invokeMethod(object, methodName, new Object[]{arguments}); }
Example #27
Source File: MetaClassImpl.java From groovy with Apache License 2.0 | 5 votes |
/** * @see MetaObjectProtocol#respondsTo(Object, String, Object[]) */ public List respondsTo(Object obj, String name, Object[] argTypes) { Class[] classes = MetaClassHelper.castArgumentsToClassArray(argTypes); MetaMethod m = getMetaMethod(name, classes); if (m != null) { return Collections.singletonList(m); } return Collections.emptyList(); }
Example #28
Source File: MultipleSetterProperty.java From groovy with Apache License 2.0 | 5 votes |
@Override public Object getProperty(Object object) { MetaMethod getter = getGetter(); if (getter == null) { if (field != null) return field.getProperty(object); throw new GroovyRuntimeException("Cannot read write-only property: " + name); } return getter.invoke(object, MetaClassHelper.EMPTY_ARRAY); }
Example #29
Source File: TemporaryMethodKey.java From groovy with Apache License 2.0 | 5 votes |
public TemporaryMethodKey(Class sender, String name, Object[] parameterValues, boolean isCallToSuper) { super(sender, name, isCallToSuper); if (parameterValues == null) { parameterValues = MetaClassHelper.EMPTY_ARRAY; } this.parameterValues = parameterValues; }
Example #30
Source File: ClosureMetaClass.java From groovy with Apache License 2.0 | 5 votes |
@Override public MetaMethod pickMethod(String name, Class[] argTypes) { if (argTypes == null) argTypes = MetaClassHelper.EMPTY_CLASS_ARRAY; if (name.equals(CLOSURE_CALL_METHOD) || name.equals(CLOSURE_DO_CALL_METHOD)) { return pickClosureMethod(argTypes); } return CLOSURE_METACLASS.getMetaMethod(name, argTypes); }