Search in sources :

Example 1 with ASTFormalParameters

use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameters 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 2 with ASTFormalParameters

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

the class MethodNameDeclaration method getParameterDisplaySignature.

public String getParameterDisplaySignature() {
    StringBuilder sb = new StringBuilder("(");
    ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0);
    // no need to trim at the end
    for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
        ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(i);
        sb.append(p.getTypeNode().getTypeImage());
        if (p.isVarargs()) {
            sb.append("...");
        }
        sb.append(',');
    }
    if (sb.charAt(sb.length() - 1) == ',') {
        sb.deleteCharAt(sb.length() - 1);
    }
    sb.append(')');
    return sb.toString();
}
Also used : ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)

Example 3 with ASTFormalParameters

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

the class MethodNameDeclaration method equals.

@Override
public boolean equals(Object o) {
    if (!(o instanceof MethodNameDeclaration)) {
        return false;
    }
    MethodNameDeclaration other = (MethodNameDeclaration) o;
    // compare name
    if (!other.node.getImage().equals(node.getImage())) {
        return false;
    }
    // params, too
    if (((ASTMethodDeclarator) other.node).getParameterCount() != ((ASTMethodDeclarator) node).getParameterCount()) {
        return false;
    }
    // compare parameter types
    ASTFormalParameters myParams = (ASTFormalParameters) node.jjtGetChild(0);
    ASTFormalParameters otherParams = (ASTFormalParameters) other.node.jjtGetChild(0);
    for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
        ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i);
        ASTFormalParameter otherParam = (ASTFormalParameter) otherParams.jjtGetChild(i);
        // Compare vararg
        if (myParam.isVarargs() != otherParam.isVarargs()) {
            return false;
        }
        Node myTypeNode = myParam.getTypeNode().jjtGetChild(0);
        Node otherTypeNode = otherParam.getTypeNode().jjtGetChild(0);
        // compare primitive vs reference type
        if (myTypeNode.getClass() != otherTypeNode.getClass()) {
            return false;
        }
        // simple comparison of type images
        // this can be fooled by one method using "String"
        // and the other method using "java.lang.String"
        // once we get real types in here that should get fixed
        String myTypeImg;
        String otherTypeImg;
        if (myTypeNode instanceof ASTPrimitiveType) {
            myTypeImg = myTypeNode.getImage();
            otherTypeImg = otherTypeNode.getImage();
        } else {
            myTypeImg = myTypeNode.jjtGetChild(0).getImage();
            otherTypeImg = otherTypeNode.jjtGetChild(0).getImage();
        }
        if (!myTypeImg.equals(otherTypeImg)) {
            return false;
        }
    // if type is ASTPrimitiveType and is an array, make sure the other
    // one is also
    }
    return true;
}
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)

Example 4 with ASTFormalParameters

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

the class ScopeAndDeclarationFinder method visit.

@Override
public Object visit(ASTConstructorDeclaration node, Object data) {
    /*
         * Local variables declared inside the constructor need to be in a
         * different scope so special handling is needed
         */
    createMethodScope(node);
    Scope methodScope = node.getScope();
    Node formalParameters = node.jjtGetChild(0);
    int i = 1;
    int n = node.jjtGetNumChildren();
    if (!(formalParameters instanceof ASTFormalParameters)) {
        visit((ASTTypeParameters) formalParameters, data);
        formalParameters = node.jjtGetChild(1);
        i++;
    }
    visit((ASTFormalParameters) formalParameters, data);
    Scope localScope = null;
    for (; i < n; i++) {
        JavaNode b = (JavaNode) node.jjtGetChild(i);
        if (b instanceof ASTBlockStatement) {
            if (localScope == null) {
                createLocalScope(node);
                localScope = node.getScope();
            }
            b.setScope(localScope);
            visit(b, data);
        } else {
            visit(b, data);
        }
    }
    if (localScope != null) {
        // pop the local scope
        scopes.pop();
        // reset the correct scope for the constructor
        node.setScope(methodScope);
    }
    // pop the method scope
    scopes.pop();
    return data;
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)

Example 5 with ASTFormalParameters

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

Aggregations

ASTFormalParameters (net.sourceforge.pmd.lang.java.ast.ASTFormalParameters)9 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)8 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)3 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 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 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)1 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)1 ASTMarkerAnnotation (net.sourceforge.pmd.lang.java.ast.ASTMarkerAnnotation)1 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)1 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)1