Search in sources :

Example 6 with GenericsType

use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.

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 7 with GenericsType

use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.

the class SignatureCodecVersion1 method doEncode.

private void doEncode(final ClassNode node, DataOutputStream dos) throws IOException {
    dos.writeUTF(node.getClass().getSimpleName());
    if (node instanceof UnionTypeClassNode) {
        UnionTypeClassNode union = (UnionTypeClassNode) node;
        ClassNode[] delegates = union.getDelegates();
        dos.writeInt(delegates.length);
        for (ClassNode delegate : delegates) {
            doEncode(delegate, dos);
        }
        return;
    } else if (node instanceof WideningCategories.LowestUpperBoundClassNode) {
        WideningCategories.LowestUpperBoundClassNode lub = (WideningCategories.LowestUpperBoundClassNode) node;
        dos.writeUTF(lub.getLubName());
        doEncode(lub.getUnresolvedSuperClass(), dos);
        ClassNode[] interfaces = lub.getInterfaces();
        if (interfaces == null) {
            dos.writeInt(-1);
        } else {
            dos.writeInt(interfaces.length);
            for (ClassNode anInterface : interfaces) {
                doEncode(anInterface, dos);
            }
        }
        return;
    }
    if (node.isArray()) {
        dos.writeBoolean(true);
        doEncode(node.getComponentType(), dos);
    } else {
        dos.writeBoolean(false);
        dos.writeUTF(BytecodeHelper.getTypeDescription(node));
        dos.writeBoolean(node.isUsingGenerics());
        GenericsType[] genericsTypes = node.getGenericsTypes();
        if (genericsTypes == null) {
            dos.writeInt(-1);
        } else {
            dos.writeInt(genericsTypes.length);
            for (GenericsType type : genericsTypes) {
                dos.writeBoolean(type.isPlaceholder());
                dos.writeBoolean(type.isWildcard());
                doEncode(type.getType(), dos);
                ClassNode lb = type.getLowerBound();
                if (lb == null) {
                    dos.writeBoolean(false);
                } else {
                    dos.writeBoolean(true);
                    doEncode(lb, dos);
                }
                ClassNode[] upperBounds = type.getUpperBounds();
                if (upperBounds == null) {
                    dos.writeInt(-1);
                } else {
                    dos.writeInt(upperBounds.length);
                    for (ClassNode bound : upperBounds) {
                        doEncode(bound, dos);
                    }
                }
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) WideningCategories(org.codehaus.groovy.ast.tools.WideningCategories) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 8 with GenericsType

use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.

the class GenericsVisitor method checkGenericsUsage.

private void checkGenericsUsage(ClassNode n, ClassNode cn, Boolean isAnonInnerClass) {
    if (n.isGenericsPlaceHolder())
        return;
    GenericsType[] nTypes = n.getGenericsTypes();
    GenericsType[] cnTypes = cn.getGenericsTypes();
    // raw type usage is always allowed
    if (nTypes == null)
        return;
    // you can't parameterize a non-generified type
    if (cnTypes == null) {
        String message = "The class " + getPrintName(n) + " (supplied with " + plural("type parameter", nTypes.length) + ") refers to the class " + getPrintName(cn) + " which takes no parameters";
        if (nTypes.length == 0) {
            message += " (invalid Diamond <> usage?)";
        }
        addError(message, n);
        return;
    }
    // parameterize a type by using all of the parameters only
    if (nTypes.length != cnTypes.length) {
        if (Boolean.FALSE.equals(isAnonInnerClass) && nTypes.length == 0) {
            // allow Diamond for non-AIC cases from CCE
            return;
        }
        String message;
        if (Boolean.TRUE.equals(isAnonInnerClass) && nTypes.length == 0) {
            message = "Cannot use diamond <> with anonymous inner classes";
        } else {
            message = "The class " + getPrintName(n) + " (supplied with " + plural("type parameter", nTypes.length) + ") refers to the class " + getPrintName(cn) + " which takes " + plural("parameter", cnTypes.length);
            if (nTypes.length == 0) {
                message += " (invalid Diamond <> usage?)";
            }
        }
        addError(message, n);
        return;
    }
    // check bounds
    for (int i = 0; i < nTypes.length; i++) {
        ClassNode nType = nTypes[i].getType();
        ClassNode cnType = cnTypes[i].getType();
        if (!nType.isDerivedFrom(cnType)) {
            if (cnType.isInterface() && nType.implementsInterface(cnType))
                continue;
            addError("The type " + nTypes[i].getName() + " is not a valid substitute for the bounded parameter <" + getPrintName(cnTypes[i]) + ">", n);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 9 with GenericsType

use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.

the class GenericsVisitor method checkWildcard.

private boolean checkWildcard(ClassNode cn) {
    ClassNode sn = cn.getUnresolvedSuperClass(false);
    if (sn == null)
        return false;
    GenericsType[] generics = sn.getGenericsTypes();
    if (generics == null)
        return false;
    boolean error = false;
    for (GenericsType generic : generics) {
        if (generic.isWildcard()) {
            addError("A supertype may not specify a wildcard type", sn);
            error = true;
        }
    }
    return error;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 10 with GenericsType

use of org.codehaus.groovy.ast.GenericsType in project groovy by apache.

the class Java5 method configureWildcardType.

private ClassNode configureWildcardType(WildcardType wildcardType) {
    ClassNode base = ClassHelper.makeWithoutCaching("?");
    base.setRedirect(ClassHelper.OBJECT_TYPE);
    //TODO: more than one lower bound for wildcards?
    ClassNode[] lowers = configureTypes(wildcardType.getLowerBounds());
    ClassNode lower = null;
    // TODO: is it safe to remove this? What was the original intention?
    if (lowers != null)
        lower = lowers[0];
    ClassNode[] upper = configureTypes(wildcardType.getUpperBounds());
    GenericsType t = new GenericsType(base, upper, lower);
    t.setWildcard(true);
    ClassNode ref = ClassHelper.makeWithoutCaching(Object.class, false);
    ref.setGenericsTypes(new GenericsType[] { t });
    return ref;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Aggregations

GenericsType (org.codehaus.groovy.ast.GenericsType)165 ClassNode (org.codehaus.groovy.ast.ClassNode)143 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)71 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)52 Parameter (org.codehaus.groovy.ast.Parameter)20 ClosureSignatureHint (groovy.transform.stc.ClosureSignatureHint)19 MethodNode (org.codehaus.groovy.ast.MethodNode)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