Make Intellij Your Second Brain
This month’s Java Magazin had an interesting article about Four common pitfalls of the BigDecimal class and how to avoid them.
Curious if I had made such a potential mistake in the past, I opened my java playground project.
A code search for BigDecimal.valueOf(
found many occurrences.
Having a closer look at the results, it turned out that some of them used a double argument.
So I was looking for a way to not make these potential mistakes again.
I had a look at IntelliJ’s inspections and as it turns out, there is an inspection for the ambiguous constructor call, but not for the factory method. I recently read about Structural Search and Replace in IntelliJ and figured that I’ve a use-case for this feature here.
Navigate to Preferences -> Editor -> Inspections and click on the + icon to add a new replace template.
The problematic code we’re looking for has the form of BigDecimal.valueOf(x)
where x is of type double.
In the form field Search Template we use java.math.BigDecimal.valueOf($parameter$)
and with the cursor places on parameter
, add a filter.
As type, specify double
.
As Replace Template I’ve chosen new java.math.BigDecimal("$parameter$")
, which isn’t perfect, but might work just well enough in some cases.
From now on, IntelliJ warns me like this: