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 - Format code doesn't handle double brace initialization
Summary: Format code doesn't handle double brace initialization
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Editor (show other bugs)
Version: 6.x
Hardware: PC Windows Vista
: P3 blocker with 2 votes (vote)
Assignee: Dusan Balek
URL: http://c2.com/cgi/wiki?DoubleBraceIni...
Keywords:
Depends on:
Blocks:
 
Reported: 2008-11-21 23:09 UTC by dfa
Modified: 2015-01-09 15:38 UTC (History)
1 user (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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).