Search in sources :

Example 31 with ASTName

use of net.sourceforge.pmd.lang.java.ast.ASTName 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)

Example 32 with ASTName

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

the class AtfdBaseVisitor method getMethodOrAttributeName.

private String getMethodOrAttributeName(ASTPrimaryExpression node) {
    ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
    ASTName name = prefix.getFirstDescendantOfType(ASTName.class);
    String methodOrAttributeName = null;
    if (name != null) {
        int dotIndex = name.getImage().indexOf(".");
        if (dotIndex > -1) {
            methodOrAttributeName = name.getImage().substring(dotIndex + 1);
        }
    }
    return methodOrAttributeName;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Example 33 with ASTName

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

the class AtfdBaseVisitor method getNameImage.

private String getNameImage(ASTPrimaryExpression node) {
    ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
    ASTName name = prefix.getFirstDescendantOfType(ASTName.class);
    String image = null;
    if (name != null) {
        image = name.getImage();
    }
    return image;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Example 34 with ASTName

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

the class UselessStringValueOfRule method isPrimitive.

private static boolean isPrimitive(Node parent) {
    boolean result = false;
    if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
        Node child = parent.jjtGetChild(0);
        if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
            Node gc = child.jjtGetChild(0);
            if (gc instanceof ASTName) {
                ASTName name = (ASTName) gc;
                NameDeclaration nd = name.getNameDeclaration();
                if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) {
                    result = true;
                }
            } else if (gc instanceof ASTLiteral) {
                result = !((ASTLiteral) gc).isStringLiteral();
            }
        }
    }
    return result;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 35 with ASTName

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

the class ClassScope method addNameOccurrence.

public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
    JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
    Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
    if (!declarations.isEmpty() && (javaOccurrence.isMethodOrConstructorInvocation() || javaOccurrence.isMethodReference())) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = getMethodDeclarations().get(decl);
            if (nameOccurrences == null) {
                // search inner classes
                for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
                    Scope innerClassScope = innerClass.getScope();
                    if (innerClassScope.contains(javaOccurrence)) {
                        innerClassScope.addNameOccurrence(javaOccurrence);
                    }
                }
            } else {
                nameOccurrences.add(javaOccurrence);
                Node n = javaOccurrence.getLocation();
                if (n instanceof ASTName) {
                    ((ASTName) n).setNameDeclaration(decl);
                }
            // TODO what to do with PrimarySuffix case?
            }
        }
    } else if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
            if (nameOccurrences == null) {
                // search inner classes
                for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
                    Scope innerClassScope = innerClass.getScope();
                    if (innerClassScope.contains(javaOccurrence)) {
                        innerClassScope.addNameOccurrence(javaOccurrence);
                    }
                }
            } else {
                nameOccurrences.add(javaOccurrence);
                Node n = javaOccurrence.getLocation();
                if (n instanceof ASTName) {
                    ((ASTName) n).setNameDeclaration(decl);
                }
            // TODO what to do with PrimarySuffix case?
            }
        }
    }
    return declarations;
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) ArrayList(java.util.ArrayList) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Aggregations

ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)53 Node (net.sourceforge.pmd.lang.ast.Node)25 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)17 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)14 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)11 ArrayList (java.util.ArrayList)10 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)10 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)8 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)8 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)7 List (java.util.List)6 ASTAnnotation (net.sourceforge.pmd.lang.java.ast.ASTAnnotation)6 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)6 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)6 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)5 Map (java.util.Map)4 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)4 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)3