Basic support for try-with-resources.
authorJan Lahoda <jlahoda@netbeans.org>
Thu, 28 Apr 2011 20:13:39 +0200
changeset 569e667b3883bb4
parent 568 90cb741e26df
child 570 f22a98c1c944
Basic support for try-with-resources.
api/src/org/netbeans/modules/jackpot30/impl/pm/CopyFinder.java
api/src/org/netbeans/modules/jackpot30/spi/JavaFix.java
api/test/unit/src/org/netbeans/modules/jackpot30/impl/pm/CopyFinderTest.java
api/test/unit/src/org/netbeans/modules/jackpot30/spi/JavaFixTest.java
     1.1 --- a/api/src/org/netbeans/modules/jackpot30/impl/pm/CopyFinder.java	Tue Apr 26 19:56:59 2011 +0200
     1.2 +++ b/api/src/org/netbeans/modules/jackpot30/impl/pm/CopyFinder.java	Thu Apr 28 20:13:39 2011 +0200
     1.3 @@ -1249,6 +1249,10 @@
     1.4  
     1.5          TryTree at = (TryTree) p.getLeaf();
     1.6  
     1.7 +        if (!checkLists(node.getResources(), at.getResources(), p)) {
     1.8 +            return false;
     1.9 +        }
    1.10 +
    1.11          if (!scan(node.getBlock(), at.getBlock(), p)) {
    1.12              return false;
    1.13          }
     2.1 --- a/api/src/org/netbeans/modules/jackpot30/spi/JavaFix.java	Tue Apr 26 19:56:59 2011 +0200
     2.2 +++ b/api/src/org/netbeans/modules/jackpot30/spi/JavaFix.java	Thu Apr 28 20:13:39 2011 +0200
     2.3 @@ -946,8 +946,9 @@
     2.4  
     2.5          @Override
     2.6          public Number visitTry(TryTree node, Void p) {
     2.7 +            List<? extends Tree> resources = (List<? extends Tree>) resolveMultiParameters(node.getResources());
     2.8              List<? extends CatchTree> catches = (List<? extends CatchTree>) resolveMultiParameters(node.getCatches());
     2.9 -            TryTree nue = wc.getTreeMaker().Try(node.getBlock(), catches, node.getFinallyBlock());
    2.10 +            TryTree nue = wc.getTreeMaker().Try(resources, node.getBlock(), catches, node.getFinallyBlock());
    2.11  
    2.12              wc.rewrite(node, nue);
    2.13              return super.visitTry(node, p);
     3.1 --- a/api/test/unit/src/org/netbeans/modules/jackpot30/impl/pm/CopyFinderTest.java	Tue Apr 26 19:56:59 2011 +0200
     3.2 +++ b/api/test/unit/src/org/netbeans/modules/jackpot30/impl/pm/CopyFinderTest.java	Thu Apr 28 20:13:39 2011 +0200
     3.3 @@ -308,4 +308,18 @@
     3.4                               true);
     3.5      }
     3.6  
     3.7 +    public void testTryWithResources() throws Exception {
     3.8 +        performVariablesTest("package test; public class Test { { try (java.io.InputStream in = null) { System.err.println(1); } } }",
     3.9 +                             "try ($resources$) {$body$;}",
    3.10 +                             new Pair[] {
    3.11 +                             },
    3.12 +                             new Pair[] {
    3.13 +                                new Pair<String, int[]>("$resources$", new int[] {41, 70}),
    3.14 +                                new Pair<String, int[]>("$body$", new int[] {74, 96}),
    3.15 +                             },
    3.16 +                             new Pair[] {
    3.17 +                             },
    3.18 +                             false,
    3.19 +                             true);
    3.20 +    }
    3.21  }
     4.1 --- a/api/test/unit/src/org/netbeans/modules/jackpot30/spi/JavaFixTest.java	Tue Apr 26 19:56:59 2011 +0200
     4.2 +++ b/api/test/unit/src/org/netbeans/modules/jackpot30/spi/JavaFixTest.java	Thu Apr 28 20:13:39 2011 +0200
     4.3 @@ -466,6 +466,33 @@
     4.4                             "}\n");
     4.5      }
     4.6  
     4.7 +    public void testTryWithResourceTarget() throws Exception {
     4.8 +        performRewriteTest("package test;\n" +
     4.9 +                           "import java.io.InputStream;\n" +
    4.10 +                           "public class Test {\n" +
    4.11 +                           "    private void t() throws Exception {\n" +
    4.12 +                           "        InputStream in = null;\n" +
    4.13 +                           "        try {\n" +
    4.14 +                           "        } finally {\n" +
    4.15 +                           "            in.close()\n" +
    4.16 +                           "        }\n" +
    4.17 +                           "    }\n" +
    4.18 +                           "}\n",
    4.19 +                           "$type $var = $init; try {} finally {$var.close();} => try ($type $var = $init) {} finally {$var.close();}",
    4.20 +                           "package test;\n" +
    4.21 +                           "import java.io.InputStream;\n" +
    4.22 +                           "public class Test {\n" +
    4.23 +                           "    private void t() throws Exception {\n" +
    4.24 +//                           "        try (InputStream in = null) {\n" +
    4.25 +                           //XXX:
    4.26 +                           "        try (final InputStream in = null) {\n" +
    4.27 +                           "        } finally {\n" +
    4.28 +                           "            in.close()\n" +
    4.29 +                           "        }\n" +
    4.30 +                           "    }\n" +
    4.31 +		           "}\n");
    4.32 +    }
    4.33 +
    4.34      public void performRewriteTest(String code, String rule, String golden) throws Exception {
    4.35  	prepareTest("test/Test.java", code);
    4.36