Search in sources :

Example 81 with GenericsType

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

the class EnumHelper method makeEnumNode.

public static ClassNode makeEnumNode(String name, int modifiers, ClassNode[] interfaces, ClassNode outerClass) {
    modifiers = modifiers | Opcodes.ACC_FINAL | Opcodes.ACC_ENUM;
    ClassNode enumClass;
    if (outerClass == null) {
        enumClass = new ClassNode(name, modifiers, null, interfaces, MixinNode.EMPTY_ARRAY);
    } else {
        name = outerClass.getName() + "$" + name;
        enumClass = new InnerClassNode(outerClass, name, modifiers, null, interfaces, MixinNode.EMPTY_ARRAY);
    }
    // set super class and generics info
    // "enum X" -> class X extends Enum<X>
    GenericsType gt = new GenericsType(enumClass);
    ClassNode superClass = ClassHelper.makeWithoutCaching("java.lang.Enum");
    superClass.setGenericsTypes(new GenericsType[] { gt });
    enumClass.setSuperClass(superClass);
    superClass.setRedirect(ClassHelper.Enum_Type);
    return enumClass;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode)

Example 82 with GenericsType

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

the class StaticTypeCheckingVisitor method inferMapExpressionType.

protected ClassNode inferMapExpressionType(final MapExpression map) {
    ClassNode mapType = LINKEDHASHMAP_CLASSNODE.getPlainNodeReference();
    List<MapEntryExpression> entryExpressions = map.getMapEntryExpressions();
    if (entryExpressions.isEmpty())
        return mapType;
    GenericsType[] genericsTypes = mapType.getGenericsTypes();
    if (genericsTypes == null || genericsTypes.length < 2 || (genericsTypes.length == 2 && OBJECT_TYPE.equals(genericsTypes[0].getType()) && OBJECT_TYPE.equals(genericsTypes[1].getType()))) {
        List<ClassNode> keyTypes = new LinkedList<ClassNode>();
        List<ClassNode> valueTypes = new LinkedList<ClassNode>();
        for (MapEntryExpression entryExpression : entryExpressions) {
            keyTypes.add(getType(entryExpression.getKeyExpression()));
            valueTypes.add(getType(entryExpression.getValueExpression()));
        }
        // to be used in generics, type must be boxed
        ClassNode keyType = getWrapper(lowestUpperBound(keyTypes));
        // to be used in generics, type must be boxed
        ClassNode valueType = getWrapper(lowestUpperBound(valueTypes));
        if (!OBJECT_TYPE.equals(keyType) || !OBJECT_TYPE.equals(valueType)) {
            ClassNode inferred = mapType.getPlainNodeReference();
            inferred.setGenericsTypes(new GenericsType[] { new GenericsType(wrapTypeIfNecessary(keyType)), new GenericsType(wrapTypeIfNecessary(valueType)) });
            return inferred;
        }
    }
    return mapType;
}
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) LinkedList(java.util.LinkedList)

Example 83 with GenericsType

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

the class StaticTypeCheckingVisitor method extractGenericsConnectionsForSuperClassAndInterfaces.

private void extractGenericsConnectionsForSuperClassAndInterfaces(final Map<String, GenericsType> resolvedPlaceholders, final Map<String, GenericsType> connections) {
    for (GenericsType value : new HashSet<GenericsType>(connections.values())) {
        if (!value.isPlaceholder() && !value.isWildcard()) {
            ClassNode valueType = value.getType();
            List<ClassNode> deepNodes = new LinkedList<ClassNode>();
            ClassNode unresolvedSuperClass = valueType.getUnresolvedSuperClass();
            if (unresolvedSuperClass != null && unresolvedSuperClass.isUsingGenerics()) {
                deepNodes.add(unresolvedSuperClass);
            }
            for (ClassNode node : valueType.getUnresolvedInterfaces()) {
                if (node.isUsingGenerics()) {
                    deepNodes.add(node);
                }
            }
            if (!deepNodes.isEmpty()) {
                for (GenericsType genericsType : resolvedPlaceholders.values()) {
                    ClassNode lowerBound = genericsType.getLowerBound();
                    if (lowerBound != null) {
                        for (ClassNode deepNode : deepNodes) {
                            if (lowerBound.equals(deepNode)) {
                                extractGenericsConnections(connections, deepNode, lowerBound);
                            }
                        }
                    }
                    ClassNode[] upperBounds = genericsType.getUpperBounds();
                    if (upperBounds != null) {
                        for (ClassNode upperBound : upperBounds) {
                            for (ClassNode deepNode : deepNodes) {
                                if (upperBound.equals(deepNode)) {
                                    extractGenericsConnections(connections, deepNode, upperBound);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
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) LinkedList(java.util.LinkedList) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 84 with GenericsType

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

the class StaticTypeCheckingVisitor method getTypeForSpreadExpression.

private ClassNode getTypeForSpreadExpression(ClassNode testClass, ClassNode objectExpressionType, PropertyExpression pexp) {
    if (!pexp.isSpreadSafe())
        return null;
    MethodCallExpression mce = new MethodCallExpression(new VariableExpression("_", testClass), "iterator", ArgumentListExpression.EMPTY_ARGUMENTS);
    mce.visit(this);
    ClassNode callType = getType(mce);
    if (!implementsInterfaceOrIsSubclassOf(callType, Iterator_TYPE))
        return null;
    GenericsType[] types = callType.getGenericsTypes();
    ClassNode contentType = OBJECT_TYPE;
    if (types != null && types.length == 1)
        contentType = types[0].getType();
    PropertyExpression subExp = new PropertyExpression(new VariableExpression("{}", contentType), pexp.getPropertyAsString());
    AtomicReference<ClassNode> result = new AtomicReference<ClassNode>();
    if (existsProperty(subExp, true, new PropertyLookupVisitor(result))) {
        ClassNode intf = LIST_TYPE.getPlainNodeReference();
        intf.setGenericsTypes(new GenericsType[] { new GenericsType(getWrapper(result.get())) });
        return intf;
    }
    return null;
}
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) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 85 with GenericsType

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

the class StaticTypeCheckingVisitor method makeType.

/**
     * wrap type in Class<> if usingClass==true
     */
private ClassNode makeType(ClassNode cn, boolean usingClass) {
    if (usingClass) {
        ClassNode clazzType = CLASS_Type.getPlainNodeReference();
        clazzType.setGenericsTypes(new GenericsType[] { new GenericsType(cn) });
        return clazzType;
    } else {
        return cn;
    }
}
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)

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