Skip to content

Commit bd51b7e

Browse files
committed
8296645: org.openjdk.bench.javax.tools.Javac leaves class files in current directory
Reviewed-by: redestad, jpai
1 parent b27a61e commit bd51b7e

File tree

1 file changed

+24
-1
lines changed
  • test/micro/org/openjdk/bench/javax/tools

1 file changed

+24
-1
lines changed

test/micro/org/openjdk/bench/javax/tools/Javac.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@
2929
import org.openjdk.jmh.annotations.Scope;
3030
import org.openjdk.jmh.annotations.Setup;
3131
import org.openjdk.jmh.annotations.State;
32+
import org.openjdk.jmh.annotations.TearDown;
3233

3334
import javax.tools.JavaCompiler;
3435
import javax.tools.SimpleJavaFileObject;
3536
import javax.tools.StandardJavaFileManager;
37+
import javax.tools.StandardLocation;
3638
import javax.tools.ToolProvider;
39+
import java.io.IOException;
40+
import java.io.File;
3741
import java.net.URI;
42+
import java.nio.file.Files;
3843
import java.util.ArrayList;
44+
import java.util.Collections;
3945
import java.util.List;
4046
import java.util.concurrent.TimeUnit;
4147

@@ -46,18 +52,35 @@ public class Javac {
4652
private List<JavaSourceFromString> compilationUnits;
4753
private JavaCompiler compiler;
4854
private StandardJavaFileManager fileManager;
55+
private File classOutputDir;
4956

5057
@Setup
51-
public void prepare() {
58+
public void prepare() throws IOException {
5259
String helloWorld = "class Apan { \n" + " public static void main(String args[]) {\n"
5360
+ " System.out.println(\"hej apa\");\n" + " }\n" + "}\n";
5461

5562
compiler = ToolProvider.getSystemJavaCompiler();
63+
5664
fileManager = compiler.getStandardFileManager(null, null, null);
65+
classOutputDir = Files.createTempDirectory(Javac.class.getName()).toFile();
66+
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(classOutputDir));
67+
5768
compilationUnits = new ArrayList<>();
5869
compilationUnits.add(new JavaSourceFromString("Apan", helloWorld));
5970
}
6071

72+
@TearDown
73+
public void tearDown() {
74+
for (File f : classOutputDir.listFiles()) {
75+
if (f.isFile()) {
76+
f.delete();
77+
} else {
78+
throw new IllegalStateException("Unexpected non-file: " + f);
79+
}
80+
}
81+
classOutputDir.delete();
82+
}
83+
6184
@Benchmark
6285
public Boolean testCompile() throws Exception {
6386
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);

0 commit comments

Comments
 (0)