Search in sources :

Example 1 with ASTMethodOrConstructorDeclaration

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

the class ProjectMemoizerTest method visitWith.

private List<Integer> visitWith(ASTCompilationUnit acu, final boolean force) {
    final JavaProjectMemoizer toplevel = JavaMetrics.getFacade().getLanguageSpecificProjectMemoizer();
    final List<Integer> result = new ArrayList<>();
    acu.jjtAccept(new JavaParserVisitorReducedAdapter() {

        @Override
        public Object visit(ASTMethodOrConstructorDeclaration node, Object data) {
            MetricMemoizer<MethodLikeNode> op = toplevel.getOperationMemoizer(node.getQualifiedName());
            result.add((int) JavaMetricsComputer.INSTANCE.computeForOperation(opMetricKey, node, force, MetricOptions.emptyOptions(), op));
            return super.visit(node, data);
        }

        @Override
        public Object visit(ASTAnyTypeDeclaration node, Object data) {
            MetricMemoizer<ASTAnyTypeDeclaration> clazz = toplevel.getClassMemoizer(node.getQualifiedName());
            result.add((int) JavaMetricsComputer.INSTANCE.computeForType(classMetricKey, node, force, MetricOptions.emptyOptions(), clazz));
            return super.visit(node, data);
        }
    }, null);
    return result;
}
Also used : ArrayList(java.util.ArrayList) JavaParserVisitorReducedAdapter(net.sourceforge.pmd.lang.java.ast.JavaParserVisitorReducedAdapter) MetricMemoizer(net.sourceforge.pmd.lang.metrics.MetricMemoizer) ASTAnyTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration) ASTMethodOrConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration)

Example 2 with ASTMethodOrConstructorDeclaration

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

the class SignatureTest method isAbstractOperationTest.

@Test
public void isAbstractOperationTest() {
    final String TEST = "abstract class Bzaz{ int x; " + "public static abstract void foo();" + "protected abstract int bar(int x);" + "int getX(){return x;}" + "void setX(int a){x=a;}" + "public void doSomething(){}}";
    List<ASTMethodOrConstructorDeclaration> nodes = getOrderedNodes(ASTMethodOrConstructorDeclaration.class, TEST);
    List<JavaOperationSignature> sigs = new ArrayList<>();
    for (ASTMethodOrConstructorDeclaration node : nodes) {
        sigs.add(JavaOperationSignature.buildFor(node));
    }
    assertTrue(sigs.get(0).isAbstract);
    assertTrue(sigs.get(1).isAbstract);
    assertFalse(sigs.get(2).isAbstract);
    assertFalse(sigs.get(3).isAbstract);
    assertFalse(sigs.get(4).isAbstract);
}
Also used : ArrayList(java.util.ArrayList) JavaOperationSignature(net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSignature) ASTMethodOrConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration) Test(org.junit.Test)

Example 3 with ASTMethodOrConstructorDeclaration

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

the class SignatureTest method operationRoleTest.

@Test
public void operationRoleTest() {
    final String TEST = "class Bzaz{ int x; " + "public static void foo(){} " + "Bzaz(){} " + "int getX(){return x;}" + " void setX(int a){x=a;}" + " public void doSomething(){}}";
    List<ASTMethodOrConstructorDeclaration> nodes = getOrderedNodes(ASTMethodOrConstructorDeclaration.class, TEST);
    List<JavaOperationSignature> sigs = new ArrayList<>();
    for (ASTMethodOrConstructorDeclaration node : nodes) {
        sigs.add(JavaOperationSignature.buildFor(node));
    }
    assertEquals(Role.STATIC, sigs.get(0).role);
    assertEquals(Role.CONSTRUCTOR, sigs.get(1).role);
    assertEquals(Role.GETTER_OR_SETTER, sigs.get(2).role);
    assertEquals(Role.GETTER_OR_SETTER, sigs.get(3).role);
    assertEquals(Role.METHOD, sigs.get(4).role);
}
Also used : ArrayList(java.util.ArrayList) JavaOperationSignature(net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSignature) ASTMethodOrConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration) Test(org.junit.Test)

Example 4 with ASTMethodOrConstructorDeclaration

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

the class AbstractJavaClassMetric method countMatchingOpSigs.

/**
 * Counts the operations matching the signature mask in this class.
 *
 * @param classNode The class on which to count
 * @param mask      The mask
 *
 * @return The number of operations matching the signature mask
 */
protected int countMatchingOpSigs(ASTAnyTypeDeclaration classNode, JavaOperationSigMask mask) {
    int count = 0;
    List<ASTMethodOrConstructorDeclaration> decls = getMethodsAndConstructors(classNode);
    for (ASTMethodOrConstructorDeclaration decl : decls) {
        if (mask.covers(decl.getSignature())) {
            count++;
        }
    }
    return count;
}
Also used : ASTMethodOrConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration)

Example 5 with ASTMethodOrConstructorDeclaration

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

the class SigMaskTest method testEmptyOperationMask.

/**
 * Ensure any non-abstract method is covered by a newly created mask.
 */
@Test
public void testEmptyOperationMask() {
    List<ASTMethodOrConstructorDeclaration> nodes = getOrderedNodes(ASTMethodOrConstructorDeclaration.class, TEST_OPERATIONS);
    SigMask<JavaOperationSignature> mask = new JavaOperationSigMask();
    for (ASTMethodOrConstructorDeclaration node : nodes) {
        if (node.isAbstract()) {
            assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
        } else {
            assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
        }
    }
}
Also used : JavaOperationSigMask(net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSigMask) JavaOperationSignature(net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSignature) ASTMethodOrConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration) Test(org.junit.Test)

Aggregations

ASTMethodOrConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration)9 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)4 JavaOperationSigMask (net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSigMask)4 JavaOperationSignature (net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSignature)4 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)2 ASTAnyTypeDeclaration (net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)1 JavaParserVisitorReducedAdapter (net.sourceforge.pmd.lang.java.ast.JavaParserVisitorReducedAdapter)1 JavaSignature (net.sourceforge.pmd.lang.java.multifile.signature.JavaSignature)1 JavaOperationQualifiedName (net.sourceforge.pmd.lang.java.qname.JavaOperationQualifiedName)1 MetricMemoizer (net.sourceforge.pmd.lang.metrics.MetricMemoizer)1