Search in sources :

Example 6 with JavaOperationSigMask

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

Example 7 with JavaOperationSigMask

use of net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSigMask 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)));
        }
    }
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) JavaOperationSigMask(net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSigMask) ASTMethodOrConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration) Test(org.junit.Test)

Example 8 with JavaOperationSigMask

use of net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSigMask 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()));
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) JavaOperationSigMask(net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSigMask) JavaOperationSignature(net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSignature) JavaOperationQualifiedName(net.sourceforge.pmd.lang.java.qname.JavaOperationQualifiedName) ASTMethodOrConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration) Test(org.junit.Test)

Aggregations

JavaOperationSigMask (net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSigMask)8 Test (org.junit.Test)6 ASTMethodOrConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration)4 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)3 JavaOperationSignature (net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSignature)2 JavaOperationQualifiedName (net.sourceforge.pmd.lang.java.qname.JavaOperationQualifiedName)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 JavaParserVisitorAdapter (net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter)1 JavaQualifiedName (net.sourceforge.pmd.lang.java.ast.JavaQualifiedName)1 Role (net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSignature.Role)1