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");
}
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());
}
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);
}
}
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);
}
}
}
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);
}
}
Aggregations