Search in sources :

Example 1 with ASTAnnotation

use of net.sourceforge.pmd.lang.java.ast.ASTAnnotation in project pmd by pmd.

the class SignatureDeclareThrowsExceptionRule method visit.

@Override
public Object visit(ASTMethodDeclaration methodDeclaration, Object o) {
    if (junitImported && isAllowedMethod(methodDeclaration)) {
        return super.visit(methodDeclaration, o);
    }
    if (methodDeclaration.getMethodName().startsWith("test")) {
        return super.visit(methodDeclaration, o);
    }
    // Ignore overridden methods, the issue should be marked on the method definition
    final List<ASTAnnotation> methodAnnotations = methodDeclaration.jjtGetParent().findChildrenOfType(ASTAnnotation.class);
    for (final ASTAnnotation annotation : methodAnnotations) {
        final ASTName annotationName = annotation.getFirstDescendantOfType(ASTName.class);
        if (annotationName.hasImageEqualTo("Override") || annotationName.hasImageEqualTo("java.lang.Override")) {
            return super.visit(methodDeclaration, o);
        }
    }
    checkExceptions(methodDeclaration, o);
    return super.visit(methodDeclaration, o);
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTAnnotation(net.sourceforge.pmd.lang.java.ast.ASTAnnotation)

Example 2 with ASTAnnotation

use of net.sourceforge.pmd.lang.java.ast.ASTAnnotation in project pmd by pmd.

the class UseUtilityClassRule method skipAnnotations.

private Node skipAnnotations(Node p) {
    int index = 0;
    Node n = p.jjtGetChild(index++);
    while (n instanceof ASTAnnotation && index < p.jjtGetNumChildren()) {
        n = p.jjtGetChild(index++);
    }
    return n;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTAnnotation(net.sourceforge.pmd.lang.java.ast.ASTAnnotation)

Example 3 with ASTAnnotation

use of net.sourceforge.pmd.lang.java.ast.ASTAnnotation in project pmd by pmd.

the class JUnitUseExpectedRule method visit.

@Override
public Object visit(ASTClassOrInterfaceBodyDeclaration node, Object data) {
    boolean inAnnotation = false;
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        Node child = node.jjtGetChild(i);
        if (child instanceof ASTAnnotation) {
            ASTName annotationName = child.getFirstDescendantOfType(ASTName.class);
            if ("Test".equals(annotationName.getImage())) {
                inAnnotation = true;
                continue;
            }
        }
        if (child instanceof ASTMethodDeclaration) {
            boolean isJUnitMethod = isJUnitMethod((ASTMethodDeclaration) child, data);
            if (inAnnotation || isJUnitMethod) {
                List<Node> found = new ArrayList<>();
                found.addAll((List<Node>) visit((ASTMethodDeclaration) child, data));
                for (Node name : found) {
                    addViolation(data, name);
                }
            }
        }
        inAnnotation = false;
    }
    return super.visit(node, data);
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTAnnotation(net.sourceforge.pmd.lang.java.ast.ASTAnnotation) ArrayList(java.util.ArrayList)

Example 4 with ASTAnnotation

use of net.sourceforge.pmd.lang.java.ast.ASTAnnotation in project pmd by pmd.

the class AbstractJUnitRule method doesNodeContainJUnitAnnotation.

private boolean doesNodeContainJUnitAnnotation(Node node, String annotationTypeClassName) {
    List<ASTAnnotation> annotations = node.findDescendantsOfType(ASTAnnotation.class);
    for (ASTAnnotation annotation : annotations) {
        Node annotationTypeNode = annotation.jjtGetChild(0);
        TypeNode annotationType = (TypeNode) annotationTypeNode;
        if (annotationType.getType() == null) {
            ASTName name = annotationTypeNode.getFirstChildOfType(ASTName.class);
            if (name != null && (name.hasImageEqualTo("Test") || name.hasImageEqualTo(annotationTypeClassName))) {
                return true;
            }
        } else if (TypeHelper.isA(annotationType, annotationTypeClassName)) {
            return true;
        }
    }
    return false;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTAnnotation(net.sourceforge.pmd.lang.java.ast.ASTAnnotation) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode)

Example 5 with ASTAnnotation

use of net.sourceforge.pmd.lang.java.ast.ASTAnnotation in project pmd by pmd.

the class AbstractLombokAwareRule method hasLombokAnnotation.

/**
 * @deprecated As of release 6.2.0, replaced by {@link #hasLombokAnnotation(Annotatable)}
 * Checks whether the given node is annotated with any lombok annotation.
 * The node can be any node, e.g. class declaration or field declaration.
 *
 * @param node
 *            the node to check
 * @return <code>true</code> if a lombok annotation has been found
 */
@Deprecated
protected boolean hasLombokAnnotation(Node node) {
    boolean result = false;
    Node parent = node.jjtGetParent();
    List<ASTAnnotation> annotations = parent.findChildrenOfType(ASTAnnotation.class);
    for (ASTAnnotation annotation : annotations) {
        ASTName name = annotation.getFirstDescendantOfType(ASTName.class);
        if (name != null) {
            String annotationName = name.getImage();
            if (lombokImported) {
                if (LOMBOK_ANNOTATIONS.contains(annotationName)) {
                    result = true;
                }
            } else {
                if (annotationName.startsWith(LOMBOK_PACKAGE + ".")) {
                    String shortName = annotationName.substring(LOMBOK_PACKAGE.length() + 1);
                    if (LOMBOK_ANNOTATIONS.contains(shortName)) {
                        result = true;
                    }
                }
            }
        }
    }
    return result;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTAnnotation(net.sourceforge.pmd.lang.java.ast.ASTAnnotation)

Aggregations

ASTAnnotation (net.sourceforge.pmd.lang.java.ast.ASTAnnotation)7 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)6 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)2 ArrayList (java.util.ArrayList)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)1 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)1 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTFormalParameters (net.sourceforge.pmd.lang.java.ast.ASTFormalParameters)1 ASTMarkerAnnotation (net.sourceforge.pmd.lang.java.ast.ASTMarkerAnnotation)1 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)1 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)1 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)1 ASTStatement (net.sourceforge.pmd.lang.java.ast.ASTStatement)1 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)1 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)1