Search in sources :

Example 11 with CachedConstructor

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

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)

Example 12 with CachedConstructor

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

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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap)

Example 13 with CachedConstructor

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

the class MetaClassImpl method selectConstructorAndTransformArguments1.

/**
     * Constructor selection algorithm for Groovy 2.1.9+.
     * This selection algorithm was introduced as a workaround for GROOVY-6080. Instead of generating an index between
     * 0 and N where N is the number of super constructors at the time the class is compiled, this algorithm uses
     * a hash of the constructor descriptor instead.
     *
     * This has the advantage of letting the super class add new constructors while being binary compatible. But there
     * are still problems with this approach:
     * <ul>
     *     <li>There's a risk of hash collision, even if it's very low (two constructors of the same class must have the same hash)</li>
     *     <li>If the super class adds a new constructor which takes as an argument a superclass of an existing constructor parameter and
     *     that this new constructor is selected at runtime, it would not find it.</li>
     * </ul>
     *
     * Hopefully in the last case, the error message is much nicer now since it explains that it's a binary incompatible change.
     *
     * @param arguments the actual constructor call arguments
     * @return a hash used to identify the constructor to be called
     * @since 2.1.9
     */
private int selectConstructorAndTransformArguments1(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) + ")");
    }
    final String methodDescriptor = BytecodeHelper.getMethodDescriptor(Void.TYPE, constructor.getNativeParameterTypes());
    // keeping 3 bits for additional information such as vargs
    return BytecodeHelper.hashCode(methodDescriptor);
}
Also used : CachedConstructor(org.codehaus.groovy.reflection.CachedConstructor) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 14 with CachedConstructor

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

the class MetaClassImpl method invokeConstructor.

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) + ")");
}
Also used : 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)

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