2929import org .openjdk .jmh .annotations .Scope ;
3030import org .openjdk .jmh .annotations .Setup ;
3131import org .openjdk .jmh .annotations .State ;
32+ import org .openjdk .jmh .annotations .TearDown ;
3233
3334import javax .tools .JavaCompiler ;
3435import javax .tools .SimpleJavaFileObject ;
3536import javax .tools .StandardJavaFileManager ;
37+ import javax .tools .StandardLocation ;
3638import javax .tools .ToolProvider ;
39+ import java .io .IOException ;
40+ import java .io .File ;
3741import java .net .URI ;
42+ import java .nio .file .Files ;
3843import java .util .ArrayList ;
44+ import java .util .Collections ;
3945import java .util .List ;
4046import 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