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 113045 - Handling of constructor arguments with Convert Anonymous to Inner
Summary: Handling of constructor arguments with Convert Anonymous to Inner
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 6.x
Hardware: PC Windows XP
: P3 blocker (vote)
Assignee: Svata Dedic
Depends on:
Reported: 2007-08-16 22:19 UTC by matthies
Modified: 2013-09-02 14:21 UTC (History)
0 users

See Also:
Exception Reporter:


Note You need to log in before you can comment on or make changes to this bug.
Description matthies 2007-08-16 22:19:49 UTC
Consider the application of Convert Anonymous to Inner to the two instantiations of Foo in the following code:

    public class Test
        class Foo { Foo(int n) { } }

        { new Foo(Math.abs(-5)) { }; }

        { int n = -2; new Foo(Math.abs(n) + 3) { }; }

I suggest that converting the first anonymous class should result in:

    { new FooImpl(); }

    private class FooImpl extends Test.Foo
        private FooImpl()

That is, constructor argument expressions which do not depend on the local or inner-class context should be moved into 
the generated constructor.

In the second instantiation of Foo above, the constructor argument expression does depend on a local context ("n" is a 
local variable), so either keep the expression as-is:

    { int n = -2; new FooImpl(Math.abs(n) + 3) { }; }

    private class FooImpl extends Test.Foo
        private FooImpl(int n)

Or alternatively, only retain dependant sub-expressions in the instantiation expression:

    { int n = -2; new FooImpl(n) { }; }

    private class FooImpl extends Test.Foo
        private FooImpl(int n)
            super(Math.abs(n) + 3);

The main point is, that for the typical case of a constant expression or literal argument (e.g. "new Foo(5)"), the 
expression should be placed in the generated constructor rather than introducting redundant constructor parameters in 
the generated class. In other words, translate the original argument list directly into the argument list of the super 
constructor call (which must be generated anyway). The other cases are just generalizations of this.