Search in sources :

Example 26 with ASTMethodDeclaration

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

the class MethodScopeTest method testMethodName.

@Test
public void testMethodName() {
    parseCode(TEST1);
    ASTMethodDeclaration meth = acu.findDescendantsOfType(ASTMethodDeclaration.class).get(0);
    MethodScope ms = (MethodScope) meth.getScope();
    assertEquals(ms.getName(), "foo");
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Test(org.junit.Test)

Example 27 with ASTMethodDeclaration

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

the class AbstractCommentRuleTest method testCommentAssignments.

@Test
public void testCommentAssignments() {
    LanguageVersionHandler handler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.8").getLanguageVersionHandler();
    Reader source = new StringReader("public class Foo {" + "     /** Comment 1 */\n" + "        public void method1() {}\n" + "    \n" + "        /** Comment 2 */\n" + "    \n" + "        /** Comment 3 */\n" + "        public void method2() {}" + "}");
    Node node = handler.getParser(handler.getDefaultParserOptions()).parse("test", source);
    testSubject.assignCommentsToDeclarations((ASTCompilationUnit) node);
    List<ASTMethodDeclaration> methods = node.findDescendantsOfType(ASTMethodDeclaration.class);
    Assert.assertEquals("/** Comment 1 */", methods.get(0).comment().getImage());
    Assert.assertEquals("/** Comment 3 */", methods.get(1).comment().getImage());
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) Test(org.junit.Test)

Example 28 with ASTMethodDeclaration

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

the class AcceptanceTest method testDemo.

@Test
public void testDemo() {
    parseCode(TEST_DEMO);
    // System.out.println(TEST_DEMO);
    ASTMethodDeclaration node = acu.findDescendantsOfType(ASTMethodDeclaration.class).get(0);
    Scope s = node.getScope();
    Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
    for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : m.entrySet()) {
        assertEquals("buz", entry.getKey().getImage());
        assertEquals("ArrayList", ((TypedNameDeclaration) entry.getKey()).getTypeImage());
        List<NameOccurrence> u = entry.getValue();
        assertEquals(1, u.size());
        NameOccurrence o = u.get(0);
        int beginLine = o.getLocation().getBeginLine();
        assertEquals(3, beginLine);
    // System.out.println("Variable: " + d.getImage());
    // System.out.println("Type: " + d.getTypeImage());
    // System.out.println("Usages: " + u.size());
    // System.out.println("Used in line " + beginLine);
    }
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Scope(net.sourceforge.pmd.lang.symboltable.Scope) 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)

Example 29 with ASTMethodDeclaration

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

the class AcceptanceTest method testEq.

@Test
public void testEq() {
    parseCode(TEST_EQ);
    ASTEqualityExpression e = acu.findDescendantsOfType(ASTEqualityExpression.class).get(0);
    ASTMethodDeclaration method = e.getFirstParentOfType(ASTMethodDeclaration.class);
    Scope s = method.getScope();
    Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
    assertEquals(2, m.size());
    for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : m.entrySet()) {
        NameDeclaration vnd = entry.getKey();
        List<NameOccurrence> usages = entry.getValue();
        if (vnd.getImage().equals("a") || vnd.getImage().equals("b")) {
            assertEquals(1, usages.size());
            assertEquals(3, usages.get(0).getLocation().getBeginLine());
        } else {
            fail("Unkown variable " + vnd);
        }
    }
}
Also used : ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Scope(net.sourceforge.pmd.lang.symboltable.Scope) 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)

Example 30 with ASTMethodDeclaration

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

the class AbstractStructureInspectorPage method showMethod.

/**
 * Confort method to show a method.
 *
 * @param index
 *            index position of the combobox
 */
protected void showMethod(int index) {
    if (index >= 0 && index < pmdMethodList.size()) {
        ASTMethodDeclaration method = pmdMethodList.get(index);
        showMethod(method);
    }
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)

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