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 203002 - "Introduce Method" refactoring generates invalid code
Summary: "Introduce Method" refactoring generates invalid code
Status: VERIFIED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 7.0.1
Hardware: PC Windows XP
: P2 normal (vote)
Assignee: Jan Lahoda
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-03 19:00 UTC by tomwheeler
Modified: 2011-10-26 14:33 UTC (History)
1 user (show)

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 tomwheeler 2011-10-03 19:00:43 UTC
I have found a case in which the "Introduce Method" refactoring generates invalid code.  It appears to have a problem when a single statement declares several variables, such as:

    double foo, bar, baz, bat, wuux;

To reproduce this, add the following code to a method within a Java class (I used the main method for a newly-generated class:

        double leftH, rightH, delta, deltaY;
        double tLeft, tRight, bLeft, bRight;
        
        tLeft = 3.4;
        tRight = 4.5;
        bLeft = 5.6;
        bRight = 6.7;
        
        leftH = Math.max(tLeft, bLeft);
        rightH = Math.max(tRight, bRight);
        delta = rightH - leftH;

Highlight all of this code and right-click, then select Refactor -> Introduce Method from the popup menu.  Type a name for the new method (I used "foo") and click OK on the dialog.  This generates the following new method:

    private static void foo(double rightH, double leftH) {
        double delta;
        double leftH, rightH, delta, deltaY;
        double tLeft,
        double tLeft, tRight,
        double tLeft, tRight, bLeft,
        double tLeft, tRight, bLeft, bRight;
        tLeft = 3.4;
        tRight = 4.5;
        bLeft = 5.6;
        bRight = 6.7;
        leftH = Math.max(tLeft, bLeft);
        rightH = Math.max(tRight, bRight);
        delta = rightH - leftH;
    }

As you can see, the tLeft variable is declared multiple times and all of these except for the last are invalid statements since they do not conclude with a semicolon.
Comment 1 tomwheeler 2011-10-04 19:52:11 UTC
I have confirmed that declaring each variable on its own line works around this bug in NetBeans.
Comment 2 Jan Becicka 2011-10-05 14:04:21 UTC
implemented in hints module.
Comment 3 Jan Lahoda 2011-10-24 10:42:25 UTC
http://hg.netbeans.org/jet-main/rev/77fa3b1a6c65
Comment 4 Quality Engineering 2011-10-25 14:47:57 UTC
Integrated into 'main-golden'
Changeset: http://hg.netbeans.org/main-golden/rev/77fa3b1a6c65
User: Jan Lahoda <jlahoda@netbeans.org>
Log: #203002: fixing handling of field groups inside the introduce method hint.
Comment 5 tomwheeler 2011-10-26 14:33:58 UTC
I verified this using the most recent nightly build:

   Product Version: NetBeans IDE Dev (Build 201110250600)
   Java: 1.6.0_25; Java HotSpot(TM) Client VM 20.0-b11
   System: Windows XP version 5.1 running on x86; Cp1252; en_US (nb)

Thanks for the quick fix!