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 153677

Summary: Format code doesn't handle double brace initialization
Product: java Reporter: dfa <dfa>
Component: EditorAssignee: Dusan Balek <dbalek>
Status: NEW ---    
Severity: blocker CC: emanuel_
Priority: P3    
Version: 6.x   
Hardware: PC   
OS: Windows Vista   
URL: http://c2.com/cgi/wiki?DoubleBraceInitialization
Issue Type: ENHANCEMENT Exception Reporter:

Description dfa 2008-11-21 23:09:23 UTC
DoubleBraceInizialization is formatted in a sub-optimal way. For example, given:

new Foo() {{
  // some code 
}}

I obtain (with Format, alt-shift-f):

new Foo() {

            {
                // some code
            }
        }
Comment 1 Jan Lahoda 2008-11-25 10:05:56 UTC
BTW, the idiom seems quite wasteful to me...
Comment 2 xwinus 2014-10-10 20:08:18 UTC
maybe wasteful, but is there any way to force syntax formatter keep the double braces initialization in the same format as shown by @dfa?

new Foo() {{
    // some code here
}}

alt-shift-f breaks it into

new Foo() {
    {
        // some code here
    }
}

which is waste of lines and looks ugly
Comment 3 naquada 2015-01-09 15:38:08 UTC
Agreed. Handling double braces in a special way would be very helpful.  Although the idiom is often discouraged, I think it does have a place, particularly when using a tool like JMockit.  

You see this very often when using JMockit:

    new Expectations()
    {{
        myClass.getValue();
        returns(11);
    }};

However, it gets reformatted to this:

    new Expectations()
    {
      {
        myClass.getValue();
        returns(11);
      }
    };

You also see this idiom initializing Sets, etc.:

  Set<Integer> intSet = new HashSet<Integer>(){{ add(4); }};

Which gets reformatted to:

  Set<Integer> intSet = new HashSet<Integer>()
           {
             {
               add(4);
             }
  };

I think the first sample would be better, and something like this if the other preferences cause the initialization to wrap:

  Set<Integer> intSet = new HashSet<Integer>()
  {{
    add(4);
  }};



I think a reasonable solution would be an option for "keep braces together in double brace initialization idiom" -- which means when a class/variable initialization/override uses the initialization idiom, for formatting purposes treat it as a single brace (subject to all the newline stuff, etc. associated with single braces).