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