Daniel Trebbien created NETBEANS-280:
----------------------------------------

             Summary: "Convert to try-with-resources" fix can produce erroneous 
code
                 Key: NETBEANS-280
                 URL: https://issues.apache.org/jira/browse/NETBEANS-280
             Project: NetBeans
          Issue Type: Bug
          Components: java - Hints
            Reporter: Daniel Trebbien


Test case:
{code:java}
package hintstest;

import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;

public abstract class ConvertToTryWithResourcesTest {
    public void test(Path path) throws IOException {
        Writer w = Files.newBufferedWriter(path);
        try {
            doSomething();
        } catch (Exception e) {
            w.write("An exception occurred.\n");
        } finally {
            w.close();
        }
    }

    public abstract void doSomething() throws Exception;
}
{code}

Invoking the "Convert to try-with-resources" fix produces:
{code:java}
package hintstest;

import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;

public abstract class ConvertToTryWithResourcesTest {
    public void test(Path path) throws IOException {
        try (Writer w = Files.newBufferedWriter(path)) {
            doSomething();
        } catch (Exception e) {
            w.write("An exception occurred.\n");
        }
    }

    public abstract void doSomething() throws Exception;
}
{code}

.. which is erroneous because {{w}}, used in the catch block, is no longer in 
scope.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to