use of net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration in project pmd by pmd.
the class SigMaskTest method testOperationVisibility.
@Test
public void testOperationVisibility() {
List<ASTMethodOrConstructorDeclaration> nodes = getOrderedNodes(ASTMethodOrConstructorDeclaration.class, TEST_OPERATIONS);
JavaOperationSigMask mask = new JavaOperationSigMask();
mask.coverAbstract();
mask.restrictVisibilitiesTo(Visibility.PUBLIC);
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node.isPublic()) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
mask.restrictVisibilitiesTo(Visibility.PRIVATE);
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node.isPrivate()) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
mask.restrictVisibilitiesTo(Visibility.PACKAGE);
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node.isPackagePrivate()) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
mask.restrictVisibilitiesTo(Visibility.PROTECTED);
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node.isProtected()) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration in project pmd by pmd.
the class SigMaskTest method testOperationRoles.
@Test
public void testOperationRoles() {
List<ASTMethodOrConstructorDeclaration> nodes = getOrderedNodes(ASTMethodOrConstructorDeclaration.class, TEST_OPERATIONS);
JavaOperationSigMask mask = new JavaOperationSigMask();
mask.restrictRolesTo(Role.STATIC);
mask.coverAbstract();
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node.isStatic()) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
mask.restrictRolesTo(Role.CONSTRUCTOR);
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node instanceof ASTConstructorDeclaration) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
mask.restrictRolesTo(Role.GETTER_OR_SETTER);
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node instanceof ASTMethodDeclaration && ((ASTMethodDeclaration) node).getMethodName().matches("(get|set).*")) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
mask.restrictRolesTo(Role.METHOD);
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node instanceof ASTMethodDeclaration && !node.isStatic() && !((ASTMethodDeclaration) node).getMethodName().matches("(get|set).*")) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration 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);
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration in project pmd by pmd.
the class PackageStatsTest method testAddOperation.
@Test
public void testAddOperation() {
final String TEST = "package org.foo; class Boo{ " + "public void foo(){}}";
ASTMethodOrConstructorDeclaration node = getOrderedNodes(ASTMethodDeclaration.class, TEST).get(0);
JavaOperationQualifiedName qname = node.getQualifiedName();
JavaOperationSignature signature = JavaOperationSignature.buildFor(node);
assertFalse(pack.hasMatchingSig(qname, new JavaOperationSigMask()));
ClassStats clazz = pack.getClassStats(qname.getClassName(), true);
clazz.addOperation("foo()", signature);
assertTrue(pack.hasMatchingSig(qname, new JavaOperationSigMask()));
}
Aggregations