use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.
the class JavaRuleViolationTest method testMethodName.
/**
* Tests that the method name is taken correctly from the given node.
*
* @see <a href="https://sourceforge.net/p/pmd/bugs/1250/">#1250</a>
*/
@Test
public void testMethodName() {
ASTCompilationUnit ast = parse("class Foo { void bar(int x) {} }");
ASTMethodDeclaration md = ast.getFirstDescendantOfType(ASTMethodDeclaration.class);
final RuleContext context = new RuleContext();
final JavaRuleViolation violation = new JavaRuleViolation(null, context, md, null);
assertEquals("bar", violation.getMethodName());
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.
the class StatementAndBraceFinder method buildDataFlowFor.
public void buildDataFlowFor(JavaNode node) {
if (!(node instanceof ASTMethodDeclaration) && !(node instanceof ASTConstructorDeclaration)) {
throw new RuntimeException("Can't build a data flow for anything other than a method or a constructor");
}
this.dataFlow = new Structure(dataFlowHandler);
this.dataFlow.createStartNode(node.getBeginLine());
this.dataFlow.createNewNode(node);
node.jjtAccept(this, dataFlow);
this.dataFlow.createEndNode(node.getEndLine());
if (LOGGER.isLoggable(Level.FINE)) {
// TODO SRT Remove after development
LOGGER.fine("DataFlow is " + this.dataFlow.dump());
}
Linker linker = new Linker(dataFlowHandler, dataFlow.getBraceStack(), dataFlow.getContinueBreakReturnStack());
try {
linker.computePaths();
} catch (SequenceException | LinkerException e) {
e.printStackTrace();
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.
the class BeanMembersShouldSerializeRule method isBeanAccessor.
private boolean isBeanAccessor(ASTMethodDeclarator meth) {
String methodName = meth.getImage();
if (methodName.startsWith("get") || methodName.startsWith("set")) {
return true;
}
if (methodName.startsWith("is")) {
ASTResultType ret = ((ASTMethodDeclaration) meth.jjtGetParent()).getResultType();
List<ASTPrimitiveType> primitives = ret.findDescendantsOfType(ASTPrimitiveType.class);
if (!primitives.isEmpty() && primitives.get(0).isBoolean()) {
return true;
}
}
return false;
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.
the class TestClassWithoutTestCasesRule method visit.
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
if (node.isAbstract() || node.isInterface() || node.isNested()) {
return data;
}
List<ASTMethodDeclaration> m = node.findDescendantsOfType(ASTMethodDeclaration.class);
boolean testsFound = false;
if (m != null) {
for (ASTMethodDeclaration md : m) {
if (isJUnitMethod(md, data)) {
testsFound = true;
break;
}
}
}
if (!testsFound) {
addViolation(data, node);
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration 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);
}
Aggregations