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

@ -413,17 +413,17 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
int run(String[] args) { int run(String[] args) {
try { 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;
}
try { 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;
}
return run(); return run();
} finally { } finally {
if (defaultFileManager != null) { if (defaultFileManager != null) {

View file

@ -25,6 +25,7 @@
package com.sun.tools.sjavac.comp; package com.sun.tools.sjavac.comp;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.net.URI; import java.net.URI;
@ -76,90 +77,93 @@ public class SjavacImpl implements Sjavac {
Set<URI> sourcesToCompile, Set<URI> sourcesToCompile,
Set<URI> visibleSources) { Set<URI> visibleSources) {
JavacTool compiler = JavacTool.create(); JavacTool compiler = JavacTool.create();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
SmartFileManager smartFileManager = new SmartFileManager(fileManager); SmartFileManager smartFileManager = new SmartFileManager(fileManager);
Context context = new Context(); Context context = new Context();
// Now setup the actual compilation.... // Now setup the actual compilation....
CompilationResult compilationResult = new CompilationResult(0); CompilationResult compilationResult = new CompilationResult(0);
// First deal with explicit source files on cmdline and in at file. // First deal with explicit source files on cmdline and in at file.
ListBuffer<JavaFileObject> compilationUnits = new ListBuffer<>(); ListBuffer<JavaFileObject> compilationUnits = new ListBuffer<>();
for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(explicitSources)) { for (JavaFileObject i : fileManager.getJavaFileObjectsFromFiles(explicitSources)) {
compilationUnits.append(i); 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);
// 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) { // Now deal with sources supplied as source_to_compile.
stderrLog.append(Util.getStackTrace(e)); ListBuffer<File> sourcesToCompileFiles = new ListBuffer<>();
rc = com.sun.tools.javac.main.Main.Result.ERROR; for (URI u : sourcesToCompile) {
} sourcesToCompileFiles.append(new File(u));
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);
}
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());
} }
rc = com.sun.tools.javac.main.Main.Result.ERROR; 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);
// 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;
}
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);
}
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());
}
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 @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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -245,21 +245,22 @@ public abstract class JavacTemplateTestBase {
private File compile(List<File> classpaths, List<JavaFileObject> files, boolean generate) throws IOException { private File compile(List<File> classpaths, List<JavaFileObject> files, boolean generate) throws IOException {
JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler(); JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = systemJavaCompiler.getStandardFileManager(null, null, null); try (StandardJavaFileManager fm = systemJavaCompiler.getStandardFileManager(null, null, null)) {
if (classpaths.size() > 0) if (classpaths.size() > 0)
fm.setLocation(StandardLocation.CLASS_PATH, classpaths); fm.setLocation(StandardLocation.CLASS_PATH, classpaths);
JavacTask ct = (JavacTask) systemJavaCompiler.getTask(null, fm, diags, compileOptions, null, files); JavacTask ct = (JavacTask) systemJavaCompiler.getTask(null, fm, diags, compileOptions, null, files);
if (generate) { if (generate) {
File destDir = new File(root, Integer.toString(counter.incrementAndGet())); File destDir = new File(root, Integer.toString(counter.incrementAndGet()));
// @@@ Assert that this directory didn't exist, or start counter at max+1 // @@@ Assert that this directory didn't exist, or start counter at max+1
destDir.mkdirs(); destDir.mkdirs();
fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir)); fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
ct.generate(); ct.generate();
return destDir; return destDir;
} }
else { else {
ct.analyze(); ct.analyze();
return nullDir; return nullDir;
}
} }
} }

View file

