use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.
the class AccessorMethodGenerationRule method analyzeScope.
private void analyzeScope(final AbstractJavaScope file, final Object data) {
for (final ClassNameDeclaration classDecl : file.getDeclarations(ClassNameDeclaration.class).keySet()) {
final ClassScope classScope = (ClassScope) classDecl.getScope();
// Check fields
for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> varDecl : classScope.getVariableDeclarations().entrySet()) {
final ASTFieldDeclaration field = varDecl.getKey().getNode().getFirstParentOfType(ASTFieldDeclaration.class);
analyzeMember(field, varDecl.getValue(), classScope, data);
}
// Check methods
for (final Map.Entry<MethodNameDeclaration, List<NameOccurrence>> methodDecl : classScope.getMethodDeclarations().entrySet()) {
final ASTMethodDeclaration method = methodDecl.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class);
analyzeMember(method, methodDecl.getValue(), classScope, data);
}
// Check inner classes
analyzeScope(classScope, data);
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.
the class AbstractJavaClassMetric method countMatchingFieldSigs.
/**
* Counts the fields matching the signature mask in this class.
*
* @param classNode The class on which to count
* @param mask The mask
*
* @return The number of fields matching the signature mask
*/
protected int countMatchingFieldSigs(ASTAnyTypeDeclaration classNode, JavaFieldSigMask mask) {
int count = 0;
List<ASTFieldDeclaration> decls = getFields(classNode);
for (ASTFieldDeclaration decl : decls) {
if (mask.covers(decl.getSignature())) {
count++;
}
}
return count;
}
use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.
the class UseUtilityClassRule method visit.
@Override
public Object visit(ASTClassOrInterfaceBody decl, Object data) {
if (decl.jjtGetParent() instanceof ASTClassOrInterfaceDeclaration) {
ASTClassOrInterfaceDeclaration parent = (ASTClassOrInterfaceDeclaration) decl.jjtGetParent();
if (parent.isAbstract() || parent.isInterface() || isExceptionType(parent)) {
return super.visit(decl, data);
}
if (isOkUsingLombok(parent)) {
return super.visit(decl, data);
}
int i = decl.jjtGetNumChildren();
int methodCount = 0;
boolean isOK = false;
while (i > 0) {
Node p = decl.jjtGetChild(--i);
if (p.jjtGetNumChildren() == 0) {
continue;
}
Node n = skipAnnotations(p);
if (n instanceof ASTFieldDeclaration) {
if (!((ASTFieldDeclaration) n).isStatic()) {
isOK = true;
break;
}
} else if (n instanceof ASTConstructorDeclaration) {
if (((ASTConstructorDeclaration) n).isPrivate()) {
isOK = true;
break;
}
} else if (n instanceof ASTMethodDeclaration) {
ASTMethodDeclaration m = (ASTMethodDeclaration) n;
if (!m.isPrivate()) {
methodCount++;
}
if (!m.isStatic()) {
isOK = true;
break;
}
// TODO use symbol table
if (m.getMethodName().equals("suite")) {
ASTResultType res = m.getResultType();
ASTClassOrInterfaceType c = res.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (c != null && c.hasImageEqualTo("Test")) {
isOK = true;
break;
}
}
}
}
if (!isOK && methodCount > 0) {
addViolation(data, decl);
}
}
return super.visit(decl, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration 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);
}
use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.
the class SigMaskTest method testStaticFields.
@Test
public void testStaticFields() {
List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST_FIELDS);
JavaFieldSigMask mask = new JavaFieldSigMask();
mask.forbidStatic();
for (ASTFieldDeclaration node : nodes) {
if (node.isStatic()) {
assertFalse(mask.covers(JavaFieldSignature.buildFor(node)));
} else {
assertTrue(mask.covers(JavaFieldSignature.buildFor(node)));
}
}
}
Aggregations