Search in sources :

Example 21 with ASTMethodDeclaration

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

the class StatementAndBraceFinderTest method testVariableDeclaratorParentChildLinks.

@Test
public void testVariableDeclaratorParentChildLinks() {
    ASTVariableDeclarator vd = getOrderedNodes(ASTVariableDeclarator.class, TEST2).get(0);
    ASTMethodDeclaration vdParent = (ASTMethodDeclaration) vd.getDataFlowNode().getParents().get(0).getNode();
    assertEquals(vd, vdParent.getDataFlowNode().getChildren().get(0).getNode());
    assertEquals(vdParent, vd.getDataFlowNode().getParents().get(0).getNode());
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) Test(org.junit.Test)

Example 22 with ASTMethodDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration 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 23 with ASTMethodDeclaration

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

the class SignatureTest method testSetterDetection.

@Test
public void testSetterDetection() {
    ASTCompilationUnit compilationUnit = parseJava17(SetterDetection.class);
    compilationUnit.jjtAccept(new JavaParserVisitorAdapter() {

        @Override
        public Object visit(ASTMethodDeclaration node, Object data) {
            assertEquals(Role.GETTER_OR_SETTER, Role.get(node));
            return data;
        }
    }, null);
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) JavaParserVisitorAdapter(net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter) Test(org.junit.Test)

Example 24 with ASTMethodDeclaration

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

the class ClassScopeTest method testMethodDeclarationRecorded.

@Test
public void testMethodDeclarationRecorded() {
    parseCode(METHOD_DECLARATIONS_RECORDED);
    ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
    ClassScope s = (ClassScope) n.getScope();
    Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
    assertEquals(1, m.size());
    MethodNameDeclaration mnd = (MethodNameDeclaration) m.keySet().iterator().next();
    assertEquals("bar", mnd.getImage());
    ASTMethodDeclaration node = (ASTMethodDeclaration) mnd.getNode().jjtGetParent();
    assertTrue(node.isPrivate());
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Example 25 with ASTMethodDeclaration

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

the class ClassScopeTest method testNestedClassFieldAndParameter.

/**
 * Test case for bug report #2410201
 */
@Test
public void testNestedClassFieldAndParameter() {
    parseCode(NESTED_CLASS_FIELD_AND_PARAM);
    ASTMethodDeclaration node = acu.getFirstDescendantOfType(ASTMethodDeclaration.class);
    Map<NameDeclaration, List<NameOccurrence>> vd = node.getScope().getDeclarations();
    assertEquals(1, vd.size());
    for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : vd.entrySet()) {
        assertEquals("field", entry.getKey().getImage());
        List<NameOccurrence> occurrences = entry.getValue();
        assertEquals(2, occurrences.size());
        NameOccurrence no1 = occurrences.get(0);
        assertEquals(8, no1.getLocation().getBeginLine());
        NameOccurrence no2 = occurrences.get(1);
        assertEquals(9, no2.getLocation().getBeginLine());
    }
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Aggregations

ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)31 Test (org.junit.Test)15 Node (net.sourceforge.pmd.lang.ast.Node)8 List (java.util.List)6 Map (java.util.Map)5 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)5 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)5 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)5 ArrayList (java.util.ArrayList)4 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)4 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)3 JavaParserVisitorAdapter (net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter)3 RuleContext (net.sourceforge.pmd.RuleContext)2 Structure (net.sourceforge.pmd.lang.dfa.Structure)2 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)2 ASTResultType (net.sourceforge.pmd.lang.java.ast.ASTResultType)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)2