Search in sources :

Example 1 with ASTFieldDeclaration

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

Example 2 with ASTFieldDeclaration

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

Example 3 with ASTFieldDeclaration

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

Example 4 with ASTFieldDeclaration

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

Example 5 with ASTFieldDeclaration

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

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