Search in sources :

Example 1 with ASTBooleanLiteral

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

the class RedundantFieldInitializerRule method visit.

public Object visit(ASTFieldDeclaration fieldDeclaration, Object data) {
    // Finals can only be initialized once.
    if (fieldDeclaration.isFinal()) {
        return data;
    }
    // VariableDeclarator/VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix/Literal
    for (ASTVariableDeclarator variableDeclarator : fieldDeclaration.findChildrenOfType(ASTVariableDeclarator.class)) {
        if (variableDeclarator.jjtGetNumChildren() > 1) {
            final Node variableInitializer = variableDeclarator.jjtGetChild(1);
            if (variableInitializer.jjtGetChild(0) instanceof ASTExpression) {
                final Node expression = variableInitializer.jjtGetChild(0);
                final Node primaryExpression;
                if (expression.jjtGetNumChildren() == 1) {
                    if (expression.jjtGetChild(0) instanceof ASTPrimaryExpression) {
                        primaryExpression = expression.jjtGetChild(0);
                    } else if (expression.jjtGetChild(0) instanceof ASTCastExpression && expression.jjtGetChild(0).jjtGetChild(1) instanceof ASTPrimaryExpression) {
                        primaryExpression = expression.jjtGetChild(0).jjtGetChild(1);
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
                final Node primaryPrefix = primaryExpression.jjtGetChild(0);
                if (primaryPrefix.jjtGetNumChildren() == 1 && primaryPrefix.jjtGetChild(0) instanceof ASTLiteral) {
                    final ASTLiteral literal = (ASTLiteral) primaryPrefix.jjtGetChild(0);
                    if (isRef(fieldDeclaration, variableDeclarator)) {
                        // Reference type
                        if (literal.jjtGetNumChildren() == 1 && literal.jjtGetChild(0) instanceof ASTNullLiteral) {
                            addViolation(data, variableDeclarator);
                        }
                    } else {
                        // Primitive type
                        if (literal.jjtGetNumChildren() == 1 && literal.jjtGetChild(0) instanceof ASTBooleanLiteral) {
                            // boolean type
                            ASTBooleanLiteral booleanLiteral = (ASTBooleanLiteral) literal.jjtGetChild(0);
                            if (!booleanLiteral.isTrue()) {
                                addViolation(data, variableDeclarator);
                            }
                        } else if (literal.jjtGetNumChildren() == 0) {
                            // numeric type
                            // Note: Not catching NumberFormatException, as
                            // it shouldn't be happening on valid source
                            // code.
                            Number value = -1;
                            if (literal.isIntLiteral()) {
                                value = literal.getValueAsInt();
                            } else if (literal.isLongLiteral()) {
                                value = literal.getValueAsLong();
                            } else if (literal.isFloatLiteral()) {
                                value = literal.getValueAsFloat();
                            } else if (literal.isDoubleLiteral()) {
                                value = literal.getValueAsDouble();
                            } else if (literal.isCharLiteral()) {
                                value = (int) literal.getImage().charAt(1);
                            }
                            if (value.doubleValue() == 0) {
                                addViolation(data, variableDeclarator);
                            }
                        }
                    }
                }
            }
        }
    }
    return data;
}
Also used : ASTCastExpression(net.sourceforge.pmd.lang.java.ast.ASTCastExpression) ASTBooleanLiteral(net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral) ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTNullLiteral(net.sourceforge.pmd.lang.java.ast.ASTNullLiteral) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 2 with ASTBooleanLiteral

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

the class ConstructorCallsOverridableMethodRule method getArgumentTypes.

private static List<String> getArgumentTypes(ASTArguments args) {
    List<String> argumentTypes = new ArrayList<>();
    ASTArgumentList argumentList = args.getFirstChildOfType(ASTArgumentList.class);
    if (argumentList != null) {
        for (int a = 0; a < argumentList.jjtGetNumChildren(); a++) {
            Node expression = argumentList.jjtGetChild(a);
            ASTPrimaryPrefix arg = expression.getFirstDescendantOfType(ASTPrimaryPrefix.class);
            String type = "<unknown>";
            if (arg != null && arg.jjtGetNumChildren() > 0) {
                if (arg.jjtGetChild(0) instanceof ASTLiteral) {
                    ASTLiteral lit = (ASTLiteral) arg.jjtGetChild(0);
                    if (lit.isCharLiteral()) {
                        type = "char";
                    } else if (lit.isFloatLiteral()) {
                        type = "float";
                    } else if (lit.isIntLiteral()) {
                        type = "int";
                    } else if (lit.isStringLiteral()) {
                        type = "String";
                    } else if (lit.jjtGetNumChildren() > 0 && lit.jjtGetChild(0) instanceof ASTBooleanLiteral) {
                        type = "boolean";
                    } else if (lit.isDoubleLiteral()) {
                        type = "double";
                    } else if (lit.isLongLiteral()) {
                        type = "long";
                    }
                } else if (arg.jjtGetChild(0) instanceof ASTName) {
                    // ASTName n = (ASTName)arg.jjtGetChild(0);
                    type = "ref";
                }
            }
            argumentTypes.add(type);
        }
    }
    return argumentTypes;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTBooleanLiteral(net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ArrayList(java.util.ArrayList) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 3 with ASTBooleanLiteral

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

Aggregations

Node (net.sourceforge.pmd.lang.ast.Node)3 ASTBooleanLiteral (net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral)3 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)3 ArrayList (java.util.ArrayList)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)2 List (java.util.List)1 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)1 ASTCastExpression (net.sourceforge.pmd.lang.java.ast.ASTCastExpression)1 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)1 ASTImplementsList (net.sourceforge.pmd.lang.java.ast.ASTImplementsList)1 ASTNullLiteral (net.sourceforge.pmd.lang.java.ast.ASTNullLiteral)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 ASTVariableDeclarator (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator)1 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)1 Scope (net.sourceforge.pmd.lang.symboltable.Scope)1