Search in sources :

Example 6 with ASTFormalParameter

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

the class ClassScope method determineParameterTypes.

/**
 * Provide a list of types of the parameters of the given method
 * declaration. The types are simple type images.
 *
 * @param mnd
 *            the method declaration.
 * @return List of types
 */
private List<TypedNameDeclaration> determineParameterTypes(MethodNameDeclaration mnd) {
    List<ASTFormalParameter> parameters = mnd.getMethodNameDeclaratorNode().findDescendantsOfType(ASTFormalParameter.class);
    if (parameters.isEmpty()) {
        return Collections.emptyList();
    }
    List<TypedNameDeclaration> parameterTypes = new ArrayList<>(parameters.size());
    SourceFileScope fileScope = getEnclosingScope(SourceFileScope.class);
    Map<String, Node> qualifiedTypeNames = fileScope.getQualifiedTypeNames();
    for (ASTFormalParameter p : parameters) {
        String typeImage = p.getTypeNode().getTypeImage();
        // typeImage might be qualified/unqualified. If it refers to a type,
        // defined in the same toplevel class,
        // we should normalize the name here.
        // It might also refer to a type, that is imported.
        typeImage = qualifyTypeName(typeImage);
        Node declaringNode = qualifiedTypeNames.get(typeImage);
        Class<?> resolvedType = fileScope.resolveType(typeImage);
        if (resolvedType == null) {
            resolvedType = resolveGenericType(p, typeImage);
        }
        parameterTypes.add(new SimpleTypedNameDeclaration(typeImage, resolvedType, determineSuper(declaringNode)));
    }
    return parameterTypes;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ArrayList(java.util.ArrayList) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)

Example 7 with ASTFormalParameter

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

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

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

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

the class LooseCouplingRule method visit.

// TODO - these should be brought in via external properties
// private static final Set implClassNames = CollectionUtil.asSet( new
// Object[] {
// "ArrayList", "HashSet", "HashMap", "LinkedHashMap", "LinkedHashSet",
// "TreeSet", "TreeMap", "Vector",
// "java.util.ArrayList", "java.util.HashSet", "java.util.HashMap",
// "java.util.LinkedHashMap", "java.util.LinkedHashSet",
// "java.util.TreeSet",
// "java.util.TreeMap", "java.util.Vector"
// });
@Override
public Object visit(ASTClassOrInterfaceType node, Object data) {
    if (methodHasOverride(node)) {
        return data;
    }
    Node parent = node.getNthParent(3);
    Class<?> clazzType = node.getType();
    boolean isType = CollectionUtil.isCollectionType(clazzType, false);
    if (isType && (parent instanceof ASTFieldDeclaration || parent instanceof ASTFormalParameter || parent instanceof ASTResultType)) {
        addViolation(data, node, node.getImage());
    }
    return data;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTResultType(net.sourceforge.pmd.lang.java.ast.ASTResultType)

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