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;
}
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);
}
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());
}
Aggregations