openrewrite / openrewrite/rewrite-static-analysis

BigDecimalDoubleConstructorRecipe should also convert BigDecimal.valueOf(doubleLiteral) -> new BigDecimal(stringLiteral)

Open
#255 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
62
Forks
112
Avg merge
1d 19h
Merged PRs (30d)
40

Description

What problem are you trying to solve?

The String constructor of BigDecimal is much nicer than BigDecimal.valueOf(doubleLiteral) because

  • two chars shorter (even shorter if the double literal has trailing zeroes)
  • more obvious that a new object is created
  • performance gain because valueOf is converted to String constructor anyway during compilation (unlikely to be noticable).

Describe the solution you'd like

Per API BigDecimal.valueOf(double)

Translates a double into a BigDecimal, using the double's canonical string representation provided by the Double.toString(double) method.

In addition to transforming new Bigdecimal(existingDouble) to valueOf, BigDecimalDoubleConstructorRecipe should convert BigDecimal.valueOf(doubleLiteral) to new BigDecimal(doubleString) where doubleString = Double.toString(doubleLiteral).toString().
E.g. BigDecimal.valueOf(1.00) -> new BigDecimal("1.0")

Have you considered any alternatives or workarounds?

Additional context

Are you interested in contributing this feature to OpenRewrite?

Iff the idea is approved.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate BigDecimalDoubleConstructorRecipe in the repository and inspect its existing handling of BigDecimal constructors, along with the tests covering that recipe. Add coverage for BigDecimal.valueOf(doubleLiteral), including the stated 1.00 example, and confirm the expected new BigDecimal string-constructor output with the recipe's test suite.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.