Search in sources :

Example 11 with ASTFormalParameter

use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameter in project pmd by pmd.

the class MethodArgumentCouldBeFinalRule method lookForViolation.

private void lookForViolation(Scope scope, Object data) {
    Map<VariableNameDeclaration, List<NameOccurrence>> decls = scope.getDeclarations(VariableNameDeclaration.class);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) {
        VariableNameDeclaration var = entry.getKey();
        AccessNode node = var.getAccessNodeParent();
        if (!node.isFinal() && node instanceof ASTFormalParameter && !assigned(entry.getValue())) {
            addViolation(data, node, var.getImage());
        }
    }
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) List(java.util.List) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Map(java.util.Map)

Example 12 with ASTFormalParameter

use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameter 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)

Example 13 with ASTFormalParameter

use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameter in project pmd by pmd.

the class QualifiedNameResolver method getOperationName.

/**
 * Returns a normalized method name (not Java-canonical!).
 */
private static String getOperationName(String methodName, ASTFormalParameters params) {
    StringBuilder sb = new StringBuilder();
    sb.append(methodName);
    sb.append('(');
    boolean first = true;
    for (ASTFormalParameter param : params) {
        if (!first) {
            sb.append(", ");
        }
        first = false;
        sb.append(param.getTypeNode().getTypeImage());
        if (param.isVarargs()) {
            sb.append("...");
        }
    }
    sb.append(')');
    return sb.toString();
}
Also used : ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)

Example 14 with ASTFormalParameter

use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameter in project pmd by pmd.

the class ArrayIsStoredDirectlyRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    final ASTFormalParameters params = node.getFirstDescendantOfType(ASTFormalParameters.class);
    ASTFormalParameter[] arrs = getArrays(params);
    checkAll(data, arrs, node.findDescendantsOfType(ASTBlockStatement.class));
    return data;
}
Also used : ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)

Example 15 with ASTFormalParameter

use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameter in project pmd by pmd.

the class MethodNameDeclaration method hashCode.

@Override
public int hashCode() {
    int hash = node.getImage().hashCode() * 31 + ((ASTMethodDeclarator) node).getParameterCount();
    ASTFormalParameters myParams = (ASTFormalParameters) node.jjtGetChild(0);
    for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
        ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i);
        Node myTypeNode = myParam.getTypeNode().jjtGetChild(0);
        String myTypeImg;
        if (myTypeNode instanceof ASTPrimitiveType) {
            myTypeImg = myTypeNode.getImage();
        } else {
            myTypeImg = myTypeNode.jjtGetChild(0).getImage();
        }
        hash = hash * 31 + myTypeImg.hashCode();
    }
    return hash;
}
Also used : ASTPrimitiveType(net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType) Node(net.sourceforge.pmd.lang.ast.Node) ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)

Aggregations

ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)21 ASTFormalParameters (net.sourceforge.pmd.lang.java.ast.ASTFormalParameters)8 Node (net.sourceforge.pmd.lang.ast.Node)7 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)5 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)4 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ArrayList (java.util.ArrayList)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 Test (org.junit.Test)3 List (java.util.List)2 Map (java.util.Map)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)2 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)2 ASTTryStatement (net.sourceforge.pmd.lang.java.ast.ASTTryStatement)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 InvalidObjectException (java.io.InvalidObjectException)1 ObjectInputStream (java.io.ObjectInputStream)1 RuleContext (net.sourceforge.pmd.RuleContext)1 AbstractNode (net.sourceforge.pmd.lang.ast.AbstractNode)1