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 243363 - Hint "Initializer can be static" should be suppressed in anonymous classes
Summary: Hint "Initializer can be static" should be suppressed in anonymous classes
Status: RESOLVED DUPLICATE of bug 241344
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0
Hardware: All All
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-28 18:31 UTC by theshadow27
Modified: 2014-08-21 09:01 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description theshadow27 2014-03-28 18:31:06 UTC
The new "initializer can be static" hint in 8.0 incorrectly identifies flags instance initializers in anonymous classes, and the resulting fix breaks the code. 


For example,


        public static final java.util.Map<Character, String> LOOKUP 
                 = java.util.Collections.unmodifiableMap(
                         new java.util.HashMap<Character, String>(){
             {
                 put('A', "A is for Apple");
                 put('B', "B is for Bannana");
                 put('C', "C is for Coconut");
             }  
         });


the hint is displayed. When performing the "fix" by clicking on the hint in the gutter ("make initializer static"), it changes the code to 


         public static final java.util.Map<Character, String> LOOKUP 
                 = java.util.Collections.unmodifiableMap(
                         new java.util.HashMap<Character, String>(){
            static  {
                 put('A', "A is for Apple");
                 put('B', "B is for Bannana");
                 put('C', "C is for Coconut");
             }  
         });

which obviously won't compile since put(K, V) is an instance method on java.util.Map.

The easiest fix would be to suppress this hint in anonymous classes. 

Alternately, it could use the "[context] Refactor->Convert Anonymous to Member" logic to create a (potentially static) inner class with the initializer as part of the constructor. 



Product Version = NetBeans IDE 8.0 (Build 201403101706)
Operating System = Mac OS X version 10.8.5 running on x86_64
Java; VM; Vendor = 1.7.0_51
Runtime = Java HotSpot(TM) 64-Bit Server VM 24.51-b03
Comment 1 skiwi 2014-08-20 07:17:33 UTC
The issue is still there and it is being annoying, please fix this, I request this to be P2 as the fix itself seems trivial to me.
Comment 2 Svata Dedic 2014-08-21 09:01:24 UTC
Fix should be present in 8.0.1

*** This bug has been marked as a duplicate of bug 241344 ***