Search in sources :

Example 6 with VariableNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.

the class UnusedPrivateFieldRule method visit.

@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    boolean classHasLombok = hasLombokAnnotation(node);
    Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations(VariableNameDeclaration.class);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
        VariableNameDeclaration decl = entry.getKey();
        AccessNode accessNodeParent = decl.getAccessNodeParent();
        if (!accessNodeParent.isPrivate() || isOK(decl.getImage()) || classHasLombok || hasIgnoredAnnotation((Annotatable) accessNodeParent)) {
            continue;
        }
        if (!actuallyUsed(entry.getValue())) {
            if (!usedInOuterClass(node, decl) && !usedInOuterEnum(node, decl)) {
                addViolation(data, decl.getNode(), decl.getImage());
            }
        }
    }
    return super.visit(node, data);
}
Also used : Annotatable(net.sourceforge.pmd.lang.java.ast.Annotatable) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ArrayList(java.util.ArrayList) List(java.util.List) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) Map(java.util.Map)

Example 7 with VariableNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.

the class AccessorMethodGenerationRule method analyzeScope.

private void analyzeScope(final AbstractJavaScope file, final Object data) {
    for (final ClassNameDeclaration classDecl : file.getDeclarations(ClassNameDeclaration.class).keySet()) {
        final ClassScope classScope = (ClassScope) classDecl.getScope();
        // Check fields
        for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> varDecl : classScope.getVariableDeclarations().entrySet()) {
            final ASTFieldDeclaration field = varDecl.getKey().getNode().getFirstParentOfType(ASTFieldDeclaration.class);
            analyzeMember(field, varDecl.getValue(), classScope, data);
        }
        // Check methods
        for (final Map.Entry<MethodNameDeclaration, List<NameOccurrence>> methodDecl : classScope.getMethodDeclarations().entrySet()) {
            final ASTMethodDeclaration method = methodDecl.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class);
            analyzeMember(method, methodDecl.getValue(), classScope, data);
        }
        // Check inner classes
        analyzeScope(classScope, data);
    }
}
Also used : MethodNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ClassNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.ClassNameDeclaration) List(java.util.List) Map(java.util.Map) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 8 with VariableNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.

the class UnnecessaryLocalBeforeReturnRule method visit.

@Override
public Object visit(ASTReturnStatement rtn, Object data) {
    // skip returns of literals
    ASTName name = rtn.getFirstDescendantOfType(ASTName.class);
    if (name == null) {
        return data;
    }
    // skip 'complicated' expressions
    if (rtn.findDescendantsOfType(ASTExpression.class).size() > 1 || rtn.findDescendantsOfType(ASTPrimaryExpression.class).size() > 1 || isMethodCall(rtn)) {
        return data;
    }
    Map<VariableNameDeclaration, List<NameOccurrence>> vars = name.getScope().getDeclarations(VariableNameDeclaration.class);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
        VariableNameDeclaration variableDeclaration = entry.getKey();
        List<NameOccurrence> usages = entry.getValue();
        if (usages.size() == 1) {
            // If there is more than 1 usage, then it's not only returned
            NameOccurrence occ = usages.get(0);
            if (occ.getLocation().equals(name) && isNotAnnotated(variableDeclaration)) {
                String var = name.getImage();
                if (var.indexOf('.') != -1) {
                    var = var.substring(0, var.indexOf('.'));
                }
                // Is the variable initialized with another member that is later used?
                if (!isInitDataModifiedAfterInit(variableDeclaration, rtn) && !statementsBeforeReturn(variableDeclaration, rtn)) {
                    addViolation(data, rtn, var);
                }
            }
        }
    }
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) List(java.util.List) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 9 with VariableNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.

the class UnnecessaryLocalBeforeReturnRule method isInitDataModifiedAfterInit.

private boolean isInitDataModifiedAfterInit(final VariableNameDeclaration variableDeclaration, final ASTReturnStatement rtn) {
    final ASTVariableInitializer initializer = variableDeclaration.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class);
    if (initializer != null) {
        final List<ASTName> referencedNames = initializer.findDescendantsOfType(ASTName.class);
        for (final ASTName refName : referencedNames) {
            // TODO : Shouldn't the scope allow us to search for a var name occurrences directly, moving up through parent scopes?
            Scope scope = refName.getScope();
            do {
                final Map<VariableNameDeclaration, List<NameOccurrence>> declarations = scope.getDeclarations(VariableNameDeclaration.class);
                for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
                    if (entry.getKey().getName().equals(refName.getImage())) {
                        // Variable found! Check usage locations
                        for (final NameOccurrence occ : entry.getValue()) {
                            final ScopedNode location = occ.getLocation();
                            // Is it used after initializing our "unnecessary" local but before the return statement?
                            if (isAfter(location, initializer) && isAfter(rtn, location)) {
                                return true;
                            }
                        }
                        return false;
                    }
                }
                scope = scope.getParent();
            } while (scope != null);
        }
    }
    return false;
}
Also used : ScopedNode(net.sourceforge.pmd.lang.symboltable.ScopedNode) Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTVariableInitializer(net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) List(java.util.List) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 10 with VariableNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.

the class MethodArgumentCouldBeFinalRule method lookForViolation.

private void lookForViolation(Scope scope, Object data) {
    Map<VariableNameDeclaration, List<NameOccurrence>> decls = scope.getDeclarations(VariableNameDeclaration.class);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) {
        VariableNameDeclaration var = entry.getKey();
        AccessNode node = var.getAccessNodeParent();
        if (!node.isFinal() && node instanceof ASTFormalParameter && !assigned(entry.getValue())) {
            addViolation(data, node, var.getImage());
        }
    }
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) List(java.util.List) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Map(java.util.Map)

Aggregations

VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)23 List (java.util.List)18 Map (java.util.Map)15 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)8 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)7 ArrayList (java.util.ArrayList)6 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)6 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 JavaNameOccurrence (net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 ClassScope (net.sourceforge.pmd.lang.java.symboltable.ClassScope)3 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 ASTForInit (net.sourceforge.pmd.lang.java.ast.ASTForInit)2 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)2 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)2 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)2 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)2 ASTVariableInitializer (net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer)2