Search in sources :

Example 1 with CachedConstructor

use of org.codehaus.groovy.reflection.CachedConstructor in project groovy by apache.

the class MetaClassImpl method createConstructorSite.

/**
     * 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);
}
Also used : MetaClassConstructorSite(org.codehaus.groovy.runtime.callsite.MetaClassConstructorSite) CachedConstructor(org.codehaus.groovy.reflection.CachedConstructor) CachedClass(org.codehaus.groovy.reflection.CachedClass) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap) ConstructorSite(org.codehaus.groovy.runtime.callsite.ConstructorSite) MetaClassConstructorSite(org.codehaus.groovy.runtime.callsite.MetaClassConstructorSite)

Example 2 with CachedConstructor

use of org.codehaus.groovy.reflection.CachedConstructor in project groovy by apache.

the class MetaClassImpl method retrieveConstructor.

public Constructor retrieveConstructor(Class[] arguments) {
    CachedConstructor constructor = (CachedConstructor) chooseMethod("<init>", constructors, arguments);
    if (constructor != null) {
        return constructor.cachedConstructor;
    }
    constructor = (CachedConstructor) chooseMethod("<init>", constructors, arguments);
    if (constructor != null) {
        return constructor.cachedConstructor;
    }
    return null;
}
Also used : CachedConstructor(org.codehaus.groovy.reflection.CachedConstructor)

Example 3 with CachedConstructor

use of org.codehaus.groovy.reflection.CachedConstructor in project groovy by apache.

the class MetaClassImpl method selectConstructorAndTransformArguments0.

private int selectConstructorAndTransformArguments0(final int numberOfConstructors, Object[] arguments) {
    //TODO: that is just a quick prototype, not the real thing!
    if (numberOfConstructors != constructors.size()) {
        throw new IncompatibleClassChangeError("the number of constructors during runtime and compile time for " + this.theClass.getName() + " do not match. Expected " + numberOfConstructors + " but got " + constructors.size());
    }
    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) + ")");
    }
    List l = new ArrayList(constructors.toList());
    Comparator comp = new Comparator() {

        public int compare(Object arg0, Object arg1) {
            CachedConstructor c0 = (CachedConstructor) arg0;
            CachedConstructor c1 = (CachedConstructor) arg1;
            String descriptor0 = BytecodeHelper.getMethodDescriptor(Void.TYPE, c0.getNativeParameterTypes());
            String descriptor1 = BytecodeHelper.getMethodDescriptor(Void.TYPE, c1.getNativeParameterTypes());
            return descriptor0.compareTo(descriptor1);
        }
    };
    Collections.sort(l, comp);
    int found = -1;
    for (int i = 0; i < l.size(); i++) {
        if (l.get(i) != constructor)
            continue;
        found = i;
        break;
    }
    // NOTE: must be changed to "1 |" if constructor was vargs
    return 0 | (found << 8);
}
Also used : CachedConstructor(org.codehaus.groovy.reflection.CachedConstructor) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 4 with CachedConstructor

use of org.codehaus.groovy.reflection.CachedConstructor in project groovy-core by groovy.

the class MetaClassImpl method retrieveConstructor.

/**
     * 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;
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) CachedConstructor(org.codehaus.groovy.reflection.CachedConstructor) CachedClass(org.codehaus.groovy.reflection.CachedClass) SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap)

Example 5 with CachedConstructor

use of org.codehaus.groovy.reflection.CachedConstructor in project groovy-core by groovy.

the class MetaClassImpl method createConstructorSite.

/**
     * 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);
}
Also used : MetaClassConstructorSite(org.codehaus.groovy.runtime.callsite.MetaClassConstructorSite) CachedConstructor(org.codehaus.groovy.reflection.CachedConstructor) CachedClass(org.codehaus.groovy.reflection.CachedClass) SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap) ConstructorSite(org.codehaus.groovy.runtime.callsite.ConstructorSite) MetaClassConstructorSite(org.codehaus.groovy.runtime.callsite.MetaClassConstructorSite)

Aggregations

CachedConstructor (org.codehaus.groovy.reflection.CachedConstructor)14 CachedClass (org.codehaus.groovy.reflection.CachedClass)10 ComplexKeyHashMap (org.codehaus.groovy.util.ComplexKeyHashMap)6 SingleKeyHashMap (org.codehaus.groovy.util.SingleKeyHashMap)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 MetaMethod (groovy.lang.MetaMethod)2 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)2 ConstructorSite (org.codehaus.groovy.runtime.callsite.ConstructorSite)2 MetaClassConstructorSite (org.codehaus.groovy.runtime.callsite.MetaClassConstructorSite)2 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)2 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)2 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)2 NewMetaMethod (org.codehaus.groovy.runtime.metaclass.NewMetaMethod)2 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)2 TransformMetaMethod (org.codehaus.groovy.runtime.metaclass.TransformMetaMethod)2