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 253612

Summary: Provide hint for missing hashCode/equals of Map keys
Product: java Reporter: pieterpaul
Component: HintsAssignee: Svata Dedic <sdedic>
Status: NEW ---    
Severity: normal    
Priority: P3    
Version: 8.1   
Hardware: PC   
OS: Windows 8 x64   
Issue Type: ENHANCEMENT Exception Reporter:

Description pieterpaul 2015-07-21 11:00:03 UTC
Feature suggestion:

Using an object as a key in a (Hash)Map that has no hashCode/equals method can lead to bugs that are hard to find. What would happen is that if two objects are created that should be equal (such as some custom index or identifier type), they will not refer to the same value in the Map because Object.equals checks for object identity. Such bugs can be hard to find because simple unit tests (that reuse key objects) will not find them.

If Netbeans would give a hint that a class that derives its equals/hashCode methods from Object is used as a Map key, such bugs would be discovered much more easily. If this hint is shown at the declaration of the Map type it will not lead to many extra hints even if the user intends to use a class as key that does not define hashCode/equals.

Possible disadvantages/caveats:

1. Users intend to use such a class as key. I am not sure how common this scenario is, but I think it's already possible to suppress warnings for that declaration? In any case, for many inexperienced programmers it would be wise to advise them of the 'danger' of the construct anyway.

2. Users use a class as map key that itself does not implement hashCode/equals, but only actually use subtypes that do. This scenario seems unlikely to me.

I cannot think of other disadvantages to which this
Comment 1 Svata Dedic 2015-10-02 14:31:29 UTC
I can - performance :)
If implemented, the hint should respect @SuppressWarning on the type-parametrized Map declaration or the key type and offer a fix to suppress itself on these places (not just for a method). Annotating variable/field decl with the @SuppressWarning should indicate the programmer is aware of that.