You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by "Daniel Trebbien (JIRA)" <ji...@apache.org> on 2018/01/13 20:33:00 UTC

[jira] [Updated] (NETBEANS-280) "Convert to try-with-resources" fix can produce erroneous code

     [ https://issues.apache.org/jira/browse/NETBEANS-280?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Daniel Trebbien updated NETBEANS-280:
-------------------------------------
    Description: 
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 on {{Writer w}} 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.

  was:
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.


> "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 on {{Writer w}} 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: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

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