Search in sources :

Example 21 with VariableNameDeclaration

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

the class LocalVariableCouldBeFinalRule method visit.

public Object visit(ASTLocalVariableDeclaration node, Object data) {
    if (node.isFinal()) {
        return data;
    }
    Scope s = node.getScope();
    Map<VariableNameDeclaration, List<NameOccurrence>> decls = s.getDeclarations(VariableNameDeclaration.class);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) {
        VariableNameDeclaration var = entry.getKey();
        if (var.getAccessNodeParent() != node) {
            continue;
        }
        if (!assigned(entry.getValue())) {
            addViolation(data, var.getAccessNodeParent(), var.getImage());
        }
    }
    return data;
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) List(java.util.List) Map(java.util.Map)

Example 22 with VariableNameDeclaration

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

Example 23 with VariableNameDeclaration

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

the class ClassTypeResolverTest method testFullyQualifiedType.

/**
 * The type should be filled also on the ASTVariableDeclaratorId node, not
 * only on the variable name declaration.
 */
@Test
public void testFullyQualifiedType() {
    String source = "public class Foo {\n" + "    public void bar() {\n" + "        java.util.StringTokenizer st = new StringTokenizer(\"a.b.c.d\", \".\");\n" + "        while (st.hasMoreTokens()) {\n" + "            System.out.println(st.nextToken());\n" + "        }\n" + "    }\n" + "}";
    ASTCompilationUnit acu = parseAndTypeResolveForString(source, "1.5");
    List<ASTName> names = acu.findDescendantsOfType(ASTName.class);
    ASTName theStringTokenizer = null;
    for (ASTName name : names) {
        if (name.hasImageEqualTo("st.hasMoreTokens")) {
            theStringTokenizer = name;
            break;
        }
    }
    Assert.assertNotNull(theStringTokenizer);
    VariableNameDeclaration declaration = (VariableNameDeclaration) theStringTokenizer.getNameDeclaration();
    Assert.assertNotNull(declaration);
    Assert.assertEquals("java.util.StringTokenizer", declaration.getTypeImage());
    Assert.assertNotNull(declaration.getType());
    Assert.assertSame(StringTokenizer.class, declaration.getType());
    ASTVariableDeclaratorId id = (ASTVariableDeclaratorId) declaration.getNode();
    Assert.assertNotNull(id.getType());
    Assert.assertSame(StringTokenizer.class, id.getType());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Test(org.junit.Test)

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