Skip to content

[java] AvoidDecimalLiteralsInBigDecimalConstructor can not detect the case new BigDecimal(Expression) #2532

@wuchiuwong

Description

@wuchiuwong

Affects PMD Version:
6.22.0

Rule:
AvoidDecimalLiteralsInBigDecimalConstructor

Description:
AvoidDecimalLiteralsInBigDecimalConstructor can not detect the case new BigDecimal(Expression)

//AllocationExpression
[ClassOrInterfaceType[@Image="BigDecimal"]]
[Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix
    [
        Literal[(not(ends-with(@Image,'"'))) and contains(@Image,".")]
        or
        Name[ancestor::Block/BlockStatement/LocalVariableDeclaration
                [Type[PrimitiveType[@Image='double' or @Image='float']
                      or ReferenceType/ClassOrInterfaceType[@Image='Double' or @Image='Float']]]
                /VariableDeclarator/VariableDeclaratorId/@Image = @Image
            ]
        or
        Name[ancestor::MethodDeclaration/MethodDeclarator/FormalParameters/FormalParameter
                [Type[PrimitiveType[@Image='double' or @Image='float']
                      or ReferenceType/ClassOrInterfaceType[@Image='Double' or @Image='Float']]]
                /VariableDeclaratorId/@Image = @Image
            ]
    ]
]

This rule doesn't consider using Expression(for example: foo * bar) to create BigDecimal

Code Sample demonstrating the issue:

new BigDecimal(remaining * 60)

Expected outcome:
false-negative

Running PMD through:
CLI

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions