8062676: Tests which leak lots of file managers should be fixed (group 2)

Reviewed-by: darcy
This commit is contained in:
Jonathan Gibbons 2014-11-05 19:12:45 -08:00
parent b96daffd2e
commit a75d2dbd39
10 changed files with 286 additions and 218 deletions

View file

@ -412,6 +412,7 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
EXIT_ABNORMAL = 4; // Compiler terminated abnormally
int run(String[] args) {
try {
try {
handleOptions(args);
@ -423,7 +424,6 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
return EXIT_CMDERR;
}
try {
return run();
} finally {
if (defaultFileManager != null) {

View file

@ -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,7 +77,7 @@ public class SjavacImpl implements Sjavac {
Set<URI> sourcesToCompile,
Set<URI> visibleSources) {
JavacTool compiler = JavacTool.create();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
SmartFileManager smartFileManager = new SmartFileManager(fileManager);
Context context = new Context();
@ -160,6 +161,9 @@ public class SjavacImpl implements Sjavac {
compilationResult.returnCode = rc.exitCode;
return compilationResult;
} catch (IOException e) {
throw new Error(e);
}
}
@Override

View file

@ -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,7 +245,7 @@ public abstract class JavacTemplateTestBase {
private File compile(List<File> classpaths, List<JavaFileObject> files, boolean generate) throws IOException {
JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = systemJavaCompiler.getStandardFileManager(null, null, null);
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);
@ -262,6 +262,7 @@ public abstract class JavacTemplateTestBase {
return nullDir;
}
}
}
/** Load the given class using the provided list of class paths */
protected Class<?> loadClass(String className, File... destDirs) {

View file

@ -44,6 +44,7 @@ import javax.lang.model.element.ElementKind;
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,7 +53,8 @@ public class VerifyErroneousAnnotationsAttributed {
new VerifyErroneousAnnotationsAttributed().run();
}
void run() {
void run() throws IOException {
try {
int failCount = 0;
for (String ann : generateAnnotations()) {
String code = PATTERN.replace("PLACEHOLDER", ann);
@ -69,6 +71,9 @@ public class VerifyErroneousAnnotationsAttributed {
if (failCount > 0) {
throw new IllegalStateException("failed sub-tests: " + failCount);
}
} finally {
fm.close();
}
}
List<String> generateAnnotations() {
@ -221,13 +226,14 @@ public class VerifyErroneousAnnotationsAttributed {
}
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,

View file

@ -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.HashSet;
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,7 +128,11 @@ public class TestContainTypes {
}
}
static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
static final JavaFileManager fm = tool.getStandardFileManager(null, null, null);
public static void main(String... args) throws Exception {
try {
for (ClassType ctA : ClassType.values()) {
for (ParameterType ptA : ParameterType.values()) {
for (ClassType ctB : ClassType.values()) {
@ -137,12 +142,14 @@ public class TestContainTypes {
}
}
}
} 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));

View file

@ -100,6 +100,7 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
JavacTool tool = JavacTool.create();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
try {
if (fmOpts != null)
fm = new FileManager(fm, fmOpts);
@ -108,6 +109,9 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
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 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
Main main = new Main("javac", out);
Context c = initContext();
JavacFileManager.preRegister(c); // can't create it until Log has been set up
try {
Main.Result result = main.compile(args.toArray(new String[args.size()]), c);
return result.isOK();
} finally {
close(c.get(JavaFileManager.class));
}
}
}
@ -160,10 +169,15 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
Context c = initContext();
JavacFileManager.preRegister(c); // can't create it until Log has been set up
try {
Main m = new Main("javac", out);
Main.Result result = m.compile(args.toArray(new String[args.size()]), c);
return result.isOK();
} finally {
close(c.get(JavaFileManager.class));
}
}
}

View file

@ -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.Diagnostic;
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;
@ -351,6 +352,14 @@ class Example implements Comparable<Example> {
loader = cl;
}
protected void close(JavaFileManager fm) {
try {
fm.close();
} catch (IOException e) {
throw new Error(e);
}
}
protected ClassLoader loader;
protected boolean verbose;
}
@ -399,6 +408,7 @@ class Example implements Comparable<Example> {
JavaCompiler c = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = c.getStandardFileManager(dc, null, null);
try {
if (fmOpts != null)
fm = new FileManager(fm, fmOpts);
@ -414,6 +424,9 @@ class Example implements Comparable<Example> {
}
return ok;
} finally {
close(fm);
}
}
/**
@ -526,6 +539,8 @@ class Example implements Comparable<Example> {
Context c = new Context();
JavacFileManager.preRegister(c); // can't create it until Log has been set up
MessageTracker.preRegister(c, keys);
try {
Main m = new Main("javac", pw);
Main.Result rc = m.compile(args.toArray(new String[args.size()]), c);
@ -534,6 +549,9 @@ class Example implements Comparable<Example> {
}
return rc.isOK();
} finally {
close(c.get(JavaFileManager.class));
}
}
static class MessageTracker extends JavacMessages {

View file

@ -35,6 +35,7 @@ import java.net.URI;
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 @@ public class SamConversionComboTest {
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 @@ public class SamConversionComboTest {
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,6 +267,7 @@ public class SamConversionComboTest {
}
public static void main(String[] args) throws Exception {
try {
for(Context ct : Context.values()) {
for (FInterface fi : FInterface.values()) {
for (LambdaKind lk: LambdaKind.values()) {
@ -276,5 +280,8 @@ public class SamConversionComboTest {
}
}
System.out.println("total tests: " + count);
} finally {
fm.close();
}
}
}

View file

@ -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.Diagnostic;
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 {
try {
new JavacParserTest().run(args);
} finally {
fm.close();
}
}
class MyFileObject extends SimpleJavaFileObject {
@ -103,7 +109,7 @@ public class JavacParserTest extends TestCase {
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 @@ public class JavacParserTest extends TestCase {
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 @@ public class JavacParserTest extends TestCase {
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 @@ public class JavacParserTest extends TestCase {
"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 @@ public class JavacParserTest extends TestCase {
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 @@ public class JavacParserTest extends TestCase {
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 @@ public class JavacParserTest extends TestCase {
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 @@ public class JavacParserTest extends TestCase {
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 @@ public class JavacParserTest extends TestCase {
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 @@ public class JavacParserTest extends TestCase {
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 @@ public class JavacParserTest extends TestCase {
"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 @@ public class JavacParserTest extends TestCase {
"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 @@ public class JavacParserTest extends TestCase {
"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 @@ public class JavacParserTest extends TestCase {
"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 @@ public class JavacParserTest extends TestCase {
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 @@ public class JavacParserTest extends TestCase {
+ " };\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 @@ public class JavacParserTest extends TestCase {
+ " }\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 @@ public class JavacParserTest extends TestCase {
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 @@ public class JavacParserTest extends TestCase {
"}";
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();

View file

@ -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.List;
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,7 +115,8 @@ public class TypeAnnotationsPretty {
code + "; }" +
postfix;
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
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()) {
@ -123,13 +125,15 @@ public class TypeAnnotationsPretty {
checkMatch(code, var);
}
}
}
private void runMethod(String code) throws IOException {
String src = prefix +
code + "}" +
postfix;
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(src)));
@ -139,6 +143,7 @@ public class TypeAnnotationsPretty {
checkMatch(code, meth);
}
}
}
void checkMatch(String code, JCTree tree) {
String expect = code.replace("\n", NL);