openrewrite / openrewrite/rewrite-static-analysis
BigDecimalDoubleConstructorRecipe should also convert BigDecimal.valueOf(doubleLiteral) -> new BigDecimal(stringLiteral)
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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