Search in sources :

Example 16 with ASTMethodDeclaration

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

the class AvoidFieldNameMatchingMethodNameRule method visit.

@Override
public Object visit(ASTClassOrInterfaceBody node, Object data) {
    int n = node.jjtGetNumChildren();
    List<ASTFieldDeclaration> fields = new ArrayList<>();
    Set<String> methodNames = new HashSet<>();
    for (int i = 0; i < n; i++) {
        Node child = node.jjtGetChild(i);
        if (child.jjtGetNumChildren() == 0) {
            continue;
        }
        child = child.jjtGetChild(child.jjtGetNumChildren() - 1);
        if (child instanceof ASTFieldDeclaration) {
            fields.add((ASTFieldDeclaration) child);
        } else if (child instanceof ASTMethodDeclaration) {
            methodNames.add(((ASTMethodDeclaration) child).getMethodName().toLowerCase(Locale.ROOT));
        }
    }
    for (ASTFieldDeclaration field : fields) {
        String varName = field.getVariableName().toLowerCase(Locale.ROOT);
        if (methodNames.contains(varName)) {
            addViolation(data, field, field.getVariableName());
        }
    }
    return super.visit(node, data);
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 17 with ASTMethodDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration 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 18 with ASTMethodDeclaration

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

the class UnusedFormalParameterRule method check.

private void check(Node node, Object data) {
    Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
    if (parent instanceof ASTClassOrInterfaceDeclaration && !((ASTClassOrInterfaceDeclaration) parent).isInterface()) {
        Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((JavaNode) node).getScope().getDeclarations(VariableNameDeclaration.class);
        for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
            VariableNameDeclaration nameDecl = entry.getKey();
            if (actuallyUsed(nameDecl, entry.getValue())) {
                continue;
            }
            addViolation(data, nameDecl.getNode(), new Object[] { node instanceof ASTMethodDeclaration ? "method" : "constructor", nameDecl.getImage() });
        }
    }
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTNameList(net.sourceforge.pmd.lang.java.ast.ASTNameList) List(java.util.List) Map(java.util.Map)

Example 19 with ASTMethodDeclaration

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

the class StatementAndBraceFinderTest method testOnlyWorksForMethodsAndConstructors.

@Test(expected = RuntimeException.class)
public void testOnlyWorksForMethodsAndConstructors() {
    StatementAndBraceFinder sbf = new StatementAndBraceFinder(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler().getDataFlowHandler());
    sbf.buildDataFlowFor(new ASTMethodDeclaration(1));
    sbf.buildDataFlowFor(new ASTConstructorDeclaration(1));
    sbf.buildDataFlowFor(new ASTCompilationUnit(1));
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Test(org.junit.Test)

Example 20 with ASTMethodDeclaration

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

the class StatementAndBraceFinderTest method testStatementExpressionParentChildLinks.

@Test
public void testStatementExpressionParentChildLinks() {
    ASTStatementExpression se = getOrderedNodes(ASTStatementExpression.class, TEST1).get(0);
    ASTMethodDeclaration seParent = (ASTMethodDeclaration) se.getDataFlowNode().getParents().get(0).getNode();
    assertEquals(se, seParent.getDataFlowNode().getChildren().get(0).getNode());
    assertEquals(seParent, se.getDataFlowNode().getParents().get(0).getNode());
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) Test(org.junit.Test)

Aggregations

ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)31 Test (org.junit.Test)15 Node (net.sourceforge.pmd.lang.ast.Node)8 List (java.util.List)6 Map (java.util.Map)5 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)5 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)5 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)5 ArrayList (java.util.ArrayList)4 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)4 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)3 JavaParserVisitorAdapter (net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter)3 RuleContext (net.sourceforge.pmd.RuleContext)2 Structure (net.sourceforge.pmd.lang.dfa.Structure)2 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)2 ASTResultType (net.sourceforge.pmd.lang.java.ast.ASTResultType)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)2