View Javadoc

1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.rules;
5   
6   import net.sourceforge.pmd.AbstractRule;
7   import net.sourceforge.pmd.RuleContext;
8   import net.sourceforge.pmd.RuleViolation;
9   import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
10  import net.sourceforge.pmd.ast.ASTMethodDeclaration;
11  
12  import java.text.MessageFormat;
13  
14  
15  
16  public class AvoidNonConstructorMethodsWithClassName extends AbstractRule {
17  	
18  	public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
19          if (node.isInterface()) {
20      		return data;
21          }
22          return super.visit(node, data);
23  	}
24  	
25  	/* (non-Javadoc)
26  	 * @see net.sourceforge.pmd.ast.JavaParserVisitor#visit(net.sourceforge.pmd.ast.ASTFieldDeclaration, java.lang.Object)
27  	 */
28  	public Object visit(ASTMethodDeclaration node, Object data) {
29  		String methodName = node.getMethodName();
30  		String declaringType = getDeclaringType (node);
31  		if (methodName!=null && declaringType!=null) {
32  			if (methodName.equals(declaringType)) {
33  				RuleContext ctx = (RuleContext) data;
34  				RuleViolation ruleViolation = createRuleViolation(ctx, node, MessageFormat.format(getMessage(), new Object[]{methodName}));
35                  ctx.getReport().addRuleViolation(ruleViolation);
36  			}
37  		}
38  		return data;
39  	}
40  	
41  }