Search in sources :

Example 1 with ClassNode

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

the class WideningCategories method areEqualWithGenerics.

/**
     * Compares two class nodes, but including their generics types.
     * @param a
     * @param b
     * @return true if the class nodes are equal, false otherwise
     */
private static boolean areEqualWithGenerics(ClassNode a, ClassNode b) {
    if (a == null)
        return b == null;
    if (!a.equals(b))
        return false;
    if (a.isUsingGenerics() && !b.isUsingGenerics())
        return false;
    GenericsType[] gta = a.getGenericsTypes();
    GenericsType[] gtb = b.getGenericsTypes();
    if (gta == null && gtb != null)
        return false;
    if (gtb == null && gta != null)
        return false;
    if (gta != null && gtb != null) {
        if (gta.length != gtb.length)
            return false;
        for (int i = 0; i < gta.length; i++) {
            GenericsType ga = gta[i];
            GenericsType gb = gtb[i];
            boolean result = ga.isPlaceholder() == gb.isPlaceholder() && ga.isWildcard() == gb.isWildcard();
            result = result && ga.isResolved() && gb.isResolved();
            result = result && ga.getName().equals(gb.getName());
            result = result && areEqualWithGenerics(ga.getType(), gb.getType());
            result = result && areEqualWithGenerics(ga.getLowerBound(), gb.getLowerBound());
            if (result) {
                ClassNode[] upA = ga.getUpperBounds();
                if (upA != null) {
                    ClassNode[] upB = gb.getUpperBounds();
                    if (upB == null || upB.length != upA.length)
                        return false;
                    for (int j = 0; j < upA.length; j++) {
                        if (!areEqualWithGenerics(upA[j], upB[j]))
                            return false;
                    }
                }
            }
            if (!result)
                return false;
        }
    }
    return true;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 2 with ClassNode

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

the class AnnotationVisitor method visitConstantExpression.

protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) {
    ClassNode constType = constExpr.getType();
    ClassNode wrapperType = ClassHelper.getWrapper(constType);
    if (!hasCompatibleType(attrType, wrapperType)) {
        addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "'; but found type '" + constType.getName() + "'", constExpr);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode)

Example 3 with ClassNode

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

the class AnnotationVisitor method checkCircularReference.

public void checkCircularReference(ClassNode searchClass, ClassNode attrType, Expression startExp) {
    if (!isValidAnnotationClass(attrType))
        return;
    if (!(startExp instanceof AnnotationConstantExpression)) {
        addError("Found '" + startExp.getText() + "' when expecting an Annotation Constant", startExp);
        return;
    }
    AnnotationConstantExpression ace = (AnnotationConstantExpression) startExp;
    AnnotationNode annotationNode = (AnnotationNode) ace.getValue();
    if (annotationNode.getClassNode().equals(searchClass)) {
        addError("Circular reference discovered in " + searchClass.getName(), startExp);
        return;
    }
    ClassNode cn = annotationNode.getClassNode();
    for (MethodNode method : cn.getMethods()) {
        if (method.getReturnType().equals(searchClass)) {
            addError("Circular reference discovered in " + cn.getName(), startExp);
        }
        ReturnStatement code = (ReturnStatement) method.getCode();
        if (code == null)
            continue;
        checkCircularReference(searchClass, method.getReturnType(), code.getExpression());
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 4 with ClassNode

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

the class AnnotationVisitor method visit.

public AnnotationNode visit(AnnotationNode node) {
    this.annotation = node;
    this.reportClass = node.getClassNode();
    if (!isValidAnnotationClass(node.getClassNode())) {
        addError("class " + node.getClassNode().getName() + " is not an annotation");
        return node;
    }
    // check if values have been passed for all annotation attributes that don't have defaults
    if (!checkIfMandatoryAnnotationValuesPassed(node)) {
        return node;
    }
    // if enum constants have been used, check if they are all valid
    if (!checkIfValidEnumConstsAreUsed(node)) {
        return node;
    }
    Map<String, Expression> attributes = node.getMembers();
    for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
        String attrName = entry.getKey();
        Expression attrExpr = transformInlineConstants(entry.getValue());
        entry.setValue(attrExpr);
        ClassNode attrType = getAttributeType(node, attrName);
        visitExpression(attrName, attrExpr, attrType);
    }
    VMPluginFactory.getPlugin().configureAnnotation(node);
    return this.annotation;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Map(java.util.Map)

Example 5 with ClassNode

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

the class AnnotationVisitor method checkIfMandatoryAnnotationValuesPassed.

private boolean checkIfMandatoryAnnotationValuesPassed(AnnotationNode node) {
    boolean ok = true;
    Map attributes = node.getMembers();
    ClassNode classNode = node.getClassNode();
    for (MethodNode mn : classNode.getMethods()) {
        String methodName = mn.getName();
        // if the annotation attribute has a default, getCode() returns a ReturnStatement with the default value
        if (mn.getCode() == null && !attributes.containsKey(methodName)) {
            addError("No explicit/default value found for annotation attribute '" + methodName + "'", node);
            ok = false;
        }
    }
    return ok;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) Map(java.util.Map)

Aggregations

ClassNode (org.codehaus.groovy.ast.ClassNode)850 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)329 MethodNode (org.codehaus.groovy.ast.MethodNode)192 GenericsType (org.codehaus.groovy.ast.GenericsType)149 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)148 Expression (org.codehaus.groovy.ast.expr.Expression)140 Parameter (org.codehaus.groovy.ast.Parameter)133 FieldNode (org.codehaus.groovy.ast.FieldNode)125 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)114 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)112 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)107 ArrayList (java.util.ArrayList)95 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)95 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)86 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)73 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)71 LinkedList (java.util.LinkedList)70 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)69 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)59 MethodVisitor (org.objectweb.asm.MethodVisitor)57