Bug 218622 - [quick assist] Split declaration and move declaration to immediate outer block
Summary: [quick assist] Split declaration and move declaration to immediate outer block
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.4   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: fix candidate
Keywords:
: 333226 355286 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-02-12 08:39 EST by G. Ralph Kuntz, MD CLA
Modified: 2011-10-25 08:51 EDT (History)
7 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description G. Ralph Kuntz, MD CLA 2008-02-12 08:39:30 EST
I occasionally find myself creating a try-catch containing a variable declaration that I later find I need outside of the block. I have to cut/paste the declaration to above the try-catch (assigned null) and then remove the type from the variable inside the try-catch. A refactoring to do this automatically would be very handy.

For example:

try {
  SomeClass x = ...
} catch (Exception ex) {
  ...
}

would become

SomeClass x = null;
try {
  x = ...
} catch (Exception ex) {
  ...
}

after the refactoring.
Comment 1 Olivier Thomann CLA 2008-02-12 08:41:13 EST
Move to JDT/UI
Comment 2 Martin Aeschlimann CLA 2008-02-12 10:18:44 EST
Looks like a good idea for a quick assist (a new refactoring is a bit heavy weight for that)
Comment 3 G. Ralph Kuntz, MD CLA 2008-02-12 10:29:32 EST
(In reply to comment #2)
> Looks like a good idea for a quick assist (a new refactoring is a bit heavy
> weight for that)
> 

A Quick Assist would be fine.
Comment 4 Deepak Azad CLA 2010-11-11 14:27:31 EST
'Split declaration' quick assist plus 'Alt+Up' can be used to do this (see bug 102816). But then it still leaves initialization to null. 

I think the compiler will eventually complain in most cases after splitting the declaration - 'variable may not have been initialized'. Hence I wonder if 'Split declaration' should also initialize to null...
Comment 5 Markus Keller CLA 2010-11-15 05:30:02 EST
> I think the compiler will eventually complain in most cases after splitting the
> declaration - 'variable may not have been initialized'. Hence I wonder if
> 'Split declaration' should also initialize to null...

It's actually a good thing that the compiler complains in these cases. The user should be forced to think about the right way to fix this problem. Initializing to null is just one option -- but not necessarily the right one.

Example:
    public void foo() {
        FileInputStream fileInputStream;
        try {
            fileInputStream= new FileInputStream("a");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println(fileInputStream);
    }

In this case, we already have a quick fix "Initialize variable", so we don't have to do anything for the "Split declaration" case.

But besides the "Split declaration", this bug is about adding another quick assist that
- splits the declaration
- moves the declaration in front of the enclosing block (or even further up if there are already unresolved references to the selected variable in an outer scope)
- initializes the variable with the default value and enters linked mode with the initializer selected
Comment 6 G. Ralph Kuntz, MD CLA 2010-11-15 07:06:59 EST
Having a "Quick Fix" that produces code that will not compile is a bad idea. One of the alternatives (initialize to null, etc.) should be provide automatically.
Comment 7 Deepak Azad CLA 2010-12-27 05:23:43 EST
*** Bug 333226 has been marked as a duplicate of this bug. ***
Comment 8 Deepak Azad CLA 2011-10-18 21:51:23 EDT
*** Bug 355286 has been marked as a duplicate of this bug. ***
Comment 9 samarjit samanta CLA 2011-10-25 08:51:27 EDT
(In reply to comment #6)
> Having a "Quick Fix" that produces code that will not compile is a bad idea.
> One of the alternatives (initialize to null, etc.) should be provide
> automatically.

90% of the times initializing to null works, so I will vote to initialize it to null automatically. 
I will also add that the cursor should remain in the original location of inline variable so that developer does not get distracted form the logic he is already working on in that line.