Search in sources :

Example 1 with ASTMethodDeclarator

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

Example 2 with ASTMethodDeclarator

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

Example 3 with ASTMethodDeclarator

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);
}
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)

Example 4 with ASTMethodDeclarator

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

Example 5 with ASTMethodDeclarator

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);
}
Also used : ASTMethodDeclarator(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator) ASTPrimitiveType(net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType) AbstractNode(net.sourceforge.pmd.lang.ast.AbstractNode) AbstractNode(net.sourceforge.pmd.lang.ast.AbstractNode) Node(net.sourceforge.pmd.lang.ast.Node) AbstractJavaAccessNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessNode) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)

Aggregations

ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)12 Node (net.sourceforge.pmd.lang.ast.Node)4 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)4 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)3 DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTFormalParameters (net.sourceforge.pmd.lang.java.ast.ASTFormalParameters)2 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 Test (org.junit.Test)2 InvalidObjectException (java.io.InvalidObjectException)1 ObjectInputStream (java.io.ObjectInputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 AbstractNode (net.sourceforge.pmd.lang.ast.AbstractNode)1 DAAPathFinder (net.sourceforge.pmd.lang.dfa.pathfinder.DAAPathFinder)1 ASTAnnotation (net.sourceforge.pmd.lang.java.ast.ASTAnnotation)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)1