Search in sources :

Example 56 with GenericsType

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

the class StaticTypeCheckingVisitor method getType.

protected ClassNode getType(ASTNode exp) {
    ClassNode cn = exp.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
    if (cn != null)
        return cn;
    if (exp instanceof ClassExpression) {
        ClassNode node = CLASS_Type.getPlainNodeReference();
        node.setGenericsTypes(new GenericsType[] { new GenericsType(((ClassExpression) exp).getType()) });
        return node;
    } else if (exp instanceof VariableExpression) {
        VariableExpression vexp = (VariableExpression) exp;
        ClassNode selfTrait = isTraitSelf(vexp);
        if (selfTrait != null)
            return makeSelf(selfTrait);
        if (vexp == VariableExpression.THIS_EXPRESSION)
            return makeThis();
        if (vexp == VariableExpression.SUPER_EXPRESSION)
            return makeSuper();
        final Variable variable = vexp.getAccessedVariable();
        if (variable instanceof FieldNode) {
            checkOrMarkPrivateAccess(vexp, (FieldNode) variable, isLHSOfEnclosingAssignment(vexp));
            return getType((FieldNode) variable);
        }
        if (variable != null && variable != vexp && variable instanceof VariableExpression) {
            return getType((Expression) variable);
        }
        if (variable instanceof Parameter) {
            Parameter parameter = (Parameter) variable;
            ClassNode type = typeCheckingContext.controlStructureVariables.get(parameter);
            TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
            ClassNode[] closureParamTypes = (ClassNode[]) (enclosingClosure != null ? enclosingClosure.getClosureExpression().getNodeMetaData(StaticTypesMarker.CLOSURE_ARGUMENTS) : null);
            if (type == null && enclosingClosure != null && "it".equals(variable.getName()) && closureParamTypes != null) {
                final Parameter[] parameters = enclosingClosure.getClosureExpression().getParameters();
                if (parameters.length == 0 && getTemporaryTypesForExpression(vexp) == null) {
                    type = closureParamTypes[0];
                }
            }
            if (type != null) {
                storeType((VariableExpression) exp, type);
                return type;
            }
        }
    }
    if (exp instanceof ListExpression) {
        return inferListExpressionType((ListExpression) exp);
    } else if (exp instanceof MapExpression) {
        return inferMapExpressionType((MapExpression) exp);
    }
    if (exp instanceof ConstructorCallExpression) {
        return ((ConstructorCallExpression) exp).getType();
    }
    if (exp instanceof MethodNode) {
        if ((exp == GET_DELEGATE || exp == GET_OWNER || exp == GET_THISOBJECT) && typeCheckingContext.getEnclosingClosure() != null) {
            return typeCheckingContext.getEnclosingClassNode();
        }
        ClassNode ret = getInferredReturnType(exp);
        return ret != null ? ret : ((MethodNode) exp).getReturnType();
    }
    if (exp instanceof ClosureExpression) {
        ClassNode irt = getInferredReturnType(exp);
        if (irt != null) {
            irt = wrapTypeIfNecessary(irt);
            ClassNode result = CLOSURE_TYPE.getPlainNodeReference();
            result.setGenericsTypes(new GenericsType[] { new GenericsType(irt) });
            return result;
        }
    }
    if (exp instanceof RangeExpression) {
        ClassNode plain = ClassHelper.RANGE_TYPE.getPlainNodeReference();
        RangeExpression re = (RangeExpression) exp;
        ClassNode fromType = getType(re.getFrom());
        ClassNode toType = getType(re.getTo());
        if (fromType.equals(toType)) {
            plain.setGenericsTypes(new GenericsType[] { new GenericsType(wrapTypeIfNecessary(fromType)) });
        } else {
            plain.setGenericsTypes(new GenericsType[] { new GenericsType(wrapTypeIfNecessary(lowestUpperBound(fromType, toType))) });
        }
        return plain;
    }
    if (exp instanceof UnaryPlusExpression) {
        return getType(((UnaryPlusExpression) exp).getExpression());
    }
    if (exp instanceof UnaryMinusExpression) {
        return getType(((UnaryMinusExpression) exp).getExpression());
    }
    if (exp instanceof BitwiseNegationExpression) {
        return getType(((BitwiseNegationExpression) exp).getExpression());
    }
    if (exp instanceof MethodCall) {
        MethodNode target = (MethodNode) exp.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
        if (target != null) {
            return getType(target);
        }
    }
    if (exp instanceof Parameter) {
        return ((Parameter) exp).getOriginType();
    }
    if (exp instanceof FieldNode) {
        FieldNode fn = (FieldNode) exp;
        return getGenericsResolvedTypeOfFieldOrProperty(fn, fn.getOriginType());
    }
    if (exp instanceof PropertyNode) {
        PropertyNode pn = (PropertyNode) exp;
        return getGenericsResolvedTypeOfFieldOrProperty(pn, pn.getOriginType());
    }
    return exp instanceof VariableExpression ? ((VariableExpression) exp).getOriginType() : ((Expression) exp).getType();
}
Also used : LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) Variable(org.codehaus.groovy.ast.Variable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodNode(org.codehaus.groovy.ast.MethodNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) GenericsType(org.codehaus.groovy.ast.GenericsType) Parameter(org.codehaus.groovy.ast.Parameter)

Example 57 with GenericsType

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

the class StaticTypeCheckingVisitor method makeThis.

private ClassNode makeThis() {
    ClassNode ret = typeCheckingContext.getEnclosingClassNode();
    if (typeCheckingContext.isInStaticContext) {
        ClassNode staticRet = CLASS_Type.getPlainNodeReference();
        GenericsType gt = new GenericsType(ret);
        staticRet.setGenericsTypes(new GenericsType[] { gt });
        ret = staticRet;
    }
    return ret;
}
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 58 with GenericsType

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

the class StaticTypeCheckingSupport method parameterizeArguments.

/**
     * Given a receiver and a method node, parameterize the method arguments using
     * available generic type information.
     *
     * @param receiver the class
     * @param m the method
     * @return the parameterized arguments
     */
public static Parameter[] parameterizeArguments(final ClassNode receiver, final MethodNode m) {
    Map<String, GenericsType> genericFromReceiver = GenericsUtils.extractPlaceholders(receiver);
    Map<String, GenericsType> contextPlaceholders = extractGenericsParameterMapOfThis(m);
    Parameter[] methodParameters = m.getParameters();
    Parameter[] params = new Parameter[methodParameters.length];
    for (int i = 0; i < methodParameters.length; i++) {
        Parameter methodParameter = methodParameters[i];
        ClassNode paramType = methodParameter.getType();
        params[i] = buildParameter(genericFromReceiver, contextPlaceholders, methodParameter, paramType);
    }
    return params;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType) Parameter(org.codehaus.groovy.ast.Parameter)

Example 59 with GenericsType

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

the class StaticTypeCheckingSupport method applyContextGenerics.

private static void applyContextGenerics(Map<String, GenericsType> resolvedPlaceholders, Map<String, GenericsType> placeholdersFromContext) {
    if (placeholdersFromContext == null)
        return;
    for (Map.Entry<String, GenericsType> entry : resolvedPlaceholders.entrySet()) {
        GenericsType gt = entry.getValue();
        if (gt.isPlaceholder()) {
            String name = gt.getName();
            GenericsType outer = placeholdersFromContext.get(name);
            if (outer == null)
                continue;
            entry.setValue(outer);
        }
    }
}
Also used : GenericsType(org.codehaus.groovy.ast.GenericsType) Map(java.util.Map) HashMap(java.util.HashMap)

Example 60 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)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