Java Code Examples for org.codehaus.groovy.runtime.MetaClassHelper#convertToTypeArray()

The following examples show how to use org.codehaus.groovy.runtime.MetaClassHelper#convertToTypeArray() . 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: MetaClassImpl.java    From groovy with Apache License 2.0 6 votes vote down vote up
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 2
Source File: MetaClassImpl.java    From groovy with Apache License 2.0 6 votes vote down vote up
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 3
Source File: MetaClassImpl.java    From groovy with Apache License 2.0 6 votes vote down vote up
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 4
Source File: MetaClassImpl.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * 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 5
Source File: MetaClassImpl.java    From groovy with Apache License 2.0 6 votes vote down vote up
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 6
Source File: MetaClassImpl.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * 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 5 votes vote down vote up
/**
 * 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 8
Source File: MetaClassImpl.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * 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 9
Source File: MetaClassImpl.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * 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 10
Source File: MetaClassImpl.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * 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 11
Source File: ExpandoMetaClass.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object invokeConstructor(Object[] arguments) {

        // TODO This is the only area where this MetaClass needs to do some interception because Groovy's current
        // MetaClass uses hard coded references to the java.lang.reflect.Constructor class so you can't simply
        // inject Constructor like you can do properties, methods and fields. When Groovy's MetaClassImpl is
        // refactored we can fix this
        Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments);
        MetaMethod method = pickMethod(GROOVY_CONSTRUCTOR, argClasses);
        if (method != null && method.getParameterTypes().length == arguments.length) {
            return method.invoke(theClass, arguments);
        }
        return super.invokeConstructor(arguments);
    }
 
Example 12
Source File: ExpandoMetaClass.java    From groovy with Apache License 2.0 5 votes vote down vote up
@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 13
Source File: ExpandoMetaClass.java    From groovy with Apache License 2.0 5 votes vote down vote up
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);
}