8062676: Tests which leak lots of file managers should be fixed (group 2)
authorjjg
Wed, 05 Nov 2014 19:12:45 -0800
changeset 27388 d694da45bd7a
parent 27387 54142fc27e8a
child 27389 301be76491af
8062676: Tests which leak lots of file managers should be fixed (group 2) Reviewed-by: darcy
langtools/src/jdk.compiler/share/classes/com/sun/tools/javap/JavapTask.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/SjavacImpl.java
langtools/test/lib/combo/tools/javac/combo/JavacTemplateTestBase.java
langtools/test/tools/javac/annotations/neg/8022765/VerifyErroneousAnnotationsAttributed.java
langtools/test/tools/javac/api/TestContainTypes.java
langtools/test/tools/javac/diags/ArgTypeCompilerFactory.java
langtools/test/tools/javac/diags/Example.java
langtools/test/tools/javac/lambda/lambdaExpression/SamConversionComboTest.java
langtools/test/tools/javac/parser/JavacParserTest.java
langtools/test/tools/javac/tree/TypeAnnotationsPretty.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javap/JavapTask.java	Wed Nov 05 19:09:09 2014 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javap/JavapTask.java	Wed Nov 05 19:12:45 2014 -0800
@@ -413,17 +413,17 @@
 
     int run(String[] args) {
         try {
-            handleOptions(args);
+            try {
+                handleOptions(args);
 
-            // the following gives consistent behavior with javac
-            if (classes == null || classes.size() == 0) {
-                if (options.help || options.version || options.fullVersion)
-                    return EXIT_OK;
-                else
-                    return EXIT_CMDERR;
-            }
+                // the following gives consistent behavior with javac
+                if (classes == null || classes.size() == 0) {
+                    if (options.help || options.version || options.fullVersion)
+                        return EXIT_OK;
+                    else
+                        return EXIT_CMDERR;
+                }
 
-            try {
                 return run();
             } finally {
                 if (defaultFileManager != null) {
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/SjavacImpl.java	Wed Nov 05 19:09:09 2014 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/SjavacImpl.java	Wed Nov 05 19:12:45 2014 -0800
@@ -25,6 +25,7 @@
 package com.sun.tools.sjavac.comp;
 
 import java.io.File;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.net.URI;
@@ -76,90 +77,93 @@
                                      Set<URI> sourcesToCompile,
                                      Set<URI> visibleSources) {
         JavacTool compiler = JavacTool.create();
-        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
-        SmartFileManager smartFileManager = new SmartFileManager(fileManager);
-        Context context = new Context();
+        try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
+            SmartFileManager smartFileManager = new SmartFileManager(fileManager);
+            Context context = new Context();
 
-        // Now setup the actual compilation....
-        CompilationResult compilationResult = new CompilationResult(0);
+            // Now setup the actual compilation....
+            CompilationResult compilationResult = new CompilationResult(0);
 
-        // First deal with explicit source files on cmdline and in at file.
-        ListBuffer<JavaFileObject> compilationUnits = new ListBuffer<>();
-        for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(explicitSources)) {
-            compilationUnits.append(i);
-        }
-        // Now deal with sources supplied as source_to_compile.
-        ListBuffer<File> sourcesToCompileFiles = new ListBuffer<>();
-        for (URI u : sourcesToCompile) {
-            sourcesToCompileFiles.append(new File(u));
-        }
-        for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(sourcesToCompileFiles)) {
-            compilationUnits.append(i);
-        }
+            // First deal with explicit source files on cmdline and in at file.
+            ListBuffer<JavaFileObject> compilationUnits = new ListBuffer<>();
+            for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(explicitSources)) {
+                compilationUnits.append(i);
+            }
+            // Now deal with sources supplied as source_to_compile.
+            ListBuffer<File> sourcesToCompileFiles = new ListBuffer<>();
+            for (URI u : sourcesToCompile) {
+                sourcesToCompileFiles.append(new File(u));
+            }
+            for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(sourcesToCompileFiles)) {
+                compilationUnits.append(i);
+            }
 
-        // Create a new logger.
-        StringWriter stdoutLog = new StringWriter();
-        StringWriter stderrLog = new StringWriter();
-        PrintWriter stdout = new PrintWriter(stdoutLog);
-        PrintWriter stderr = new PrintWriter(stderrLog);
-        com.sun.tools.javac.main.Main.Result rc = com.sun.tools.javac.main.Main.Result.OK;
-        DependencyCollector depsCollector = new DependencyCollector();
-        PublicApiCollector pubApiCollector = new PublicApiCollector();
-        PathAndPackageVerifier papVerifier = new PathAndPackageVerifier();
-        try {
-            if (compilationUnits.size() > 0) {
-                smartFileManager.setVisibleSources(visibleSources);
-                smartFileManager.cleanArtifacts();
-                smartFileManager.setLog(stdout);
+            // Create a new logger.
+            StringWriter stdoutLog = new StringWriter();
+            StringWriter stderrLog = new StringWriter();
+            PrintWriter stdout = new PrintWriter(stdoutLog);
+            PrintWriter stderr = new PrintWriter(stderrLog);
+            com.sun.tools.javac.main.Main.Result rc = com.sun.tools.javac.main.Main.Result.OK;
+            DependencyCollector depsCollector = new DependencyCollector();
+            PublicApiCollector pubApiCollector = new PublicApiCollector();
+            PathAndPackageVerifier papVerifier = new PathAndPackageVerifier();
+            try {
+                if (compilationUnits.size() > 0) {
+                    smartFileManager.setVisibleSources(visibleSources);
+                    smartFileManager.cleanArtifacts();
+                    smartFileManager.setLog(stdout);
 
-                // Do the compilation!
-                JavacTaskImpl task =
-                        (JavacTaskImpl) compiler.getTask(stderr,
-                                                         smartFileManager,
-                                                         null,
-                                                         Arrays.asList(args),
-                                                         null,
-                                                         compilationUnits,
-                                                         context);
-                smartFileManager.setSymbolFileEnabled(!Options.instance(context).isSet("ignore.symbol.file"));
-                task.addTaskListener(depsCollector);
-                task.addTaskListener(pubApiCollector);
-                task.addTaskListener(papVerifier);
-                rc = task.doCall();
-                smartFileManager.flush();
+                    // Do the compilation!
+                    JavacTaskImpl task =
+                            (JavacTaskImpl) compiler.getTask(stderr,
+                                                             smartFileManager,
+                                                             null,
+                                                             Arrays.asList(args),
+                                                             null,
+                                                             compilationUnits,
+                                                             context);
+                    smartFileManager.setSymbolFileEnabled(!Options.instance(context).isSet("ignore.symbol.file"));
+                    task.addTaskListener(depsCollector);
+                    task.addTaskListener(pubApiCollector);
+                    task.addTaskListener(papVerifier);
+                    rc = task.doCall();
+                    smartFileManager.flush();
+                }
+            } catch (Exception e) {
+                stderrLog.append(Util.getStackTrace(e));
+                rc = com.sun.tools.javac.main.Main.Result.ERROR;
             }
-        } catch (Exception e) {
-            stderrLog.append(Util.getStackTrace(e));
-            rc = com.sun.tools.javac.main.Main.Result.ERROR;
-        }
 
-        compilationResult.packageArtifacts = smartFileManager.getPackageArtifacts();
+            compilationResult.packageArtifacts = smartFileManager.getPackageArtifacts();
 
-        Dependencies deps = Dependencies.instance(context);
-        for (PackageSymbol from : depsCollector.getSourcePackages()) {
-            for (PackageSymbol to : depsCollector.getDependenciesForPkg(from))
-                deps.collect(from.fullname, to.fullname);
-        }
+            Dependencies deps = Dependencies.instance(context);
+            for (PackageSymbol from : depsCollector.getSourcePackages()) {
+                for (PackageSymbol to : depsCollector.getDependenciesForPkg(from))
+                    deps.collect(from.fullname, to.fullname);
+            }
 
-        for (ClassSymbol cs : pubApiCollector.getClassSymbols())
-            deps.visitPubapi(cs);
+            for (ClassSymbol cs : pubApiCollector.getClassSymbols())
+                deps.visitPubapi(cs);
 
-        if (papVerifier.getMisplacedCompilationUnits().size() > 0) {
-            for (CompilationUnitTree cu : papVerifier.getMisplacedCompilationUnits()) {
-                System.err.println("Misplaced compilation unit.");
-                System.err.println("    Directory: " + Paths.get(cu.getSourceFile().toUri()).getParent());
-                System.err.println("    Package:   " + cu.getPackageName());
+            if (papVerifier.getMisplacedCompilationUnits().size() > 0) {
+                for (CompilationUnitTree cu : papVerifier.getMisplacedCompilationUnits()) {
+                    System.err.println("Misplaced compilation unit.");
+                    System.err.println("    Directory: " + Paths.get(cu.getSourceFile().toUri()).getParent());
+                    System.err.println("    Package:   " + cu.getPackageName());
+                }
+                rc = com.sun.tools.javac.main.Main.Result.ERROR;
             }
-            rc = com.sun.tools.javac.main.Main.Result.ERROR;
+
+            compilationResult.packageDependencies = deps.getDependencies();
+            compilationResult.packagePubapis = deps.getPubapis();
+            compilationResult.stdout = stdoutLog.toString();
+            compilationResult.stderr = stderrLog.toString();
+            compilationResult.returnCode = rc.exitCode;
+
+            return compilationResult;
+        } catch (IOException e) {
+            throw new Error(e);
         }
-
-        compilationResult.packageDependencies = deps.getDependencies();
-        compilationResult.packagePubapis = deps.getPubapis();
-        compilationResult.stdout = stdoutLog.toString();
-        compilationResult.stderr = stderrLog.toString();
-        compilationResult.returnCode = rc.exitCode;
-
-        return compilationResult;
     }
 
     @Override
--- a/langtools/test/lib/combo/tools/javac/combo/JavacTemplateTestBase.java	Wed Nov 05 19:09:09 2014 -0800
+++ b/langtools/test/lib/combo/tools/javac/combo/JavacTemplateTestBase.java	Wed Nov 05 19:12:45 2014 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 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
@@ -245,21 +245,22 @@
 
     private File compile(List<File> classpaths, List<JavaFileObject> files, boolean generate) throws IOException {
         JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
-        StandardJavaFileManager fm = systemJavaCompiler.getStandardFileManager(null, null, null);
-        if (classpaths.size() > 0)
-            fm.setLocation(StandardLocation.CLASS_PATH, classpaths);
-        JavacTask ct = (JavacTask) systemJavaCompiler.getTask(null, fm, diags, compileOptions, null, files);
-        if (generate) {
-            File destDir = new File(root, Integer.toString(counter.incrementAndGet()));
-            // @@@ Assert that this directory didn't exist, or start counter at max+1
-            destDir.mkdirs();
-            fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
-            ct.generate();
-            return destDir;
-        }
-        else {
-            ct.analyze();
-            return nullDir;
+        try (StandardJavaFileManager fm = systemJavaCompiler.getStandardFileManager(null, null, null)) {
+            if (classpaths.size() > 0)
+                fm.setLocation(StandardLocation.CLASS_PATH, classpaths);
+            JavacTask ct = (JavacTask) systemJavaCompiler.getTask(null, fm, diags, compileOptions, null, files);
+            if (generate) {
+                File destDir = new File(root, Integer.toString(counter.incrementAndGet()));
+                // @@@ Assert that this directory didn't exist, or start counter at max+1
+                destDir.mkdirs();
+                fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
+                ct.generate();
+                return destDir;
+            }
+            else {
+                ct.analyze();
+                return nullDir;
+            }
         }
     }
 
--- a/langtools/test/tools/javac/annotations/neg/8022765/VerifyErroneousAnnotationsAttributed.java	Wed Nov 05 19:09:09 2014 -0800
+++ b/langtools/test/tools/javac/annotations/neg/8022765/VerifyErroneousAnnotationsAttributed.java	Wed Nov 05 19:12:45 2014 -0800
@@ -44,6 +44,7 @@
 import javax.lang.model.type.TypeKind;
 import javax.tools.Diagnostic;
 import javax.tools.DiagnosticListener;
+import javax.tools.JavaFileManager;
 import javax.tools.JavaFileObject;
 import javax.tools.SimpleJavaFileObject;
 
@@ -52,22 +53,26 @@
         new VerifyErroneousAnnotationsAttributed().run();
     }
 
-    void run() {
-        int failCount = 0;
-        for (String ann : generateAnnotations()) {
-            String code = PATTERN.replace("PLACEHOLDER", ann);
-            try {
-                validate(code);
-            } catch (Throwable t) {
-                System.out.println("Failed for: ");
-                System.out.println(code);
-                t.printStackTrace(System.out);
-                failCount++;
+    void run() throws IOException {
+        try {
+            int failCount = 0;
+            for (String ann : generateAnnotations()) {
+                String code = PATTERN.replace("PLACEHOLDER", ann);
+                try {
+                    validate(code);
+                } catch (Throwable t) {
+                    System.out.println("Failed for: ");
+                    System.out.println(code);
+                    t.printStackTrace(System.out);
+                    failCount++;
+                }
             }
-        }
 
-        if (failCount > 0) {
-            throw new IllegalStateException("failed sub-tests: " + failCount);
+            if (failCount > 0) {
+                throw new IllegalStateException("failed sub-tests: " + failCount);
+            }
+        } finally {
+            fm.close();
         }
     }
 
@@ -221,13 +226,14 @@
     }
 
     final JavacTool tool = JavacTool.create();
+    final JavaFileManager fm = tool.getStandardFileManager(null, null, null);
     final DiagnosticListener<JavaFileObject> devNull = new DiagnosticListener<JavaFileObject>() {
         @Override public void report(Diagnostic<? extends JavaFileObject> diagnostic) {}
     };
 
     void validate(String code) throws IOException, URISyntaxException {
         JavacTask task = tool.getTask(null,
-                                      null,
+                                      fm,
                                       devNull,
                                       Arrays.asList("-XDshouldStopPolicy=FLOW"),
                                       null,
--- a/langtools/test/tools/javac/api/TestContainTypes.java	Wed Nov 05 19:09:09 2014 -0800
+++ b/langtools/test/tools/javac/api/TestContainTypes.java	Wed Nov 05 19:12:45 2014 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 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
@@ -34,19 +34,20 @@
 import java.util.Set;
 import javax.annotation.processing.AbstractProcessor;
 import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedSourceVersion;
+import javax.lang.model.SourceVersion;
 import javax.lang.model.element.Element;
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.element.ExecutableElement;
 import javax.lang.model.type.TypeMirror;
 import javax.lang.model.type.DeclaredType;
 import javax.tools.JavaCompiler;
+import javax.tools.JavaFileManager;
 import javax.tools.JavaFileObject;
 import javax.tools.SimpleJavaFileObject;
 import javax.tools.ToolProvider;
 
 import com.sun.source.util.JavacTask;
-import javax.annotation.processing.SupportedSourceVersion;
-import javax.lang.model.SourceVersion;
 
 public class TestContainTypes {
 
@@ -127,22 +128,28 @@
         }
     }
 
+    static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
+    static final JavaFileManager fm = tool.getStandardFileManager(null, null, null);
+
     public static void main(String... args) throws Exception {
-        for (ClassType ctA : ClassType.values()) {
-            for (ParameterType ptA : ParameterType.values()) {
-                for (ClassType ctB : ClassType.values()) {
-                    for (ParameterType ptB : ParameterType.values()) {
-                        compileAndCheck(ptA, ctA, ptB, ctB);
+        try {
+            for (ClassType ctA : ClassType.values()) {
+                for (ParameterType ptA : ParameterType.values()) {
+                    for (ClassType ctB : ClassType.values()) {
+                        for (ParameterType ptB : ParameterType.values()) {
+                            compileAndCheck(ptA, ctA, ptB, ctB);
+                        }
                     }
                 }
             }
+        } finally {
+            fm.close();
         }
     }
 
     static void compileAndCheck(ParameterType ptA, ClassType ctA, ParameterType ptB, ClassType ctB) throws Exception {
         JavaSource source = new JavaSource(ptA.instantiate(ctA), ptB.instantiate(ctB));
-        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
-        JavacTask ct = (JavacTask)tool.getTask(null, null, null,
+        JavacTask ct = (JavacTask)tool.getTask(null, fm, null,
                 null, null, Arrays.asList(source));
         ct.setProcessors(Arrays.asList(new ContainTypesTester(ParameterType.contains(ptA, ctA, ptB, ctB), source)));
         System.err.println("A = " + ptA +" / " + ptA.instantiate(ctA));
--- a/langtools/test/tools/javac/diags/ArgTypeCompilerFactory.java	Wed Nov 05 19:09:09 2014 -0800
+++ b/langtools/test/tools/javac/diags/ArgTypeCompilerFactory.java	Wed Nov 05 19:12:45 2014 -0800
@@ -100,14 +100,18 @@
             JavacTool tool = JavacTool.create();
 
             StandardJavaFileManager fm = tool.getStandardFileManager(null, 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);
-
-            Context c = initContext();
-            JavacTaskImpl t = (JavacTaskImpl) tool.getTask(out, fm, null, opts, null, fos, c);
-            return t.call();
+                Context c = initContext();
+                JavacTaskImpl t = (JavacTaskImpl) tool.getTask(out, fm, null, opts, null, fos, c);
+                return t.call();
+            } finally {
+                close(fm);
+            }
         }
     }
 
@@ -136,9 +140,14 @@
             Main main = new Main("javac", out);
             Context c = initContext();
             JavacFileManager.preRegister(c); // can't create it until Log has been set up
-            Main.Result result = main.compile(args.toArray(new String[args.size()]), c);
+
+            try {
+                Main.Result result = main.compile(args.toArray(new String[args.size()]), c);
 
-            return result.isOK();
+                return result.isOK();
+            } finally {
+                close(c.get(JavaFileManager.class));
+            }
         }
     }
 
@@ -160,10 +169,15 @@
 
             Context c = initContext();
             JavacFileManager.preRegister(c); // can't create it until Log has been set up
-            Main m = new Main("javac", out);
-            Main.Result result = m.compile(args.toArray(new String[args.size()]), c);
+
+            try {
+                Main m = new Main("javac", out);
+                Main.Result result = m.compile(args.toArray(new String[args.size()]), c);
 
-            return result.isOK();
+                return result.isOK();
+            } finally {
+                close(c.get(JavaFileManager.class));
+            }
         }
     }
 
--- 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 {
--- a/langtools/test/tools/javac/lambda/lambdaExpression/SamConversionComboTest.java	Wed Nov 05 19:09:09 2014 -0800
+++ b/langtools/test/tools/javac/lambda/lambdaExpression/SamConversionComboTest.java	Wed Nov 05 19:12:45 2014 -0800
@@ -35,6 +35,7 @@
 import java.util.Arrays;
 import javax.tools.Diagnostic;
 import javax.tools.JavaCompiler;
+import javax.tools.JavaFileManager;
 import javax.tools.JavaFileObject;
 import javax.tools.SimpleJavaFileObject;
 import javax.tools.ToolProvider;
@@ -206,9 +207,8 @@
         String clientFileStr = clientSourceFile.toString();
         System.out.println(clientFileStr.substring(0, clientFileStr.indexOf("\n\n")));
 
-        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
         DiagnosticChecker dc = new DiagnosticChecker();
-        JavacTask ct = (JavacTask)tool.getTask(null, null, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
+        JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
         try {
             ct.analyze();
         } catch (Exception e) {
@@ -255,6 +255,9 @@
     ReturnValue returnValue;
     static int count = 0;
 
+    static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
+    static JavaFileManager fm = comp.getStandardFileManager(null, null, null);
+
     SamConversionComboTest(FInterface f, Context c, LambdaBody lb, LambdaKind lk, ReturnValue rv) {
         fInterface = f;
         context = c;
@@ -264,17 +267,21 @@
     }
 
     public static void main(String[] args) throws Exception {
-        for(Context ct : Context.values()) {
-            for (FInterface fi : FInterface.values()) {
-                for (LambdaKind lk: LambdaKind.values()) {
-                    for (LambdaBody lb : LambdaBody.values()) {
-                        for(ReturnValue rv : ReturnValue.values()) {
-                            new SamConversionComboTest(fi, ct, lb, lk, rv).test();
+        try {
+            for(Context ct : Context.values()) {
+                for (FInterface fi : FInterface.values()) {
+                    for (LambdaKind lk: LambdaKind.values()) {
+                        for (LambdaBody lb : LambdaBody.values()) {
+                            for(ReturnValue rv : ReturnValue.values()) {
+                                new SamConversionComboTest(fi, ct, lb, lk, rv).test();
+                            }
                         }
                     }
                 }
             }
+        System.out.println("total tests: " + count);
+        } finally {
+            fm.close();
         }
-        System.out.println("total tests: " + count);
     }
 }
--- a/langtools/test/tools/javac/parser/JavacParserTest.java	Wed Nov 05 19:09:09 2014 -0800
+++ b/langtools/test/tools/javac/parser/JavacParserTest.java	Wed Nov 05 19:12:45 2014 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 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
@@ -67,17 +67,23 @@
 import javax.tools.DiagnosticCollector;
 import javax.tools.DiagnosticListener;
 import javax.tools.JavaCompiler;
+import javax.tools.JavaFileManager;
 import javax.tools.JavaFileObject;
 import javax.tools.SimpleJavaFileObject;
 import javax.tools.ToolProvider;
 
 public class JavacParserTest extends TestCase {
     static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
+    static final JavaFileManager fm = tool.getStandardFileManager(null, null, null);
 
     private JavacParserTest(){}
 
     public static void main(String... args) throws Exception {
-        new JavacParserTest().run(args);
+        try {
+            new JavacParserTest().run(args);
+        } finally {
+            fm.close();
+        }
     }
 
     class MyFileObject extends SimpleJavaFileObject {
@@ -103,7 +109,7 @@
 
     CompilationUnitTree getCompilationUnitTree(String code) throws IOException {
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
                 null, Arrays.asList(new MyFileObject(code)));
         CompilationUnitTree cut = ct.parse().iterator().next();
         return cut;
@@ -129,7 +135,7 @@
 
         String code = "package test; public class Test {public Test() {super();}}";
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
                 null, Arrays.asList(new MyFileObject(code)));
         CompilationUnitTree cut = ct.parse().iterator().next();
         SourcePositions pos = Trees.instance(ct).getSourcePositions();
@@ -168,7 +174,7 @@
         final String theString = "public";
         String code = "package test; " + theString + " enum Test {A;}";
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
                 null, Arrays.asList(new MyFileObject(code)));
         CompilationUnitTree cut = ct.parse().iterator().next();
         SourcePositions pos = Trees.instance(ct).getSourcePositions();
@@ -191,7 +197,7 @@
                 "class d {} private void method() { " +
                 "Object o = " + theString + "; } }";
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
                 null, Arrays.asList(new MyFileObject(code)));
         CompilationUnitTree cut = ct.parse().iterator().next();
         SourcePositions pos = Trees.instance(ct).getSourcePositions();
@@ -238,7 +244,7 @@
         final List<Diagnostic<? extends JavaFileObject>> errors =
                 new LinkedList<Diagnostic<? extends JavaFileObject>>();
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm,
                 new DiagnosticListener<JavaFileObject>() {
             public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
                 errors.add(diagnostic);
@@ -261,7 +267,7 @@
 
         String code = "\n@interface Test {}";
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
                 null, Arrays.asList(new MyFileObject(code)));
 
         CompilationUnitTree cut = ct.parse().iterator().next();
@@ -300,7 +306,7 @@
         final List<Diagnostic<? extends JavaFileObject>> errors =
                 new LinkedList<Diagnostic<? extends JavaFileObject>>();
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm,
                 new DiagnosticListener<JavaFileObject>() {
 
             public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
@@ -443,7 +449,7 @@
         final List<Diagnostic<? extends JavaFileObject>> errors =
                 new LinkedList<Diagnostic<? extends JavaFileObject>>();
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm,
                 new DiagnosticListener<JavaFileObject>() {
                     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
                         errors.add(diagnostic);
@@ -482,7 +488,7 @@
 
         String code = "package t; class Test { <T> void t() {} }";
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
                 null, Arrays.asList(new MyFileObject(code)));
         CompilationUnitTree cut = ct.parse().iterator().next();
         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
@@ -505,7 +511,7 @@
         DiagnosticCollector<JavaFileObject> coll =
                 new DiagnosticCollector<JavaFileObject>();
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
                 null, Arrays.asList(new MyFileObject(code)));
 
         ct.parse();
@@ -529,7 +535,7 @@
                 "if (name != null) class X {} } }";
         DiagnosticCollector<JavaFileObject> coll =
                 new DiagnosticCollector<JavaFileObject>();
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
                 null, Arrays.asList(new MyFileObject(code)));
 
         ct.parse();
@@ -552,7 +558,7 @@
                 "if (true) abstract class F {} }}";
         DiagnosticCollector<JavaFileObject> coll =
                 new DiagnosticCollector<JavaFileObject>();
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
                 null, Arrays.asList(new MyFileObject(code)));
 
         ct.parse();
@@ -575,7 +581,7 @@
                 "if (name != null) interface X {} } }";
         DiagnosticCollector<JavaFileObject> coll =
                 new DiagnosticCollector<JavaFileObject>();
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
                 null, Arrays.asList(new MyFileObject(code)));
 
         ct.parse();
@@ -598,7 +604,7 @@
                 "if (true) } }";
         DiagnosticCollector<JavaFileObject> coll =
                 new DiagnosticCollector<JavaFileObject>();
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
                 null, Arrays.asList(new MyFileObject(code)));
 
         ct.parse();
@@ -620,7 +626,7 @@
 
         String code = "\nclass Test { { System.err.println(0e); } }";
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
                 null, Arrays.asList(new MyFileObject(code)));
 
         assertNotNull(ct.parse().iterator().next());
@@ -820,7 +826,7 @@
                     + "        };\n"
                     + "    }\n"
                     + "}";
-            JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null,
+            JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null,
                     null, null, Arrays.asList(new MyFileObject(code)));
             CompilationUnitTree cut = ct.parse().iterator().next();
 
@@ -861,7 +867,7 @@
                     + "    }\n"
                     + "}";
 
-            JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null,
+            JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null,
                     null, null, Arrays.asList(new MyFileObject(code)));
             CompilationUnitTree cut = ct.parse().iterator().next();
 
@@ -886,7 +892,7 @@
 
         String code = "package t; enum Test { AAA; }";
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
                 null, Arrays.asList(new MyFileObject(code)));
         CompilationUnitTree cut = ct.parse().iterator().next();
         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
@@ -905,7 +911,7 @@
                 "}";
         DiagnosticCollector<JavaFileObject> coll =
                 new DiagnosticCollector<>();
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
+        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
                 null, Arrays.asList(new MyFileObject(code)));
 
         CompilationUnitTree cut = ct.parse().iterator().next();
--- a/langtools/test/tools/javac/tree/TypeAnnotationsPretty.java	Wed Nov 05 19:09:09 2014 -0800
+++ b/langtools/test/tools/javac/tree/TypeAnnotationsPretty.java	Wed Nov 05 19:12:45 2014 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 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
@@ -40,6 +40,7 @@
 import java.util.LinkedList;
 
 import javax.tools.JavaCompiler;
+import javax.tools.JavaFileManager;
 import javax.tools.JavaFileObject;
 import javax.tools.SimpleJavaFileObject;
 import javax.tools.ToolProvider;
@@ -114,13 +115,15 @@
                 code + "; }" +
                 postfix;
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
-                null, Arrays.asList(new MyFileObject(src)));
+        try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
+            JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
+                    null, Arrays.asList(new MyFileObject(src)));
 
-        for (CompilationUnitTree cut : ct.parse()) {
-            JCTree.JCVariableDecl var =
-                    (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
-            checkMatch(code, var);
+            for (CompilationUnitTree cut : ct.parse()) {
+                JCTree.JCVariableDecl var =
+                        (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
+                checkMatch(code, var);
+            }
         }
     }
 
@@ -129,14 +132,16 @@
                 code + "}" +
                 postfix;
 
-        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
-                null, Arrays.asList(new MyFileObject(src)));
+        try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
+            JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
+                    null, Arrays.asList(new MyFileObject(src)));
 
 
-        for (CompilationUnitTree cut : ct.parse()) {
-            JCTree.JCMethodDecl meth =
-                    (JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
-            checkMatch(code, meth);
+            for (CompilationUnitTree cut : ct.parse()) {
+                JCTree.JCMethodDecl meth =
+                        (JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
+                checkMatch(code, meth);
+            }
         }
     }