use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.
the class AvoidFieldNameMatchingMethodNameRule method visit.
@Override
public Object visit(ASTClassOrInterfaceBody node, Object data) {
int n = node.jjtGetNumChildren();
List<ASTFieldDeclaration> fields = new ArrayList<>();
Set<String> methodNames = new HashSet<>();
for (int i = 0; i < n; i++) {
Node child = node.jjtGetChild(i);
if (child.jjtGetNumChildren() == 0) {
continue;
}
child = child.jjtGetChild(child.jjtGetNumChildren() - 1);
if (child instanceof ASTFieldDeclaration) {
fields.add((ASTFieldDeclaration) child);
} else if (child instanceof ASTMethodDeclaration) {
methodNames.add(((ASTMethodDeclaration) child).getMethodName().toLowerCase(Locale.ROOT));
}
}
for (ASTFieldDeclaration field : fields) {
String varName = field.getVariableName().toLowerCase(Locale.ROOT);
if (methodNames.contains(varName)) {
addViolation(data, field, field.getVariableName());
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.
the class ConstructorCallsOverridableMethodRule method visit.
/**
* Create a MethodHolder to hold the method. Store the MethodHolder in the
* Map as the key Store each method called by the current method as a List
* in the Map as the Object
*/
@Override
public Object visit(ASTMethodDeclarator node, Object data) {
if (!(getCurrentEvalPackage() instanceof NullEvalPackage)) {
// only evaluate if we have an eval package for this class
AccessNode parent = (AccessNode) node.jjtGetParent();
MethodHolder h = new MethodHolder(node);
if (!parent.isAbstract() && !parent.isPrivate() && !parent.isStatic() && !parent.isFinal()) {
// Skip abstract methods, have a separate rule for that
// this method is overridable
h.setDangerous();
ASTMethodDeclaration decl = node.getFirstParentOfType(ASTMethodDeclaration.class);
h.setCalledMethod(decl.getMethodName());
}
List<MethodInvocation> l = new ArrayList<>();
addCalledMethodsOfNode(parent, l, getCurrentEvalPackage().className);
getCurrentEvalPackage().allMethodsOfClass.put(h, l);
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.
the class UnusedFormalParameterRule method check.
private void check(Node node, Object data) {
Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
if (parent instanceof ASTClassOrInterfaceDeclaration && !((ASTClassOrInterfaceDeclaration) parent).isInterface()) {
Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((JavaNode) node).getScope().getDeclarations(VariableNameDeclaration.class);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
VariableNameDeclaration nameDecl = entry.getKey();
if (actuallyUsed(nameDecl, entry.getValue())) {
continue;
}
addViolation(data, nameDecl.getNode(), new Object[] { node instanceof ASTMethodDeclaration ? "method" : "constructor", nameDecl.getImage() });
}
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.
the class StatementAndBraceFinderTest method testOnlyWorksForMethodsAndConstructors.
@Test(expected = RuntimeException.class)
public void testOnlyWorksForMethodsAndConstructors() {
StatementAndBraceFinder sbf = new StatementAndBraceFinder(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler().getDataFlowHandler());
sbf.buildDataFlowFor(new ASTMethodDeclaration(1));
sbf.buildDataFlowFor(new ASTConstructorDeclaration(1));
sbf.buildDataFlowFor(new ASTCompilationUnit(1));
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.
the class StatementAndBraceFinderTest method testStatementExpressionParentChildLinks.
@Test
public void testStatementExpressionParentChildLinks() {
ASTStatementExpression se = getOrderedNodes(ASTStatementExpression.class, TEST1).get(0);
ASTMethodDeclaration seParent = (ASTMethodDeclaration) se.getDataFlowNode().getParents().get(0).getNode();
assertEquals(se, seParent.getDataFlowNode().getChildren().get(0).getNode());
assertEquals(seParent, se.getDataFlowNode().getParents().get(0).getNode());
}
Aggregations