Search in sources :

Example 6 with ASTFieldDeclaration

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);
    }
}
Also used : MethodNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ClassNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.ClassNameDeclaration) List(java.util.List) Map(java.util.Map) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 7 with ASTFieldDeclaration

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;
}
Also used : ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)

Example 8 with ASTFieldDeclaration

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);
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ASTResultType(net.sourceforge.pmd.lang.java.ast.ASTResultType) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 9 with ASTFieldDeclaration

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);
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 10 with ASTFieldDeclaration

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)));
        }
    }
}
Also used : ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) JavaFieldSigMask(net.sourceforge.pmd.lang.java.multifile.signature.JavaFieldSigMask) Test(org.junit.Test)

Aggregations

ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)14 Test (org.junit.Test)8 JavaFieldSigMask (net.sourceforge.pmd.lang.java.multifile.signature.JavaFieldSigMask)5 ArrayList (java.util.ArrayList)4 JavaFieldSignature (net.sourceforge.pmd.lang.java.multifile.signature.JavaFieldSignature)4 Node (net.sourceforge.pmd.lang.ast.Node)3 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)3 ASTResultType (net.sourceforge.pmd.lang.java.ast.ASTResultType)2 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)1 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTMethodOrConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration)1 JavaSignature (net.sourceforge.pmd.lang.java.multifile.signature.JavaSignature)1 JavaTypeQualifiedName (net.sourceforge.pmd.lang.java.qname.JavaTypeQualifiedName)1 ClassNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.ClassNameDeclaration)1 ClassScope (net.sourceforge.pmd.lang.java.symboltable.ClassScope)1