This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 236244

Summary: Hint for transformation to BigDecimal constants like BigDecimal.ZERO/BigDecimal.ONE/BigDecimal.TEN
Product: java Reporter: markiewb
Component: HintsAssignee: Svata Dedic <sdedic>
Status: NEW ---    
Severity: normal    
Priority: P3    
Version: 7.4   
Hardware: PC   
OS: Windows 7   
Issue Type: ENHANCEMENT Exception Reporter:
Attachments: Proposed patch incl. tests for java.hints
Patch v2
Updated patch

Description markiewb 2013-09-22 15:07:02 UTC
Provide hint which transforms
```
new BigDecimal("0.0")
new BigDecimal("0")
new BigDecimal("0.00")
new BigDecimal(0)
new BigDecimal(0L)
new BigDecimal(0.0f)
new BigDecimal(0.0d)
BigDecimal.valueOf(0)
BigDecimal.valueOf(0L)
BigDecimal.valueOf(0.0L)
```
to
```
BigDecimal.ZERO
```

Same for
```BigDecimal.TEN, BigDecimal.ONE```
Comment 1 markiewb 2013-11-16 11:23:35 UTC
FYI: Fixed in 1.1.0 of the additional hints plugin https://github.com/markiewb/nb-additional-hints/issues/11
Comment 2 Svata Dedic 2013-11-16 11:35:52 UTC
great ! Shouldn't the enhancement closed as resolved/fixed ?
Comment 3 markiewb 2013-11-16 13:24:27 UTC
(In reply to Svata Dedic from comment #2)
> great ! Shouldn't the enhancement closed as resolved/fixed ?
@Svata:
You may close it as wontfix (because this feature is already available as plugin)
OR
I could provide a patch so that the hint can get into the official NB sources

What do you like?
Comment 4 Svata Dedic 2013-11-26 07:02:59 UTC
If you're willing to provide a patch, I'll be pleased to integrate it to 8.0
Comment 5 markiewb 2013-11-28 19:46:16 UTC
Created attachment 142697 [details]
Proposed patch incl. tests for java.hints

(In reply to Svata Dedic from comment #4)
> If you're willing to provide a patch, I'll be pleased to integrate it to 8.0


Hi Svata, 

here's the patch. Please review.
If you like to include some more hints from https://github.com/markiewb/nb-additional-hints please tell me.
You even can contribute some experimental hints to the plugin itself.

Best regards, Benno
Comment 6 Svata Dedic 2013-11-29 15:14:40 UTC
Ah thanks; a couple of ideas:

* combine all BigDecimal constant hints into one. Reason: I can't think of a case I would like 0 to be replaced and not 10 (or any other JDK-defined constants)

* the hint should check for Source >= 5, BigDec constants are @since 1.5

* ctors with BigInteger.ZERO|ONE|TEN could be also covered; not sure about the ctor with scale ... possibly for scale == 0 ?

* the variants of 0s 1s etc except the most basic form could be replaced by $v, where ArithmeticUtilities evaluate $v expression to constant.
Comment 7 markiewb 2013-11-30 19:44:05 UTC
Created attachment 142749 [details]
Patch v2

(In reply to Svata Dedic from comment #6)
> Ah thanks; a couple of ideas:
> 
> * combine all BigDecimal constant hints into one. Reason: I can't think of a
> case I would like 0 to be replaced and not 10 (or any other JDK-defined
> constants)
> 
> * the hint should check for Source >= 5, BigDec constants are @since 1.5
> 
> * the variants of 0s 1s etc except the most basic form could be replaced by
> $v, where ArithmeticUtilities evaluate $v expression to constant.

Fixed. Please review the new patch. 

> * ctors with BigInteger.ZERO|ONE|TEN could be also covered; not sure about
> the ctor with scale ... possibly for scale == 0 ?

Mmh. I do not know how to integrate this in the same hint. Suggestions? Or worth a new RFE?
Comment 8 Svata Dedic 2013-12-03 09:10:30 UTC
Created attachment 142802 [details]
Updated patch

Sorry, the changes to the original patch (after going through some rounds of optimization) were quite big. Here's what I think works better:

* String goes through constant evaluation (without resolving field refs, as it may decouple the BigDec value from the original constant)
* Strings are converted to their value to avoid .00
* pritimives are converted to double, it's faster than going through BigDecimal and .compareTo, since we have constants for integer values only
* ctors that use BigInteger constants with 0 scale are converted to BigDecimal constants. I didn't mess with combination like BigInteger 10 + scale 1 (= 1.0 = BigDecimal.ONE), sorry :) 

[I've noticed an unrelated issue in ArithmeticUtilities, so only values that resolve constants are cached]
Comment 9 markiewb 2013-12-10 22:50:57 UTC
(In reply to Svata Dedic from comment #8)
> Created attachment 142802 [details]
> Updated patch
> 
> Sorry, the changes to the original patch (after going through some rounds of
> optimization) were quite big. Here's what I think works better:
> 
> * String goes through constant evaluation (without resolving field refs, as
> it may decouple the BigDec value from the original constant)
> * Strings are converted to their value to avoid .00
> * pritimives are converted to double, it's faster than going through
> BigDecimal and .compareTo, since we have constants for integer values only
> * ctors that use BigInteger constants with 0 scale are converted to
> BigDecimal constants. I didn't mess with combination like BigInteger 10 +
> scale 1 (= 1.0 = BigDecimal.ONE), sorry :) 
> 
> [I've noticed an unrelated issue in ArithmeticUtilities, so only values that
> resolve constants are cached]

Oh. You covered some more cases I wasn't aware of. Nice.