Search in sources :

Example 31 with GenericsType

use of org.codehaus.groovy.ast.GenericsType in project groovy-core by groovy.

the class SignatureCodecVersion1 method doDecode.

private ClassNode doDecode(final DataInputStream dis) throws IOException {
    String classNodeType = dis.readUTF();
    if (UnionTypeClassNode.class.getSimpleName().equals(classNodeType)) {
        int len = dis.readInt();
        ClassNode[] delegates = new ClassNode[len];
        for (int i = 0; i < len; i++) {
            delegates[i] = doDecode(dis);
        }
        return new UnionTypeClassNode(delegates);
    } else if (WideningCategories.LowestUpperBoundClassNode.class.getSimpleName().equals(classNodeType)) {
        String name = dis.readUTF();
        ClassNode upper = doDecode(dis);
        int len = dis.readInt();
        ClassNode[] interfaces = null;
        if (len >= 0) {
            interfaces = new ClassNode[len];
            for (int i = 0; i < len; i++) {
                interfaces[i] = doDecode(dis);
            }
        }
        return new WideningCategories.LowestUpperBoundClassNode(name, upper, interfaces);
    }
    boolean makeArray = dis.readBoolean();
    if (makeArray) {
        return doDecode(dis).makeArray();
    }
    String typedesc = dis.readUTF();
    char typeCode = typedesc.charAt(0);
    ClassNode result = OBJECT_TYPE;
    if (typeCode == 'L') {
        // object type
        String className = typedesc.replace('/', '.').substring(1, typedesc.length() - 1);
        try {
            result = ClassHelper.make(Class.forName(className, false, classLoader)).getPlainNodeReference();
        } catch (ClassNotFoundException e) {
            result = ClassHelper.make(className);
        }
        result.setUsingGenerics(dis.readBoolean());
        int len = dis.readInt();
        if (len >= 0) {
            GenericsType[] gts = new GenericsType[len];
            for (int i = 0; i < len; i++) {
                boolean placeholder = dis.readBoolean();
                boolean wildcard = dis.readBoolean();
                ClassNode type = doDecode(dis);
                boolean low = dis.readBoolean();
                ClassNode lb = null;
                if (low) {
                    lb = doDecode(dis);
                }
                int upc = dis.readInt();
                ClassNode[] ups = null;
                if (upc >= 0) {
                    ups = new ClassNode[upc];
                    for (int j = 0; j < upc; j++) {
                        ups[j] = doDecode(dis);
                    }
                }
                GenericsType gt = new GenericsType(type, ups, lb);
                gt.setPlaceholder(placeholder);
                gt.setWildcard(wildcard);
                gts[i] = gt;
            }
            result.setGenericsTypes(gts);
        }
    } else {
        // primitive type
        switch(typeCode) {
            case 'I':
                result = int_TYPE;
                break;
            case 'Z':
                result = boolean_TYPE;
                break;
            case 'B':
                result = byte_TYPE;
                break;
            case 'C':
                result = char_TYPE;
                break;
            case 'S':
                result = short_TYPE;
                break;
            case 'D':
                result = double_TYPE;
                break;
            case 'F':
                result = float_TYPE;
                break;
            case 'J':
                result = long_TYPE;
                break;
            case 'V':
                result = VOID_TYPE;
                break;
        }
    }
    return result;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) WideningCategories(org.codehaus.groovy.ast.tools.WideningCategories) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 32 with GenericsType

use of org.codehaus.groovy.ast.GenericsType in project groovy-core by groovy.

the class StaticTypeCheckingVisitor method inferLoopElementType.

/**
     * Given a loop collection type, returns the inferred type of the loop element. Used, for
     * example, to infer the element type of a (for e in list) loop.
     *
     * @param collectionType the type of the collection
     * @return the inferred component type
     */
public static ClassNode inferLoopElementType(final ClassNode collectionType) {
    ClassNode componentType = collectionType.getComponentType();
    if (componentType == null) {
        if (implementsInterfaceOrIsSubclassOf(collectionType, ITERABLE_TYPE)) {
            ClassNode intf = GenericsUtils.parameterizeType(collectionType, ITERABLE_TYPE);
            GenericsType[] genericsTypes = intf.getGenericsTypes();
            componentType = genericsTypes[0].getType();
        } else if (implementsInterfaceOrIsSubclassOf(collectionType, MAP_TYPE)) {
            // GROOVY-6240
            ClassNode intf = GenericsUtils.parameterizeType(collectionType, MAP_TYPE);
            GenericsType[] genericsTypes = intf.getGenericsTypes();
            componentType = MAP_ENTRY_TYPE.getPlainNodeReference();
            componentType.setGenericsTypes(genericsTypes);
        } else if (STRING_TYPE.equals(collectionType)) {
            componentType = ClassHelper.Character_TYPE;
        } else if (ENUMERATION_TYPE.equals(collectionType)) {
            // GROOVY-6123
            ClassNode intf = GenericsUtils.parameterizeType(collectionType, ENUMERATION_TYPE);
            GenericsType[] genericsTypes = intf.getGenericsTypes();
            componentType = genericsTypes[0].getType();
        } else {
            componentType = ClassHelper.OBJECT_TYPE;
        }
    }
    return componentType;
}
Also used : LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 33 with GenericsType

use of org.codehaus.groovy.ast.GenericsType in project groovy-core by groovy.

the class TypeSignatureParser method createWildcard.

private static GenericsType createWildcard(ClassNode[] upper, ClassNode lower) {
    ClassNode base = ClassHelper.makeWithoutCaching("?");
    base.setRedirect(ClassHelper.OBJECT_TYPE);
    GenericsType t = new GenericsType(base, upper, lower);
    t.setWildcard(true);
    return t;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 34 with GenericsType

use of org.codehaus.groovy.ast.GenericsType in project groovy-core by groovy.

the class TypeSignatureParser method visitEnd.

@Override
public void visitEnd() {
    ClassNode base = resolver.resolveClass(baseName);
    if (arguments.isEmpty()) {
        finished(base);
        return;
    }
    ClassNode bound = base.getPlainNodeReference();
    bound.setGenericsTypes(arguments.toArray(new GenericsType[arguments.size()]));
    finished(bound);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 35 with GenericsType

use of org.codehaus.groovy.ast.GenericsType in project groovy-core by groovy.

the class GenericsUtils method correctToGenericsSpecRecurse.

public static ClassNode correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode type, List<String> exclusions) {
    if (type.isArray()) {
        return correctToGenericsSpecRecurse(genericsSpec, type.getComponentType(), exclusions).makeArray();
    }
    if (type.isGenericsPlaceHolder() && !exclusions.contains(type.getUnresolvedName())) {
        String name = type.getGenericsTypes()[0].getName();
        type = genericsSpec.get(name);
        if (type != null && type.isGenericsPlaceHolder() && type.getGenericsTypes() == null) {
            ClassNode placeholder = ClassHelper.makeWithoutCaching(type.getUnresolvedName());
            placeholder.setGenericsPlaceHolder(true);
            type = makeClassSafeWithGenerics(type, new GenericsType(placeholder));
        }
    }
    if (type == null)
        type = ClassHelper.OBJECT_TYPE;
    GenericsType[] oldgTypes = type.getGenericsTypes();
    GenericsType[] newgTypes = GenericsType.EMPTY_ARRAY;
    if (oldgTypes != null) {
        newgTypes = new GenericsType[oldgTypes.length];
        for (int i = 0; i < newgTypes.length; i++) {
            GenericsType oldgType = oldgTypes[i];
            if (oldgType.isPlaceholder()) {
                if (genericsSpec.get(oldgType.getName()) != null) {
                    newgTypes[i] = new GenericsType(genericsSpec.get(oldgType.getName()));
                } else {
                    newgTypes[i] = new GenericsType(ClassHelper.OBJECT_TYPE);
                }
            } else if (oldgType.isWildcard()) {
                ClassNode oldLower = oldgType.getLowerBound();
                ClassNode lower = oldLower != null ? correctToGenericsSpecRecurse(genericsSpec, oldLower, exclusions) : null;
                ClassNode[] oldUpper = oldgType.getUpperBounds();
                ClassNode[] upper = null;
                if (oldUpper != null) {
                    upper = new ClassNode[oldUpper.length];
                    for (int j = 0; j < oldUpper.length; j++) {
                        upper[j] = correctToGenericsSpecRecurse(genericsSpec, oldUpper[j], exclusions);
                    }
                }
                GenericsType fixed = new GenericsType(oldgType.getType(), upper, lower);
                fixed.setName(oldgType.getName());
                fixed.setWildcard(true);
                newgTypes[i] = fixed;
            } else {
                newgTypes[i] = new GenericsType(correctToGenericsSpecRecurse(genericsSpec, correctToGenericsSpec(genericsSpec, oldgType), exclusions));
            }
        }
    }
    return makeClassSafeWithGenerics(type, newgTypes);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Aggregations

GenericsType (org.codehaus.groovy.ast.GenericsType)171 ClassNode (org.codehaus.groovy.ast.ClassNode)148 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)76 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)52 Parameter (org.codehaus.groovy.ast.Parameter)21 MethodNode (org.codehaus.groovy.ast.MethodNode)20 ClosureSignatureHint (groovy.transform.stc.ClosureSignatureHint)19 LinkedList (java.util.LinkedList)18 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)15 LinkedHashMap (java.util.LinkedHashMap)9 AST (antlr.collections.AST)8 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)8 ListHashMap (org.codehaus.groovy.util.ListHashMap)8 Map (java.util.Map)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 EnumConstantClassNode (org.codehaus.groovy.ast.EnumConstantClassNode)6 FieldNode (org.codehaus.groovy.ast.FieldNode)6 GroovyBugError (org.codehaus.groovy.GroovyBugError)5 DynamicVariable (org.codehaus.groovy.ast.DynamicVariable)4