@ -44,6 +44,7 @@ import javax.lang.model.element.ElementKind;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener; import javax.tools.DiagnosticListener;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject; import javax.tools.SimpleJavaFileObject;
@ -52,22 +53,26 @@ public class VerifyErroneousAnnotationsAttributed {
new VerifyErroneousAnnotationsAttributed().run(); new VerifyErroneousAnnotationsAttributed().run();
} }
void run() { void run() throws IOException {
int failCount = 0; try {
for (String ann : generateAnnotations()) { int failCount = 0;
String code = PATTERN.replace("PLACEHOLDER", ann); for (String ann : generateAnnotations()) {
try { String code = PATTERN.replace("PLACEHOLDER", ann);
validate(code); try {
} catch (Throwable t) { validate(code);
System.out.println("Failed for: "); } catch (Throwable t) {
System.out.println(code); System.out.println("Failed for: ");
t.printStackTrace(System.out); System.out.println(code);
failCount++; t.printStackTrace(System.out);
failCount++;
}
} }
}
if (failCount > 0) { if (failCount > 0) {
throw new IllegalStateException("failed sub-tests: " + failCount); throw new IllegalStateException("failed sub-tests: " + failCount);
}
} finally {
fm.close();
} }
} }
@ -221,13 +226,14 @@ public class VerifyErroneousAnnotationsAttributed {
} }
final JavacTool tool = JavacTool.create(); final JavacTool tool = JavacTool.create();
final JavaFileManager fm = tool.getStandardFileManager(null, null, null);
final DiagnosticListener<JavaFileObject> devNull = new DiagnosticListener<JavaFileObject>() { final DiagnosticListener<JavaFileObject> devNull = new DiagnosticListener<JavaFileObject>() {
@Override public void report(Diagnostic<? extends JavaFileObject> diagnostic) {} @Override public void report(Diagnostic<? extends JavaFileObject> diagnostic) {}
}; };
void validate(String code) throws IOException, URISyntaxException { void validate(String code) throws IOException, URISyntaxException {
JavacTask task = tool.getTask(null, JavacTask task = tool.getTask(null,
null, fm,
devNull, devNull,
Arrays.asList("-XDshouldStopPolicy=FLOW"), Arrays.asList("-XDshouldStopPolicy=FLOW"),
null, 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 java.util.Set;
import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment; 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.Element;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.DeclaredType; import javax.lang.model.type.DeclaredType;
import javax.tools.JavaCompiler; import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject; import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider; import javax.tools.ToolProvider;
import com.sun.source.util.JavacTask; import com.sun.source.util.JavacTask;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
public class TestContainTypes { public class TestContainTypes {
@ -127,22 +128,28 @@ 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 { public static void main(String... args) throws Exception {
for (ClassType ctA : ClassType.values()) { try {
for (ParameterType ptA : ParameterType.values()) { for (ClassType ctA : ClassType.values()) {
for (ClassType ctB : ClassType.values()) { for (ParameterType ptA : ParameterType.values()) {
for (ParameterType ptB : ParameterType.values()) { for (ClassType ctB : ClassType.values()) {
compileAndCheck(ptA, ctA, ptB, ctB); 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 { static void compileAndCheck(ParameterType ptA, ClassType ctA, ParameterType ptB, ClassType ctB) throws Exception {
JavaSource source = new JavaSource(ptA.instantiate(ctA), ptB.instantiate(ctB)); JavaSource source = new JavaSource(ptA.instantiate(ctA), ptB.instantiate(ctB));
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); JavacTask ct = (JavacTask)tool.getTask(null, fm, null,
JavacTask ct = (JavacTask)tool.getTask(null, null, null,
null, null, Arrays.asList(source)); null, null, Arrays.asList(source));
ct.setProcessors(Arrays.asList(new ContainTypesTester(ParameterType.contains(ptA, ctA, ptB, ctB), source))); ct.setProcessors(Arrays.asList(new ContainTypesTester(ParameterType.contains(ptA, ctA, ptB, ctB), source)));
System.err.println("A = " + ptA +" / " + ptA.instantiate(ctA)); System.err.println("A = " + ptA +" / " + ptA.instantiate(ctA));

View file

@ -100,14 +100,18 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
JavacTool tool = JavacTool.create(); JavacTool tool = JavacTool.create();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
if (fmOpts != null) try {
fm = new FileManager(fm, fmOpts); 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(); Context c = initContext();
JavacTaskImpl t = (JavacTaskImpl) tool.getTask(out, fm, null, opts, null, fos, c); JavacTaskImpl t = (JavacTaskImpl) tool.getTask(out, fm, null, opts, null, fos, c);
return t.call(); return t.call();
} finally {
close(fm);
}
} }
} }
@ -136,9 +140,14 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
Main main = new Main("javac", out); Main main = new Main("javac", out);
Context c = initContext(); Context c = initContext();
JavacFileManager.preRegister(c); // can't create it until Log has been set up 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);
return result.isOK(); 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(); Context c = initContext();
JavacFileManager.preRegister(c); // can't create it until Log has been set up 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);
return result.isOK(); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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.DiagnosticCollector;
import javax.tools.JavaCompiler; import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask; import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager; import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider; import javax.tools.ToolProvider;
@ -311,24 +312,24 @@ class Example implements Comparable<Example> {
static class DefaultFactory implements Factory { static class DefaultFactory implements Factory {
public Compiler getCompiler(List<String> opts, boolean verbose) { public Compiler getCompiler(List<String> opts, boolean verbose) {
String first; String first;
String[] rest; String[] rest;
if (opts == null || opts.isEmpty()) { if (opts == null || opts.isEmpty()) {
first = null; first = null;
rest = new String[0]; rest = new String[0];
} else { } else {
first = opts.get(0); first = opts.get(0);
rest = opts.subList(1, opts.size()).toArray(new String[opts.size() - 1]); 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; static Factory factory;
@ -351,6 +352,14 @@ class Example implements Comparable<Example> {
loader = cl; loader = cl;
} }
protected void close(JavaFileManager fm) {
try {
fm.close();
} catch (IOException e) {
throw new Error(e);
}
}
protected ClassLoader loader; protected ClassLoader loader;
protected boolean verbose; protected boolean verbose;
} }
@ -399,21 +408,25 @@ class Example implements Comparable<Example> {
JavaCompiler c = ToolProvider.getSystemJavaCompiler(); JavaCompiler c = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = c.getStandardFileManager(dc, null, null); StandardJavaFileManager fm = c.getStandardFileManager(dc, null, null);
if (fmOpts != null) try {
fm = new FileManager(fm, fmOpts); 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); CompilationTask t = c.getTask(out, fm, dc, opts, null, fos);
Boolean ok = t.call(); Boolean ok = t.call();
if (keys != null) { if (keys != null) {
for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) { for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) {
scanForKeys(unwrap(d), keys); scanForKeys(unwrap(d), keys);
}
} }
}
return ok; return ok;
} finally {
close(fm);
}
} }
/** /**
@ -526,14 +539,19 @@ class Example implements Comparable<Example> {
Context c = new Context(); Context c = new Context();
JavacFileManager.preRegister(c); // can't create it until Log has been set up JavacFileManager.preRegister(c); // can't create it until Log has been set up
MessageTracker.preRegister(c, keys); MessageTracker.preRegister(c, keys);
Main m = new Main("javac", pw);
Main.Result rc = m.compile(args.toArray(new String[args.size()]), c);
if (keys != null) { try {
pw.close(); Main m = new Main("javac", pw);
Main.Result rc = m.compile(args.toArray(new String[args.size()]), c);
if (keys != null) {
pw.close();
}
return rc.isOK();
} finally {
close(c.get(JavaFileManager.class));
} }
return rc.isOK();
} }
static class MessageTracker extends JavacMessages { static class MessageTracker extends JavacMessages {

View file

@ -35,6 +35,7 @@ import java.net.URI;
import java.util.Arrays; import java.util.Arrays;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import javax.tools.JavaCompiler; import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject; import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider; import javax.tools.ToolProvider;
@ -206,9 +207,8 @@ public class SamConversionComboTest {
String clientFileStr = clientSourceFile.toString(); String clientFileStr = clientSourceFile.toString();
System.out.println(clientFileStr.substring(0, clientFileStr.indexOf("\n\n"))); System.out.println(clientFileStr.substring(0, clientFileStr.indexOf("\n\n")));
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
DiagnosticChecker dc = new DiagnosticChecker(); 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 { try {
ct.analyze(); ct.analyze();
} catch (Exception e) { } catch (Exception e) {
@ -255,6 +255,9 @@ public class SamConversionComboTest {
ReturnValue returnValue; ReturnValue returnValue;
static int count = 0; 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) { SamConversionComboTest(FInterface f, Context c, LambdaBody lb, LambdaKind lk, ReturnValue rv) {
fInterface = f; fInterface = f;
context = c; context = c;
@ -264,17 +267,21 @@ public class SamConversionComboTest {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
for(Context ct : Context.values()) { try {
for (FInterface fi : FInterface.values()) { for(Context ct : Context.values()) {
for (LambdaKind lk: LambdaKind.values()) { for (FInterface fi : FInterface.values()) {
for (LambdaBody lb : LambdaBody.values()) { for (LambdaKind lk: LambdaKind.values()) {
for(ReturnValue rv : ReturnValue.values()) { for (LambdaBody lb : LambdaBody.values()) {
new SamConversionComboTest(fi, ct, lb, lk, rv).test(); for(ReturnValue rv : ReturnValue.values()) {
new SamConversionComboTest(fi, ct, lb, lk, rv).test();
}
} }
} }
} }
} }
}
System.out.println("total tests: " + count); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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.DiagnosticCollector;
import javax.tools.DiagnosticListener; import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler; import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject; import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider; import javax.tools.ToolProvider;
public class JavacParserTest extends TestCase { public class JavacParserTest extends TestCase {
static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
static final JavaFileManager fm = tool.getStandardFileManager(null, null, null);
private JavacParserTest(){} private JavacParserTest(){}
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
new JavacParserTest().run(args); try {
new JavacParserTest().run(args);
} finally {
fm.close();
}
} }
class MyFileObject extends SimpleJavaFileObject { class MyFileObject extends SimpleJavaFileObject {
@ -103,7 +109,7 @@ public class JavacParserTest extends TestCase {
CompilationUnitTree getCompilationUnitTree(String code) throws IOException { 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))); null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next(); CompilationUnitTree cut = ct.parse().iterator().next();
return cut; return cut;
@ -129,7 +135,7 @@ public class JavacParserTest extends TestCase {
String code = "package test; public class Test {public Test() {super();}}"; 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))); null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next(); CompilationUnitTree cut = ct.parse().iterator().next();
SourcePositions pos = Trees.instance(ct).getSourcePositions(); SourcePositions pos = Trees.instance(ct).getSourcePositions();
@ -168,7 +174,7 @@ public class JavacParserTest extends TestCase {
final String theString = "public"; final String theString = "public";
String code = "package test; " + theString + " enum Test {A;}"; 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))); null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next(); CompilationUnitTree cut = ct.parse().iterator().next();
SourcePositions pos = Trees.instance(ct).getSourcePositions(); SourcePositions pos = Trees.instance(ct).getSourcePositions();
@ -191,7 +197,7 @@ public class JavacParserTest extends TestCase {
"class d {} private void method() { " + "class d {} private void method() { " +
"Object o = " + theString + "; } }"; "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))); null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next(); CompilationUnitTree cut = ct.parse().iterator().next();
SourcePositions pos = Trees.instance(ct).getSourcePositions(); SourcePositions pos = Trees.instance(ct).getSourcePositions();
@ -238,7 +244,7 @@ public class JavacParserTest extends TestCase {
final List<Diagnostic<? extends JavaFileObject>> errors = final List<Diagnostic<? extends JavaFileObject>> errors =
new LinkedList<Diagnostic<? extends JavaFileObject>>(); new LinkedList<Diagnostic<? extends JavaFileObject>>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm,
new DiagnosticListener<JavaFileObject>() { new DiagnosticListener<JavaFileObject>() {
public void report(Diagnostic<? extends JavaFileObject> diagnostic) { public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
errors.add(diagnostic); errors.add(diagnostic);
@ -261,7 +267,7 @@ public class JavacParserTest extends TestCase {
String code = "\n@interface Test {}"; 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))); null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next(); CompilationUnitTree cut = ct.parse().iterator().next();
@ -300,7 +306,7 @@ public class JavacParserTest extends TestCase {
final List<Diagnostic<? extends JavaFileObject>> errors = final List<Diagnostic<? extends JavaFileObject>> errors =
new LinkedList<Diagnostic<? extends JavaFileObject>>(); new LinkedList<Diagnostic<? extends JavaFileObject>>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm,
new DiagnosticListener<JavaFileObject>() { new DiagnosticListener<JavaFileObject>() {
public void report(Diagnostic<? extends JavaFileObject> diagnostic) { public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
@ -443,7 +449,7 @@ public class JavacParserTest extends TestCase {
final List<Diagnostic<? extends JavaFileObject>> errors = final List<Diagnostic<? extends JavaFileObject>> errors =
new LinkedList<Diagnostic<? extends JavaFileObject>>(); new LinkedList<Diagnostic<? extends JavaFileObject>>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm,
new DiagnosticListener<JavaFileObject>() { new DiagnosticListener<JavaFileObject>() {
public void report(Diagnostic<? extends JavaFileObject> diagnostic) { public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
errors.add(diagnostic); errors.add(diagnostic);
@ -482,7 +488,7 @@ public class JavacParserTest extends TestCase {
String code = "package t; class Test { <T> void t() {} }"; 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))); null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next(); CompilationUnitTree cut = ct.parse().iterator().next();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
@ -505,7 +511,7 @@ public class JavacParserTest extends TestCase {
DiagnosticCollector<JavaFileObject> coll = DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>(); 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))); null, Arrays.asList(new MyFileObject(code)));
ct.parse(); ct.parse();
@ -529,7 +535,7 @@ public class JavacParserTest extends TestCase {
"if (name != null) class X {} } }"; "if (name != null) class X {} } }";
DiagnosticCollector<JavaFileObject> coll = DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>(); 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))); null, Arrays.asList(new MyFileObject(code)));
ct.parse(); ct.parse();
@ -552,7 +558,7 @@ public class JavacParserTest extends TestCase {
"if (true) abstract class F {} }}"; "if (true) abstract class F {} }}";
DiagnosticCollector<JavaFileObject> coll = DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>(); 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))); null, Arrays.asList(new MyFileObject(code)));
ct.parse(); ct.parse();
@ -575,7 +581,7 @@ public class JavacParserTest extends TestCase {
"if (name != null) interface X {} } }"; "if (name != null) interface X {} } }";
DiagnosticCollector<JavaFileObject> coll = DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>(); 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))); null, Arrays.asList(new MyFileObject(code)));
ct.parse(); ct.parse();
@ -598,7 +604,7 @@ public class JavacParserTest extends TestCase {
"if (true) } }"; "if (true) } }";
DiagnosticCollector<JavaFileObject> coll = DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>(); 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))); null, Arrays.asList(new MyFileObject(code)));
ct.parse(); ct.parse();
@ -620,7 +626,7 @@ public class JavacParserTest extends TestCase {
String code = "\nclass Test { { System.err.println(0e); } }"; 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))); null, Arrays.asList(new MyFileObject(code)));
assertNotNull(ct.parse().iterator().next()); assertNotNull(ct.parse().iterator().next());
@ -820,7 +826,7 @@ public class JavacParserTest extends TestCase {
+ " };\n" + " };\n"
+ " }\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))); null, null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next(); CompilationUnitTree cut = ct.parse().iterator().next();
@ -861,7 +867,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))); null, null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next(); CompilationUnitTree cut = ct.parse().iterator().next();
@ -886,7 +892,7 @@ public class JavacParserTest extends TestCase {
String code = "package t; enum Test { AAA; }"; 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))); null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next(); CompilationUnitTree cut = ct.parse().iterator().next();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
@ -905,7 +911,7 @@ public class JavacParserTest extends TestCase {
"}"; "}";
DiagnosticCollector<JavaFileObject> coll = DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<>(); 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))); null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next(); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 java.util.LinkedList;
import javax.tools.JavaCompiler; import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject; import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider; import javax.tools.ToolProvider;
@ -114,13 +115,15 @@ public class TypeAnnotationsPretty {
code + "; }" + code + "; }" +
postfix; postfix;
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
null, Arrays.asList(new MyFileObject(src))); JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(src)));
for (CompilationUnitTree cut : ct.parse()) { for (CompilationUnitTree cut : ct.parse()) {
JCTree.JCVariableDecl var = JCTree.JCVariableDecl var =
(JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
checkMatch(code, var); checkMatch(code, var);
}
} }
} }
@ -129,14 +132,16 @@ public class TypeAnnotationsPretty {
code + "}" + code + "}" +
postfix; postfix;
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
null, Arrays.asList(new MyFileObject(src))); JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(src)));
for (CompilationUnitTree cut : ct.parse()) { for (CompilationUnitTree cut : ct.parse()) {
JCTree.JCMethodDecl meth = JCTree.JCMethodDecl meth =
(JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); (JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
checkMatch(code, meth); checkMatch(code, meth);
}
} }
} }