Search in sources :

Example 6 with ASTClassOrInterfaceType

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

the class ExceptionAsFlowControlRule method visit.

@Override
public Object visit(ASTThrowStatement node, Object data) {
    ASTTryStatement parent = node.getFirstParentOfType(ASTTryStatement.class);
    if (parent == null) {
        return data;
    }
    for (parent = parent.getFirstParentOfType(ASTTryStatement.class); parent != null; parent = parent.getFirstParentOfType(ASTTryStatement.class)) {
        List<ASTCatchStatement> list = parent.findDescendantsOfType(ASTCatchStatement.class);
        for (ASTCatchStatement catchStmt : list) {
            ASTFormalParameter fp = (ASTFormalParameter) catchStmt.jjtGetChild(0);
            ASTType type = fp.getFirstDescendantOfType(ASTType.class);
            ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
            if (node.getFirstClassOrInterfaceTypeImage() != null && node.getFirstClassOrInterfaceTypeImage().equals(name.getImage())) {
                addViolation(data, name);
            }
        }
    }
    return data;
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTCatchStatement(net.sourceforge.pmd.lang.java.ast.ASTCatchStatement) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 7 with ASTClassOrInterfaceType

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

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

the class ClassScope method determineArgumentTypes.

/**
 * Provide a list of types of the arguments of the given method call. The
 * types are simple type images. If the argument type cannot be determined
 * (e.g. because it is itself the result of a method call), the parameter
 * type is used - so it is assumed, it is of the correct type. This might
 * cause confusion when methods are overloaded.
 *
 * @param occurrence
 *            the method call
 * @param parameterTypes
 *            the parameter types of the called method
 * @return the list of argument types
 */
private List<TypedNameDeclaration> determineArgumentTypes(JavaNameOccurrence occurrence, List<TypedNameDeclaration> parameterTypes) {
    ASTArgumentList arguments = null;
    Node nextSibling;
    if (occurrence.getLocation() instanceof ASTPrimarySuffix) {
        nextSibling = getNextSibling(occurrence.getLocation());
    } else {
        nextSibling = getNextSibling(occurrence.getLocation().jjtGetParent());
    }
    if (nextSibling != null) {
        arguments = nextSibling.getFirstDescendantOfType(ASTArgumentList.class);
    }
    if (arguments == null) {
        return Collections.emptyList();
    }
    List<TypedNameDeclaration> argumentTypes = new ArrayList<>(arguments.jjtGetNumChildren());
    Map<String, Node> qualifiedTypeNames = getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames();
    for (int i = 0; i < arguments.jjtGetNumChildren(); i++) {
        Node argument = arguments.jjtGetChild(i);
        Node child = null;
        boolean isMethodCall = false;
        if (argument.jjtGetNumChildren() > 0 && argument.jjtGetChild(0).jjtGetNumChildren() > 0 && argument.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0) {
            child = argument.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
            isMethodCall = argument.jjtGetChild(0).jjtGetNumChildren() > 1;
        }
        TypedNameDeclaration type = null;
        if (child instanceof ASTName && !isMethodCall) {
            ASTName name = (ASTName) child;
            Scope s = name.getScope();
            final JavaNameOccurrence nameOccurrence = new JavaNameOccurrence(name, name.getImage());
            while (s != null) {
                if (s.contains(nameOccurrence)) {
                    break;
                }
                s = s.getParent();
            }
            if (s != null) {
                Map<VariableNameDeclaration, List<NameOccurrence>> vars = s.getDeclarations(VariableNameDeclaration.class);
                for (VariableNameDeclaration d : vars.keySet()) {
                    // might be unknown
                    if (d.getImage().equals(name.getImage()) && d.getTypeImage() != null) {
                        String typeName = d.getTypeImage();
                        typeName = qualifyTypeName(typeName);
                        Node declaringNode = qualifiedTypeNames.get(typeName);
                        type = new SimpleTypedNameDeclaration(typeName, this.getEnclosingScope(SourceFileScope.class).resolveType(typeName), determineSuper(declaringNode));
                        break;
                    }
                }
            }
        } else if (child instanceof ASTLiteral) {
            ASTLiteral literal = (ASTLiteral) child;
            if (literal.isCharLiteral()) {
                type = new SimpleTypedNameDeclaration("char", literal.getType());
            } else if (literal.isStringLiteral()) {
                type = new SimpleTypedNameDeclaration("String", literal.getType());
            } else if (literal.isFloatLiteral()) {
                type = new SimpleTypedNameDeclaration("float", literal.getType());
            } else if (literal.isDoubleLiteral()) {
                type = new SimpleTypedNameDeclaration("double", literal.getType());
            } else if (literal.isIntLiteral()) {
                type = new SimpleTypedNameDeclaration("int", literal.getType());
            } else if (literal.isLongLiteral()) {
                type = new SimpleTypedNameDeclaration("long", literal.getType());
            } else if (literal.jjtGetNumChildren() == 1 && literal.jjtGetChild(0) instanceof ASTBooleanLiteral) {
                type = new SimpleTypedNameDeclaration("boolean", Boolean.TYPE);
            }
        } else if (child instanceof ASTAllocationExpression && child.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
            ASTClassOrInterfaceType classInterface = (ASTClassOrInterfaceType) child.jjtGetChild(0);
            type = convertToSimpleType(classInterface);
        }
        if (type == null && !parameterTypes.isEmpty()) {
            // arguments than parameterTypes
            if (parameterTypes.size() > i) {
                type = parameterTypes.get(i);
            } else {
                // last parameter is the vararg type
                type = parameterTypes.get(parameterTypes.size() - 1);
            }
        }
        if (type != null && type.getType() == null) {
            Class<?> typeBound = resolveGenericType(argument, type.getTypeImage());
            if (typeBound != null) {
                type = new SimpleTypedNameDeclaration(type.getTypeImage(), typeBound);
            }
        }
        argumentTypes.add(type);
    }
    return argumentTypes;
}
Also used : ASTBooleanLiteral(net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral) Node(net.sourceforge.pmd.lang.ast.Node) ArrayList(java.util.ArrayList) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) ArrayList(java.util.ArrayList) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 9 with ASTClassOrInterfaceType

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

