Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 470810

Summary: extract method fails on try-with-resources that depends on other local variable
Product: [Eclipse Project] JDT Reporter: Rudolf Mayer <rudolf.mayer>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: jarthana
Version: 4.4.1   
Target Milestone: ---   
Hardware: All   
OS: Linux   
Whiteboard: stalebug

Description Rudolf Mayer CLA 2015-06-23 10:42:22 EDT
When using a try-with-resources statement in the to-be extracted code that depends on a previously defined local variable, this variable is not created as parameter in the new method. Example:


public class ExtractMethodError {
    public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {

        String file = "/tmp/test.txt";
        try (java.io.FileInputStream inputStream = new java.io.FileInputStream(file)) {
            System.out.println(inputStream.available());
        }
    }
}

When selecting the part in the try statement and extracting a method, this gets generated:

public class ExtractMethodError {
    public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {

        String file = "/tmp/test.txt";
        extractedMethod();
    }

    public static void extractedMethod() throws java.io.FileNotFoundException, java.io.IOException {
        try (java.io.FileInputStream inputStream = new java.io.FileInputStream(file)) {
            System.out.println(inputStream.available());
        }
    }
}

I.e. "file" is not created as parameter (and also not passed)
Comment 1 Jay Arthanareeswaran CLA 2015-06-24 02:56:04 EDT
I tried with a more recent version of eclipse (4.5 RC4) and I do see the new method gets the local variable as a argument. Can you please check with the latest build?
Comment 2 Eclipse Genie CLA 2020-05-02 08:33:18 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.