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 152701

Summary: "Introduce method" puts new method in an arbitrary location
Product: java Reporter: jnicol <jnicol>
Component: HintsAssignee: Svata Dedic <sdedic>
Status: REOPENED ---    
Severity: blocker    
Priority: P2    
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:
Attachments: Patch that fixes the sorting issue for introduced methods

Description jnicol 2008-11-07 16:03:26 UTC
When using Refactor->Introduce method, the new method will appear at somewhat random locations in the file (although it
appears to be based on the method name, see below for example), and sometimes very far away from the original method. 
Expected behavior should be: put the method adjacent to the previous method, ideally directly below it.

Small example:
package testrefactor;
public class Main {
    public static void main(String[] args) { blah(); }
    private static void blah() { int i = 5; }
}

If you highlight "int i=5;", and introduce a method called "a", then the method "a" appears ABOVE blah().
If you highlight "int i=5;", and introduce a method called "c", then the method "c" appears BELOW blah().
Comment 1 Petr Dvorak 2008-11-11 10:08:32 UTC
I don't think this is a defect, but this behavior could certainly enhance user's experience, IMO -> P2 Enhancement.
Comment 2 jnicol 2009-03-25 20:44:53 UTC
Created attachment 78859 [details]
Patch that fixes the sorting issue for introduced methods
Comment 3 jnicol 2009-03-25 20:49:42 UTC
The attached patch should fix the issue.  The problem was caused because introduced methods were being sorted in the
same way as introduced classes.  (Although classes have a defined order per the Java Sun coding standards, methods are
to be put in a functional order, so this fix makes sense.)

I was not certain if the fix also needed to be applied to the class IntroduceExpressionBasedMethodFix, so I left that
one the same.  If it does need to be applied to that class, it should only require changing the same three lines as
changed in IntroduceMethodFix.
Comment 4 jnicol 2009-08-30 22:33:04 UTC
Can this be added to the 6.8 release?  There is an attached patch for this.
Comment 5 Max Sauer 2009-08-31 12:07:52 UTC
Thanks for your contribution. There is already an implementation in org.​netbeans.​api.​java.​source.GeneratorUtilities, which adds even more logic.
ie.
GeneratorUtilities.get(copy).insertClassMember((ClassTree)pathToClass.getLeaf(), method);

I'll fix this soon.
Comment 6 Max Sauer 2009-08-31 16:30:09 UTC
Fixed.
---
http://hg.netbeans.org/jet-main/rev/27d3056062a7
Comment 7 Quality Engineering 2009-09-03 08:43:01 UTC
Integrated into 'main-golden', will be available in build *200909021401* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/27d3056062a7
User: Max Sauer <msauer@netbeans.org>
Log: #152701: "Introduce method" puts new method in an arbitrary location
Comment 8 jnicol 2009-09-07 15:37:07 UTC
I verified that the original sample works.  However, a similar one does not.

public class Main {
    private void main() {}
    void blah() { int i=5; }
}

If you highlight "int i=5;", and introduce a method called "a", then the method "a" appears ABOVE main().
If you highlight "int i=5;", and introduce a method called "c", then the method "c" appears BELOW blah().