Search in sources :

Example 61 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 62 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 63 with GenericsType

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

the class DelegateASTTransformation method genericPlaceholderNames.

private static List<String> genericPlaceholderNames(MethodNode candidate) {
    GenericsType[] candidateGenericsTypes = candidate.getGenericsTypes();
    List<String> names = new ArrayList<String>();
    if (candidateGenericsTypes != null) {
        for (GenericsType gt : candidateGenericsTypes) {
            names.add(gt.getName());
        }
    }
    return names;
}
Also used : GenericsType(org.codehaus.groovy.ast.GenericsType) ArrayList(java.util.ArrayList)

Example 64 with GenericsType

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

the class AntlrParserPlugin method annotationDef.

protected void annotationDef(AST classDef) {
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST node = classDef.getFirstChild();
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(classDef, "Annotation Definition", modifiers, Opcodes.ACC_SYNCHRONIZED, "synchronized");
        node = node.getNextSibling();
    }
    modifiers |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_ANNOTATION;
    String name = identifier(node);
    node = node.getNextSibling();
    ClassNode superClass = ClassHelper.OBJECT_TYPE;
    GenericsType[] genericsType = null;
    if (isType(TYPE_PARAMETERS, node)) {
        genericsType = makeGenericsType(node);
        node = node.getNextSibling();
    }
    ClassNode[] interfaces = ClassNode.EMPTY_ARRAY;
    if (isType(EXTENDS_CLAUSE, node)) {
        interfaces = interfaces(node);
        node = node.getNextSibling();
    }
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    classNode = new ClassNode(dot(getPackageName(), name), modifiers, superClass, interfaces, null);
    classNode.setSyntheticPublic(syntheticPublic);
    classNode.addAnnotations(annotations);
    classNode.setGenericsTypes(genericsType);
    classNode.addInterface(ClassHelper.Annotation_TYPE);
    configureAST(classNode, classDef);
    assertNodeType(OBJBLOCK, node);
    objectBlock(node);
    output.addClass(classNode);
    classNode = null;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArrayList(java.util.ArrayList) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 65 with GenericsType

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

the class AntlrParserPlugin method makeGenericsArgumentType.

private GenericsType makeGenericsArgumentType(AST typeArgument) {
    GenericsType gt;
    AST rootNode = typeArgument.getFirstChild();
    if (isType(WILDCARD_TYPE, rootNode)) {
        ClassNode base = ClassHelper.makeWithoutCaching("?");
        if (rootNode.getNextSibling() != null) {
            int boundType = getBoundType(rootNode.getNextSibling());
            ClassNode[] gts = makeGenericsBounds(rootNode, boundType);
            if (boundType == TYPE_UPPER_BOUNDS) {
                gt = new GenericsType(base, gts, null);
            } else {
                gt = new GenericsType(base, null, gts[0]);
            }
        } else {
            gt = new GenericsType(base, null, null);
        }
        gt.setName("?");
        gt.setWildcard(true);
    } else {
        ClassNode argument = makeTypeWithArguments(rootNode);
        gt = new GenericsType(argument);
    }
    configureAST(gt, typeArgument);
    return gt;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) 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