Search in sources :

Example 6 with ASTPrimarySuffix

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

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

the class NameFinder method checkForNameChild.

private void checkForNameChild(JavaNode node) {
    if (node.getImage() != null) {
        add(new JavaNameOccurrence(node, node.getImage()));
    }
    if (node.jjtGetNumChildren() > 0 && node.jjtGetChild(0) instanceof ASTName) {
        ASTName grandchild = (ASTName) node.jjtGetChild(0);
        for (StringTokenizer st = new StringTokenizer(grandchild.getImage(), "."); st.hasMoreTokens(); ) {
            add(new JavaNameOccurrence(grandchild, st.nextToken()));
        }
    }
    if (node.jjtGetNumChildren() > 1 && node.jjtGetChild(1) instanceof ASTMethodReference) {
        ASTMethodReference methodRef = (ASTMethodReference) node.jjtGetChild(1);
        add(new JavaNameOccurrence(methodRef, methodRef.getImage()));
    }
    if (node instanceof ASTPrimarySuffix) {
        ASTPrimarySuffix suffix = (ASTPrimarySuffix) node;
        if (suffix.isArguments()) {
            JavaNameOccurrence occurrence = names.get(names.size() - 1);
            occurrence.setIsMethodOrConstructorInvocation();
            ASTArguments args = (ASTArguments) ((ASTPrimarySuffix) node).jjtGetChild(0);
            occurrence.setArgumentCount(args.getArgumentCount());
        } else if (suffix.jjtGetNumChildren() == 1 && suffix.jjtGetChild(0) instanceof ASTMemberSelector) {
            ASTMemberSelector member = (ASTMemberSelector) suffix.jjtGetChild(0);
            if (member.jjtGetNumChildren() == 1 && member.jjtGetChild(0) instanceof ASTMethodReference) {
                ASTMethodReference methodRef = (ASTMethodReference) member.jjtGetChild(0);
                add(new JavaNameOccurrence(methodRef, methodRef.getImage()));
            } else {
                add(new JavaNameOccurrence(member, member.getImage()));
            }
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) ASTMethodReference(net.sourceforge.pmd.lang.java.ast.ASTMethodReference) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTMemberSelector(net.sourceforge.pmd.lang.java.ast.ASTMemberSelector)

Example 8 with ASTPrimarySuffix

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

the class ForLoopCanBeForeachRule method occurenceIsListGet.

/**
 * @return true if this occurence is as an argument to List.get on the correct list
 */
private boolean occurenceIsListGet(NameOccurrence occ, String listName) {
    if (occ.getLocation() instanceof ASTName) {
        ASTPrimarySuffix suffix = occ.getLocation().getFirstParentOfType(ASTPrimarySuffix.class);
        if (suffix == null) {
            return false;
        }
        Node prefix = suffix.jjtGetParent().jjtGetChild(0);
        if (!(prefix instanceof ASTPrimaryPrefix) && prefix.jjtGetNumChildren() != 1 && !(prefix.jjtGetChild(0) instanceof ASTName)) {
            return false;
        }
        String callImage = prefix.jjtGetChild(0).getImage();
        return (listName + ".get").equals(callImage);
    }
    return false;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ScopedNode(net.sourceforge.pmd.lang.symboltable.ScopedNode) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 9 with ASTPrimarySuffix

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

the class GuardLogStatementRule method visit.

@Override
public Object visit(ASTPrimaryExpression node, Object data) {
    if (node.jjtGetNumChildren() >= 2 && node.jjtGetChild(0) instanceof ASTPrimaryPrefix) {
        ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) node.jjtGetChild(0);
        String methodCall = getMethodCallName(prefix);
        String logLevel = getLogLevelName(node, methodCall);
        if (guardStmtByLogLevel.containsKey(methodCall) && node.jjtGetChild(1) instanceof ASTPrimarySuffix && node.jjtGetChild(1).hasDescendantOfType(ASTAdditiveExpression.class)) {
            if (!hasGuard(node, methodCall, logLevel)) {
                super.addViolation(data, node);
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 10 with ASTPrimarySuffix

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

the class GuardLogStatementRule method getLogLevelName.

/**
 * Determines the log level, that is used. It is either the called method name
 * itself or - if the method has a first argument a primary prefix - the first argument.
 *
 * @param node the method call
 * @param methodCallName the called method name previously determined
 * @return the log level
 */
private String getLogLevelName(Node node, String methodCallName) {
    String logLevel = methodCallName;
    ASTPrimarySuffix suffix = node.getFirstDescendantOfType(ASTPrimarySuffix.class);
    if (suffix != null) {
        ASTArgumentList argumentList = suffix.getFirstDescendantOfType(ASTArgumentList.class);
        if (argumentList != null && argumentList.hasDescendantOfType(ASTName.class)) {
            ASTName name = argumentList.getFirstDescendantOfType(ASTName.class);
            String lastPart = getLastPartOfName(name.getImage());
            lastPart = lastPart.toLowerCase(Locale.ROOT);
            if (!lastPart.isEmpty()) {
                logLevel = lastPart;
            }
        }
    }
    return logLevel;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Aggregations

ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)24 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)14 Node (net.sourceforge.pmd.lang.ast.Node)12 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)11 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)10 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)4 ArrayList (java.util.ArrayList)3 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3 List (java.util.List)2 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)2 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)2 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)2 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)2 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)2 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)2 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)2