use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.
the class TooManyFieldsRule method visit.
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
final int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR);
int counter = 0;
final List<ASTFieldDeclaration> l = node.findDescendantsOfType(ASTFieldDeclaration.class);
for (ASTFieldDeclaration fd : l) {
if (fd.isFinal() && fd.isStatic()) {
continue;
}
counter++;
}
if (counter > maxFields) {
addViolation(data, node);
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.
the class SigMaskTest method testEmptyFieldMask.
/**
* Ensure any field is covered by a newly created mask.
*/
@Test
public void testEmptyFieldMask() {
List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST_FIELDS);
SigMask<JavaFieldSignature> mask = new JavaFieldSigMask();
for (ASTFieldDeclaration node : nodes) {
assertTrue(mask.covers(JavaFieldSignature.buildFor(node)));
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.
the class SignatureTest method isFinalFieldTest.
@Test
public void isFinalFieldTest() {
final String TEST = "class Bzaz{" + "public String x;" + "private int y;" + "private final int a;" + "protected final double u;" + "final long v;" + "}";
List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST);
List<JavaFieldSignature> sigs = new ArrayList<>();
for (ASTFieldDeclaration node : nodes) {
sigs.add(JavaFieldSignature.buildFor(node));
}
assertFalse(sigs.get(0).isFinal);
assertFalse(sigs.get(1).isFinal);
assertTrue(sigs.get(2).isFinal);
assertTrue(sigs.get(3).isFinal);
assertTrue(sigs.get(4).isFinal);
}
use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.
the class PackageStatsTest method testAddField.
@Test
public void testAddField() {
final String TEST = "package org.foo; class Boo{ " + "public String bar;}";
ASTFieldDeclaration node = getOrderedNodes(ASTFieldDeclaration.class, TEST).get(0);
JavaTypeQualifiedName qname = (JavaTypeQualifiedName) QualifiedNameFactory.ofString("org.foo.Boo");
String fieldName = "bar";
JavaFieldSignature signature = JavaFieldSignature.buildFor(node);
assertFalse(pack.hasMatchingSig(qname, fieldName, new JavaFieldSigMask()));
ClassStats clazz = pack.getClassStats(qname, true);
clazz.addField(fieldName, signature);
assertTrue(pack.hasMatchingSig(qname, fieldName, new JavaFieldSigMask()));
}
use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.
the class LooseCouplingRule method visit.
// TODO - these should be brought in via external properties
// private static final Set implClassNames = CollectionUtil.asSet( new
// Object[] {
// "ArrayList", "HashSet", "HashMap", "LinkedHashMap", "LinkedHashSet",
// "TreeSet", "TreeMap", "Vector",
// "java.util.ArrayList", "java.util.HashSet", "java.util.HashMap",
// "java.util.LinkedHashMap", "java.util.LinkedHashSet",
// "java.util.TreeSet",
// "java.util.TreeMap", "java.util.Vector"
// });
@Override
public Object visit(ASTClassOrInterfaceType node, Object data) {
if (methodHasOverride(node)) {
return data;
}
Node parent = node.getNthParent(3);
Class<?> clazzType = node.getType();
boolean isType = CollectionUtil.isCollectionType(clazzType, false);
if (isType && (parent instanceof ASTFieldDeclaration || parent instanceof ASTFormalParameter || parent instanceof ASTResultType)) {
addViolation(data, node, node.getImage());
}
return data;
}
Aggregations