langtools/test/tools/javac/diags/Example.java
changeset 27388 d694da45bd7a
parent 17798 abe47e71498b
child 27852 2e6ad0e4fe20
--- a/langtools/test/tools/javac/diags/Example.java	Wed Nov 05 19:09:09 2014 -0800
+++ b/langtools/test/tools/javac/diags/Example.java	Wed Nov 05 19:12:45 2014 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 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
@@ -31,6 +31,7 @@
 import javax.tools.DiagnosticCollector;
 import javax.tools.JavaCompiler;
 import javax.tools.JavaCompiler.CompilationTask;
+import javax.tools.JavaFileManager;
 import javax.tools.JavaFileObject;
 import javax.tools.StandardJavaFileManager;
 import javax.tools.ToolProvider;
@@ -311,24 +312,24 @@
 
         static class DefaultFactory implements Factory {
             public Compiler getCompiler(List<String> opts, boolean verbose) {
-            String first;
-            String[] rest;
-                if (opts == null || opts.isEmpty()) {
-                first = null;
-                rest = new String[0];
-            } else {
-                first = opts.get(0);
-                rest = opts.subList(1, opts.size()).toArray(new String[opts.size() - 1]);
+                String first;
+                String[] rest;
+                    if (opts == null || opts.isEmpty()) {
+                    first = null;
+                    rest = new String[0];
+                } else {
+                    first = opts.get(0);
+                    rest = opts.subList(1, opts.size()).toArray(new String[opts.size() - 1]);
+                }
+                if (first == null || first.equals("jsr199"))
+                    return new Jsr199Compiler(verbose, rest);
+                else if (first.equals("simple"))
+                    return new SimpleCompiler(verbose);
+                else if (first.equals("backdoor"))
+                    return new BackdoorCompiler(verbose);
+                else
+                    throw new IllegalArgumentException(first);
             }
-            if (first == null || first.equals("jsr199"))
-                return new Jsr199Compiler(verbose, rest);
-            else if (first.equals("simple"))
-                return new SimpleCompiler(verbose);
-            else if (first.equals("backdoor"))
-                return new BackdoorCompiler(verbose);
-            else
-                throw new IllegalArgumentException(first);
-                }
         }
 
         static Factory factory;
@@ -351,6 +352,14 @@
             loader = cl;
         }
 
+        protected void close(JavaFileManager fm) {
+            try {
+                fm.close();
+            } catch (IOException e) {
+                throw new Error(e);
+            }
+        }
+
         protected ClassLoader loader;
         protected boolean verbose;
     }
@@ -399,21 +408,25 @@
             JavaCompiler c = ToolProvider.getSystemJavaCompiler();
 
             StandardJavaFileManager fm = c.getStandardFileManager(dc, null, null);
-            if (fmOpts != null)
-                fm = new FileManager(fm, fmOpts);
+            try {
+                if (fmOpts != null)
+                    fm = new FileManager(fm, fmOpts);
 
-            Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);
+                Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);
 
-            CompilationTask t = c.getTask(out, fm, dc, opts, null, fos);
-            Boolean ok = t.call();
+                CompilationTask t = c.getTask(out, fm, dc, opts, null, fos);
+                Boolean ok = t.call();
 
-            if (keys != null) {
-                for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) {
-                    scanForKeys(unwrap(d), keys);
+                if (keys != null) {
+                    for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) {
+                        scanForKeys(unwrap(d), keys);
+                    }
                 }
+
+                return ok;
+            } finally {
+                close(fm);
             }
-
-            return ok;
         }
 
         /**
@@ -526,14 +539,19 @@
             Context c = new Context();
             JavacFileManager.preRegister(c); // can't create it until Log has been set up
             MessageTracker.preRegister(c, keys);
-            Main m = new Main("javac", pw);
-            Main.Result rc = m.compile(args.toArray(new String[args.size()]), c);
+
+            try {
+                Main m = new Main("javac", pw);
+                Main.Result rc = m.compile(args.toArray(new String[args.size()]), c);
 
-            if (keys != null) {
-                pw.close();
+                if (keys != null) {
+                    pw.close();
+                }
+
+                return rc.isOK();
+            } finally {
+                close(c.get(JavaFileManager.class));
             }
-
-            return rc.isOK();
         }
 
         static class MessageTracker extends JavacMessages {