the class UseCollectionIsEmptyRule method visit.

@Override
public Object visit(ASTPrimarySuffix node, Object data) {
    if (node.getImage() != null && node.getImage().endsWith("size")) {
        ASTClassOrInterfaceType type = getTypeOfPrimaryPrefix(node);
        if (type == null) {
            type = getTypeOfMethodCall(node);
        }
        if (type != null && CollectionUtil.isCollectionType(type.getType(), true)) {
            Node expr = node.jjtGetParent().jjtGetParent();
            checkNodeAndReport(data, node, expr);
        }
    }
    return data;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 10 with ASTClassOrInterfaceType

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

the class UseCollectionIsEmptyRule method getTypeOfMethodCall.

private ASTClassOrInterfaceType getTypeOfMethodCall(ASTPrimarySuffix node) {
    ASTClassOrInterfaceType type = null;
    ASTName methodName = node.jjtGetParent().getFirstChildOfType(ASTPrimaryPrefix.class).getFirstChildOfType(ASTName.class);
    if (methodName != null) {
        ClassScope classScope = node.getScope().getEnclosingScope(ClassScope.class);
        Map<MethodNameDeclaration, List<NameOccurrence>> methods = classScope.getMethodDeclarations();
        for (Map.Entry<MethodNameDeclaration, List<NameOccurrence>> e : methods.entrySet()) {
            if (e.getKey().getName().equals(methodName.getImage())) {
                type = e.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class).getFirstChildOfType(ASTResultType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class);
                break;
            }
        }
    }
    return type;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) MethodNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) List(java.util.List) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) HashMap(java.util.HashMap) Map(java.util.Map) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Aggregations

ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)25 Node (net.sourceforge.pmd.lang.ast.Node)13 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)7 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)7 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)5 ASTImplementsList (net.sourceforge.pmd.lang.java.ast.ASTImplementsList)5 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)5 ArrayList (java.util.ArrayList)4 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)4 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)4 List (java.util.List)3 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)3 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)3 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)3 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)3 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)3 Map (java.util.Map)2 RuleContext (net.sourceforge.pmd.RuleContext)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2