use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator in project pmd by pmd.
the class StdCyclomaticComplexityRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
entryStack.push(new Entry(node));
super.visit(node, data);
Entry methodEntry = entryStack.pop();
if (!isSuppressed(node)) {
int methodDecisionPoints = methodEntry.decisionPoints;
Entry classEntry = entryStack.peek();
classEntry.methodCount++;
classEntry.bumpDecisionPoints(methodDecisionPoints);
if (methodDecisionPoints > classEntry.highestDecisionPoints) {
classEntry.highestDecisionPoints = methodDecisionPoints;
}
ASTMethodDeclarator methodDeclarator = null;
for (int n = 0; n < node.jjtGetNumChildren(); n++) {
Node childNode = node.jjtGetChild(n);
if (childNode instanceof ASTMethodDeclarator) {
methodDeclarator = (ASTMethodDeclarator) childNode;
break;
}
}
if (showMethodsComplexity && methodEntry.decisionPoints >= reportLevel) {
addViolation(data, node, new String[] { "method", methodDeclarator == null ? "" : methodDeclarator.getImage(), String.valueOf(methodEntry.decisionPoints) });
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator in project pmd by pmd.
the class SuspiciousHashcodeMethodNameRule method visit.
public Object visit(ASTMethodDeclaration node, Object data) {
/*
* original XPath rule was //MethodDeclaration [ResultType
* //PrimitiveType [@Image='int'] [//MethodDeclarator [@Image='hashcode'
* or @Image='HashCode' or @Image='Hashcode']
* [not(FormalParameters/*)]]]
*/
ASTResultType type = node.getResultType();
ASTMethodDeclarator decl = node.getFirstChildOfType(ASTMethodDeclarator.class);
String name = decl.getImage();
if ("hashcode".equalsIgnoreCase(name) && !"hashCode".equals(name) && decl.jjtGetChild(0).jjtGetNumChildren() == 0 && type.jjtGetNumChildren() != 0) {
Node t = type.jjtGetChild(0).jjtGetChild(0);
if (t instanceof ASTPrimitiveType && "int".equals(t.getImage())) {
addViolation(data, node);
return data;
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator in project pmd by pmd.
the class ClassScope method createBuiltInMethodDeclaration.
/**
* Creates a fake method name declaration for built-in methods from Java
* like the Enum Method "valueOf".
*
* @param methodName
* the method name
* @param parameterTypes
* the reference types of each parameter of the method
* @return a method name declaration
*/
private MethodNameDeclaration createBuiltInMethodDeclaration(final String methodName, final String... parameterTypes) {
ASTMethodDeclaration methodDeclaration = new ASTMethodDeclaration(JavaParserTreeConstants.JJTMETHODDECLARATION);
methodDeclaration.setPublic(true);
methodDeclaration.setScope(this);
ASTMethodDeclarator methodDeclarator = new ASTMethodDeclarator(JavaParserTreeConstants.JJTMETHODDECLARATOR);
methodDeclarator.setImage(methodName);
methodDeclarator.setScope(this);
ASTFormalParameters formalParameters = new ASTFormalParameters(JavaParserTreeConstants.JJTFORMALPARAMETERS);
formalParameters.setScope(this);
methodDeclaration.jjtAddChild(methodDeclarator, 0);
methodDeclarator.jjtSetParent(methodDeclaration);
methodDeclarator.jjtAddChild(formalParameters, 0);
formalParameters.jjtSetParent(methodDeclarator);
/*
* jjtAddChild resizes it's child node list according to known indexes.
* Going backwards makes sure the first time it gets the right size avoiding copies.
*/
for (int i = parameterTypes.length - 1; i >= 0; i--) {
ASTFormalParameter formalParameter = new ASTFormalParameter(JavaParserTreeConstants.JJTFORMALPARAMETER);
formalParameters.jjtAddChild(formalParameter, i);
formalParameter.jjtSetParent(formalParameters);
ASTVariableDeclaratorId variableDeclaratorId = new ASTVariableDeclaratorId(JavaParserTreeConstants.JJTVARIABLEDECLARATORID);
variableDeclaratorId.setImage("arg" + i);
formalParameter.jjtAddChild(variableDeclaratorId, 1);
variableDeclaratorId.jjtSetParent(formalParameter);
ASTType type = new ASTType(JavaParserTreeConstants.JJTTYPE);
formalParameter.jjtAddChild(type, 0);
type.jjtSetParent(formalParameter);
if (PRIMITIVE_TYPES.contains(parameterTypes[i])) {
ASTPrimitiveType primitiveType = new ASTPrimitiveType(JavaParserTreeConstants.JJTPRIMITIVETYPE);
primitiveType.setImage(parameterTypes[i]);
type.jjtAddChild(primitiveType, 0);
primitiveType.jjtSetParent(type);
} else {
ASTReferenceType referenceType = new ASTReferenceType(JavaParserTreeConstants.JJTREFERENCETYPE);
type.jjtAddChild(referenceType, 0);
referenceType.jjtSetParent(type);
// TODO : this could actually be a primitive array...
ASTClassOrInterfaceType classOrInterfaceType = new ASTClassOrInterfaceType(JavaParserTreeConstants.JJTCLASSORINTERFACETYPE);
classOrInterfaceType.setImage(parameterTypes[i]);
referenceType.jjtAddChild(classOrInterfaceType, 0);
classOrInterfaceType.jjtSetParent(referenceType);
}
}
return new MethodNameDeclaration(methodDeclarator);
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator in project pmd by pmd.
the class ScopeAndDeclarationFinder method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
createMethodScope(node);
ASTMethodDeclarator md = node.getFirstChildOfType(ASTMethodDeclarator.class);
node.getScope().getEnclosingScope(ClassScope.class).addDeclaration(new MethodNameDeclaration(md));
cont(node);
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator in project pmd-eclipse-plugin by pmd.
the class ASTUtil method parameterTypes.
public static String parameterTypes(ASTMethodDeclaration node) {
StringBuilder sb = new StringBuilder();
for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) {
Node sn = node.jjtGetChild(ix);
if (sn instanceof ASTMethodDeclarator) {
List<ASTFormalParameter> allParams = ((ASTMethodDeclarator) sn).findDescendantsOfType(ASTFormalParameter.class);
for (ASTFormalParameter formalParam : allParams) {
AbstractNode param = formalParam.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (param == null) {
param = formalParam.getFirstDescendantOfType(ASTPrimitiveType.class);
}
if (param == null) {
continue;
}
sb.append(param.getImage()).append(", ");
}
}
}
int length = sb.length();
return length == 0 ? "" : sb.toString().substring(0, length - 2);
}
Aggregations