Search in sources :

Example 6 with AccessNode

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

the class ConstructorCallsOverridableMethodRule method visit.

/**
 * Create a MethodHolder to hold the method. Store the MethodHolder in the
 * Map as the key Store each method called by the current method as a List
 * in the Map as the Object
 */
@Override
public Object visit(ASTMethodDeclarator node, Object data) {
    if (!(getCurrentEvalPackage() instanceof NullEvalPackage)) {
        // only evaluate if we have an eval package for this class
        AccessNode parent = (AccessNode) node.jjtGetParent();
        MethodHolder h = new MethodHolder(node);
        if (!parent.isAbstract() && !parent.isPrivate() && !parent.isStatic() && !parent.isFinal()) {
            // Skip abstract methods, have a separate rule for that
            // this method is overridable
            h.setDangerous();
            ASTMethodDeclaration decl = node.getFirstParentOfType(ASTMethodDeclaration.class);
            h.setCalledMethod(decl.getMethodName());
        }
        List<MethodInvocation> l = new ArrayList<>();
        addCalledMethodsOfNode(parent, l, getCurrentEvalPackage().className);
        getCurrentEvalPackage().allMethodsOfClass.put(h, l);
    }
    return super.visit(node, data);
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ArrayList(java.util.ArrayList) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode)

Example 7 with AccessNode

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

the class AssignmentToNonFinalStaticRule method visit.

public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    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 = (AccessNode) decl.getAccessNodeParent();
        if (!accessNodeParent.isStatic() || accessNodeParent.isFinal()) {
            continue;
        }
        if (initializedInConstructor(entry.getValue())) {
            addViolation(data, decl.getNode(), decl.getImage());
        }
    }
    return super.visit(node, data);
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) List(java.util.List) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) Map(java.util.Map)

Aggregations

AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)7 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)6 List (java.util.List)5 Map (java.util.Map)5 ArrayList (java.util.ArrayList)3 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)1 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)1 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)1 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 Annotatable (net.sourceforge.pmd.lang.java.ast.Annotatable)1 ClassScope (net.sourceforge.pmd.lang.java.symboltable.ClassScope)1 MethodNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration)1