Search in sources :

Example 11 with JavaNode

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

the class ScopeAndDeclarationFinder method createClassScope.

/**
 * Creates a new class scope for an AST node. The scope on top of the stack
 * is set as the parent of the new scope, which is then also stored on the
 * scope stack.
 *
 * @param node
 *            the AST node for which the scope has to be created.
 * @throws java.util.EmptyStackException
 *             if the scope stack is empty.
 */
private void createClassScope(JavaNode node) {
    Scope s = ((JavaNode) node.jjtGetParent()).getScope();
    ClassNameDeclaration classNameDeclaration = new ClassNameDeclaration(node);
    s.addDeclaration(classNameDeclaration);
    if (node instanceof ASTClassOrInterfaceBody) {
        addScope(new ClassScope(classNameDeclaration), node);
    } else {
        addScope(new ClassScope(node.getImage(), classNameDeclaration), node);
    }
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTClassOrInterfaceBody(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)

Example 12 with JavaNode

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

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

the class AbstractNcssCountRule method countNodeChildren.

/**
 * Count the number of children of the given Java node. Adds one to count
 * the node itself.
 *
 * @param node
 *            java node having children counted
 * @param data
 *            node data
 * @return count of the number of children of the node, plus one
 */
protected Integer countNodeChildren(Node node, Object data) {
    Integer nodeCount = null;
    int lineCount = 0;
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        nodeCount = (Integer) ((JavaNode) node.jjtGetChild(i)).jjtAccept(this, data);
        lineCount += nodeCount.intValue();
    }
    return ++lineCount;
}
Also used : DataPoint(net.sourceforge.pmd.stat.DataPoint) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Aggregations

JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)13 List (java.util.List)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Node (net.sourceforge.pmd.lang.ast.Node)2 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)2 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)2 DummyJavaNode (net.sourceforge.pmd.lang.java.ast.DummyJavaNode)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 EnumTest (net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)2 Scope (net.sourceforge.pmd.lang.symboltable.Scope)2 DataPoint (net.sourceforge.pmd.stat.DataPoint)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)1 StartOrEndDataFlowNode (net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)1 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)1 ASTClassOrInterfaceBody (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody)1