Search in sources :

Example 6 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 7 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 8 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 9 with CachedConstructor

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

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 10 with CachedConstructor

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

the class MethodSelectionException method appendMethods.

private void appendMethods(StringBuilder buffer) {
    for (int i = 0; i < methods.size; i++) {
        buffer.append("\n  ");
        Object methodOrConstructor = methods.get(i);
        if (methodOrConstructor instanceof MetaMethod) {
            MetaMethod method = (MetaMethod) methodOrConstructor;
            buffer.append(Modifier.toString(method.getModifiers()));
            buffer.append(" ").append(method.getReturnType().getName());
            buffer.append(" ").append(method.getDeclaringClass().getName());
            buffer.append("#");
            buffer.append(method.getName());
            appendClassNames(buffer, method.getNativeParameterTypes());
        } else {
            CachedConstructor method = (CachedConstructor) methodOrConstructor;
            buffer.append(Modifier.toString(method.cachedConstructor.getModifiers()));
            buffer.append(" ").append(method.cachedConstructor.getDeclaringClass().getName());
            buffer.append("#<init>");
            appendClassNames(buffer, method.getNativeParameterTypes());
        }
    }
}
Also used : MetaMethod(groovy.lang.MetaMethod) CachedConstructor(org.codehaus.groovy.reflection.CachedConstructor)

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