Search in sources :

Example 11 with ASTLiteral

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

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

the class AbstractInefficientZeroCheck method isLiteral.

private boolean isLiteral(Node equality, int child) {
    Node target = equality.jjtGetChild(child);
    target = getFirstChildOrThis(target);
    target = getFirstChildOrThis(target);
    return target instanceof ASTLiteral;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 13 with ASTLiteral

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

the class UselessStringValueOfRule method isPrimitive.

private static boolean isPrimitive(Node parent) {
    boolean result = false;
    if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
        Node child = parent.jjtGetChild(0);
        if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
            Node gc = child.jjtGetChild(0);
            if (gc instanceof ASTName) {
                ASTName name = (ASTName) gc;
                NameDeclaration nd = name.getNameDeclaration();
                if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) {
                    result = true;
                }
            } else if (gc instanceof ASTLiteral) {
                result = !((ASTLiteral) gc).isStringLiteral();
            }
        }
    }
    return result;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 14 with ASTLiteral

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

the class InefficientStringBufferingRule method visit.

@Override
public Object visit(ASTAdditiveExpression node, Object data) {
    ASTBlockStatement bs = node.getFirstParentOfType(ASTBlockStatement.class);
    if (bs == null) {
        return data;
    }
    int immediateLiterals = 0;
    int immediateStringLiterals = 0;
    List<ASTLiteral> nodes = node.findDescendantsOfType(ASTLiteral.class);
    for (ASTLiteral literal : nodes) {
        if (literal.getNthParent(3) instanceof ASTAdditiveExpression) {
            immediateLiterals++;
            if (literal.isStringLiteral()) {
                immediateStringLiterals++;
            }
        }
        if (literal.isIntLiteral() || literal.isFloatLiteral() || literal.isDoubleLiteral() || literal.isLongLiteral()) {
            return data;
        }
    }
    if (immediateLiterals > 1) {
        return data;
    }
    // if literal + public static final, return
    List<ASTName> nameNodes = node.findDescendantsOfType(ASTName.class);
    for (ASTName name : nameNodes) {
        if (name.getNameDeclaration() != null && name.getNameDeclaration() instanceof VariableNameDeclaration) {
            VariableNameDeclaration vnd = (VariableNameDeclaration) name.getNameDeclaration();
            AccessNode accessNodeParent = vnd.getAccessNodeParent();
            if (accessNodeParent.isFinal() && accessNodeParent.isStatic()) {
                return data;
            }
        }
    }
    // if literal primitive type and not strings variables, then return
    boolean stringFound = false;
    for (ASTName name : nameNodes) {
        if (!isPrimitiveType(name) && isStringType(name)) {
            stringFound = true;
            break;
        }
    }
    if (!stringFound && immediateStringLiterals == 0) {
        return data;
    }
    if (bs.isAllocation()) {
        for (Iterator<ASTName> iterator = nameNodes.iterator(); iterator.hasNext(); ) {
            ASTName name = iterator.next();
            if (!name.getImage().endsWith("length")) {
                break;
            } else if (!iterator.hasNext()) {
                // All names end with length
                return data;
            }
        }
        if (isAllocatedStringBuffer(node)) {
            addViolation(data, node);
        }
    } else if (isInStringBufferOperation(node, 6, "append")) {
        addViolation(data, node);
    }
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 15 with ASTLiteral

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

the class InsufficientStringBufferDeclarationRule method getConstructorLength.

private int getConstructorLength(Node node, int constructorLength) {
    int iConstructorLength = constructorLength;
    Node block = node.getFirstParentOfType(ASTBlockStatement.class);
    if (block == null) {
        block = node.getFirstParentOfType(ASTFieldDeclaration.class);
    }
    if (block == null) {
        block = node.getFirstParentOfType(ASTFormalParameter.class);
        if (block != null) {
            iConstructorLength = -1;
        } else {
            return DEFAULT_BUFFER_SIZE;
        }
    }
    // if there is any addition/subtraction going on then just use the
    // default.
    ASTAdditiveExpression exp = block.getFirstDescendantOfType(ASTAdditiveExpression.class);
    if (exp != null) {
        return DEFAULT_BUFFER_SIZE;
    }
    ASTMultiplicativeExpression mult = block.getFirstDescendantOfType(ASTMultiplicativeExpression.class);
    if (mult != null) {
        return DEFAULT_BUFFER_SIZE;
    }
    List<ASTLiteral> literals;
    ASTAllocationExpression constructorCall = block.getFirstDescendantOfType(ASTAllocationExpression.class);
    if (constructorCall != null) {
        // if this is a constructor call, only consider the literals within
        // it.
        literals = constructorCall.findDescendantsOfType(ASTLiteral.class);
    } else {
        // otherwise it might be a setLength call...
        literals = block.findDescendantsOfType(ASTLiteral.class);
    }
    if (literals.isEmpty()) {
        List<ASTName> name = block.findDescendantsOfType(ASTName.class);
        if (!name.isEmpty()) {
            iConstructorLength = -1;
        }
    } else if (literals.size() == 1) {
        ASTLiteral literal = literals.get(0);
        String str = literal.getImage();
        if (str == null) {
            iConstructorLength = 0;
        } else if (isStringOrCharLiteral(literal)) {
            // since it's not taken into account
            // anywhere. only count the extra 16
            // characters
            // don't add the constructor's length
            iConstructorLength = 14 + str.length();
        } else if (literal.isIntLiteral()) {
            iConstructorLength = literal.getValueAsInt();
        }
    } else {
        iConstructorLength = -1;
    }
    if (iConstructorLength == 0) {
        if (constructorLength == -1) {
            iConstructorLength = DEFAULT_BUFFER_SIZE;
        } else {
            iConstructorLength = constructorLength;
        }
    }
    return iConstructorLength;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTMultiplicativeExpression(net.sourceforge.pmd.lang.java.ast.ASTMultiplicativeExpression) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Aggregations

ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)18 Node (net.sourceforge.pmd.lang.ast.Node)13 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)7 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)4 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)4 ArrayList (java.util.ArrayList)3 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)3 ASTBooleanLiteral (net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral)3 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)3 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)3 List (java.util.List)2 ASTCastExpression (net.sourceforge.pmd.lang.java.ast.ASTCastExpression)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)2 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)2 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 BigInteger (java.math.BigInteger)1