Search in sources :

Example 11 with ASTFieldDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.

the class SigMaskTest method testFinalFields.

@Test
public void testFinalFields() {
    List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST_FIELDS);
    JavaFieldSigMask mask = new JavaFieldSigMask();
    mask.forbidFinal();
    for (ASTFieldDeclaration node : nodes) {
        if (node.isFinal()) {
            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)

Example 12 with ASTFieldDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.

the class SigMaskTest method testFieldvisibility.

@Test
public void testFieldvisibility() {
    List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST_FIELDS);
    JavaFieldSigMask mask = new JavaFieldSigMask();
    mask.restrictVisibilitiesTo(Visibility.PUBLIC);
    for (ASTFieldDeclaration node : nodes) {
        if (node.isPublic()) {
            assertTrue(mask.covers(JavaFieldSignature.buildFor(node)));
        } else {
            assertFalse(mask.covers(JavaFieldSignature.buildFor(node)));
        }
    }
    mask.restrictVisibilitiesTo(Visibility.PRIVATE);
    for (ASTFieldDeclaration node : nodes) {
        if (node.isPrivate()) {
            assertTrue(mask.covers(JavaFieldSignature.buildFor(node)));
        } else {
            assertFalse(mask.covers(JavaFieldSignature.buildFor(node)));
        }
    }
    mask.restrictVisibilitiesTo(Visibility.PACKAGE);
    for (ASTFieldDeclaration node : nodes) {
        if (node.isPackagePrivate()) {
            assertTrue(mask.covers(JavaFieldSignature.buildFor(node)));
        } else {
            assertFalse(mask.covers(JavaFieldSignature.buildFor(node)));
        }
    }
    mask.restrictVisibilitiesTo(Visibility.PROTECTED);
    for (ASTFieldDeclaration node : nodes) {
        if (node.isProtected()) {
            assertTrue(mask.covers(JavaFieldSignature.buildFor(node)));
        } else {
            assertFalse(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)

Example 13 with ASTFieldDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.

the class SignatureTest method visibilityTest.

// common to operation and field signatures
@Test
public void visibilityTest() {
    final String TEST = "class Bzaz{ " + "public int bar;" + "String k;" + "protected double d;" + "private int i;" + "protected int x;" + "public Bzaz(){} " + "void bar(){} " + "protected void foo(int x){}" + "private Bzaz(int y){}" + "}";
    List<ASTMethodOrConstructorDeclaration> operationDeclarations = getOrderedNodes(ASTMethodOrConstructorDeclaration.class, TEST);
    List<ASTFieldDeclaration> fieldDeclarations = getOrderedNodes(ASTFieldDeclaration.class, TEST);
    List<JavaSignature> sigs = new ArrayList<>();
    for (ASTMethodOrConstructorDeclaration node : operationDeclarations) {
        sigs.add(JavaOperationSignature.buildFor(node));
    }
    // operations
    assertEquals(Visibility.PUBLIC, sigs.get(0).visibility);
    assertEquals(Visibility.PACKAGE, sigs.get(1).visibility);
    assertEquals(Visibility.PROTECTED, sigs.get(2).visibility);
    assertEquals(Visibility.PRIVATE, sigs.get(3).visibility);
    sigs.clear();
    for (ASTFieldDeclaration node : fieldDeclarations) {
        sigs.add(JavaFieldSignature.buildFor(node));
    }
    // fields
    assertEquals(Visibility.PUBLIC, sigs.get(0).visibility);
    assertEquals(Visibility.PACKAGE, sigs.get(1).visibility);
    assertEquals(Visibility.PROTECTED, sigs.get(2).visibility);
    assertEquals(Visibility.PRIVATE, sigs.get(3).visibility);
}
Also used : ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ArrayList(java.util.ArrayList) JavaSignature(net.sourceforge.pmd.lang.java.multifile.signature.JavaSignature) ASTMethodOrConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration) Test(org.junit.Test)

Example 14 with ASTFieldDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration in project pmd by pmd.

the class SignatureTest method isStaticFieldTest.

@Test
public void isStaticFieldTest() {
    final String TEST = "class Bzaz{" + "public final String x;" + "private int y;" + "private static int a;" + "protected static final double u;" + "static 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).isStatic);
    assertFalse(sigs.get(1).isStatic);
    assertTrue(sigs.get(2).isStatic);
    assertTrue(sigs.get(3).isStatic);
    assertTrue(sigs.get(4).isStatic);
}
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)

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