Search in sources :

Example 76 with GenericsType

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

the class GradleResolveVisitor method resolveGenericsHeader.

private void resolveGenericsHeader(GenericsType[] types) {
    if (types == null) {
        return;
    }
    currentClass.setUsingGenerics(true);
    for (GenericsType type : types) {
        ClassNode classNode = type.getType();
        String name = type.getName();
        ClassNode[] bounds = type.getUpperBounds();
        if (bounds != null) {
            boolean nameAdded = false;
            for (ClassNode upperBound : bounds) {
                if (!nameAdded && upperBound != null || !resolve(classNode)) {
                    genericParameterNames.put(name, type);
                    type.setPlaceholder(true);
                    classNode.setRedirect(upperBound);
                    nameAdded = true;
                }
                resolveOrFail(upperBound, classNode);
            }
        } else {
            genericParameterNames.put(name, type);
            classNode.setRedirect(ClassHelper.OBJECT_TYPE);
            type.setPlaceholder(true);
        }
    }
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 77 with GenericsType

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

the class GradleResolveVisitor method visitClass.

public void visitClass(ClassNode node) {
    ClassNode oldNode = currentClass;
    if (node instanceof InnerClassNode) {
        if (Modifier.isStatic(node.getModifiers())) {
            genericParameterNames = new HashMap<String, GenericsType>();
        }
    } else {
        genericParameterNames = new HashMap<String, GenericsType>();
    }
    currentClass = node;
    resolveGenericsHeader(node.getGenericsTypes());
    ModuleNode module = node.getModule();
    if (!module.hasImportsResolved()) {
        for (ImportNode importNode : module.getImports()) {
            currImportNode = importNode;
            ClassNode type = importNode.getType();
            if (resolve(type, false, false, true)) {
                currImportNode = null;
                continue;
            }
            currImportNode = null;
            addError("unable to resolve class " + type.getName(), type);
        }
        for (ImportNode importNode : module.getStaticStarImports().values()) {
            ClassNode type = importNode.getType();
            if (resolve(type, false, false, true)) {
                continue;
            }
            // Try with the node's package too. If still not found, revert to original type name.
            if (type.getPackageName() == null && node.getPackageName() != null) {
                String oldTypeName = type.getName();
                type.setName(node.getPackageName() + "." + oldTypeName);
                if (resolve(type, false, false, true)) {
                    continue;
                }
                type.setName(oldTypeName);
            }
            addError("unable to resolve class " + type.getName(), type);
        }
        for (ImportNode importNode : module.getStaticImports().values()) {
            ClassNode type = importNode.getType();
            if (resolve(type, true, true, true)) {
                continue;
            }
            addError("unable to resolve class " + type.getName(), type);
        }
        for (ImportNode importNode : module.getStaticStarImports().values()) {
            ClassNode type = importNode.getType();
            if (resolve(type, true, true, true)) {
                continue;
            }
            addError("unable to resolve class " + type.getName(), type);
        }
        module.setImportsResolved(true);
    }
    ClassNode sn = node.getUnresolvedSuperClass();
    if (sn != null) {
        resolveOrFail(sn, node, true);
    }
    for (ClassNode anInterface : node.getInterfaces()) {
        resolveOrFail(anInterface, node, true);
    }
    checkCyclicInheritence(node, node.getUnresolvedSuperClass(), node.getInterfaces());
    super.visitClass(node);
    currentClass = oldNode;
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType) ImportNode(org.codehaus.groovy.ast.ImportNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 78 with GenericsType

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

the class GradleResolveVisitor method resolveGenericsType.

private boolean resolveGenericsType(GenericsType genericsType) {
    if (genericsType.isResolved()) {
        return true;
    }
    currentClass.setUsingGenerics(true);
    ClassNode type = genericsType.getType();
    // save name before redirect
    String name = type.getName();
    ClassNode[] bounds = genericsType.getUpperBounds();
    if (!genericParameterNames.containsKey(name)) {
        if (bounds != null) {
            for (ClassNode upperBound : bounds) {
                resolveOrFail(upperBound, genericsType);
                type.setRedirect(upperBound);
                resolveGenericsTypes(upperBound.getGenericsTypes());
            }
        } else if (genericsType.isWildcard()) {
            type.setRedirect(ClassHelper.OBJECT_TYPE);
        } else {
            resolveOrFail(type, genericsType);
        }
    } else {
        GenericsType gt = genericParameterNames.get(name);
        type.setRedirect(gt.getType());
        genericsType.setPlaceholder(true);
    }
    if (genericsType.getLowerBound() != null) {
        resolveOrFail(genericsType.getLowerBound(), genericsType);
    }
    if (resolveGenericsTypes(type.getGenericsTypes())) {
        genericsType.setResolved(genericsType.getType().isResolved());
    }
    return genericsType.isResolved();
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 79 with GenericsType

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

the class DefaultGrailsDomainClassInjector method findPropertyType.

/**
     * Finds the type of the generated property.  The type will be a {@link Set} that is parameterized
     * by the type of the expression passed in.
     * @param expression the expression used to parameterize the {@link Set}.  Only used if a {@link ClassExpression}.  Otherwise ignored.
     * @return A {@link ClassNode} of type {@link Set} that is possibly parameterized by the expression that is passed in.
     */
private ClassNode findPropertyType(Expression expression) {
    ClassNode setNode = ClassHelper.make(Set.class).getPlainNodeReference();
    if (expression instanceof ClassExpression) {
        GenericsType[] genericsTypes = new GenericsType[1];
        genericsTypes[0] = new GenericsType(GrailsASTUtils.nonGeneric(expression.getType()));
        setNode.setGenericsTypes(genericsTypes);
    }
    return setNode;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) Set(java.util.Set) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 80 with GenericsType

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

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 (int i = 0; i < generics.length; i++) {
        if (generics[i].isWildcard()) {
            addError("A supertype may not specify a wildcard type", sn);
            error = true;
        }
    }
    return error;
}
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