Search in sources :

Example 1 with ASTMethodDeclaration

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());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) RuleContext(net.sourceforge.pmd.RuleContext) Test(org.junit.Test)

Example 2 with ASTMethodDeclaration

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();
    }
}
Also used : LinkerException(net.sourceforge.pmd.lang.dfa.LinkerException) ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) SequenceException(net.sourceforge.pmd.lang.dfa.SequenceException) Structure(net.sourceforge.pmd.lang.dfa.Structure) Linker(net.sourceforge.pmd.lang.dfa.Linker)

Example 3 with ASTMethodDeclaration

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;
}
Also used : ASTPrimitiveType(net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTResultType(net.sourceforge.pmd.lang.java.ast.ASTResultType)

Example 4 with ASTMethodDeclaration

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;
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)

Example 5 with ASTMethodDeclaration

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);
}
Also used : ASTMethodDeclarator(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator) ASTPrimitiveType(net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Aggregations

ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)31 Test (org.junit.Test)15 Node (net.sourceforge.pmd.lang.ast.Node)8 List (java.util.List)6 Map (java.util.Map)5 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)5 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)5 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)5 ArrayList (java.util.ArrayList)4 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)4 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)3 JavaParserVisitorAdapter (net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter)3 RuleContext (net.sourceforge.pmd.RuleContext)2 Structure (net.sourceforge.pmd.lang.dfa.Structure)2 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)2 ASTResultType (net.sourceforge.pmd.lang.java.ast.ASTResultType)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)2