langtools/test/tools/javac/6902720/Test.java
changeset 27319 030080f03e4f
parent 5520 86e4b9a9da40
child 30730 d3ce7619db2c
--- a/langtools/test/tools/javac/6902720/Test.java	Wed Oct 29 12:09:17 2014 +0100
+++ b/langtools/test/tools/javac/6902720/Test.java	Wed Oct 29 17:25:23 2014 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -54,38 +54,39 @@
 
     void test(File test) throws Exception {
         JavacTool tool1 = JavacTool.create();
-        StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null);
-        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);
+        try (StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null)) {
+            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);
 
-        // parse test file into a tree, and write it out to a stringbuffer using Pretty
-        JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);
-        StringWriter sw = new StringWriter();
-        PrintWriter pw = new PrintWriter(sw);
-        Iterable<? extends CompilationUnitTree> trees = t1.parse();
-        for (CompilationUnitTree tree: trees) {
-            new Pretty(pw, true).printExpr((JCTree) tree);
-        }
-        pw.close();
+            // parse test file into a tree, and write it out to a stringbuffer using Pretty
+            JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);
+            StringWriter sw = new StringWriter();
+            PrintWriter pw = new PrintWriter(sw);
+            Iterable<? extends CompilationUnitTree> trees = t1.parse();
+            for (CompilationUnitTree tree: trees) {
+                new Pretty(pw, true).printExpr((JCTree) tree);
+            }
+            pw.close();
 
-        final String out = sw.toString();
-        System.err.println("generated code:\n" + out + "\n");
+            final String out = sw.toString();
+            System.err.println("generated code:\n" + out + "\n");
 
-        // verify the generated code is valid Java by compiling it
-        JavacTool tool2 = JavacTool.create();
-        JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {
-            @Override
-            public CharSequence getCharContent(boolean ignoreEncodingErrors) {
-                return out;
-            }
-        };
-        JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));
-        boolean ok = t2.call();
-        if (!ok)
-            throw new Exception("compilation of generated code failed");
+            // verify the generated code is valid Java by compiling it
+            JavacTool tool2 = JavacTool.create();
+            JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {
+                @Override
+                public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+                    return out;
+                }
+            };
+            JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));
+            boolean ok = t2.call();
+            if (!ok)
+                throw new Exception("compilation of generated code failed");
 
-        File expectedClass = new File(test.getName().replace(".java", ".class"));
-        if (!expectedClass.exists())
-            throw new Exception(expectedClass + " not found");
+            File expectedClass = new File(test.getName().replace(".java", ".class"));
+            if (!expectedClass.exists())
+                throw new Exception(expectedClass + " not found");
+        }
     }
 }