29
29
import org .openjdk .jmh .annotations .Scope ;
30
30
import org .openjdk .jmh .annotations .Setup ;
31
31
import org .openjdk .jmh .annotations .State ;
32
+ import org .openjdk .jmh .annotations .TearDown ;
32
33
33
34
import javax .tools .JavaCompiler ;
34
35
import javax .tools .SimpleJavaFileObject ;
35
36
import javax .tools .StandardJavaFileManager ;
37
+ import javax .tools .StandardLocation ;
36
38
import javax .tools .ToolProvider ;
39
+ import java .io .IOException ;
40
+ import java .io .File ;
37
41
import java .net .URI ;
42
+ import java .nio .file .Files ;
38
43
import java .util .ArrayList ;
44
+ import java .util .Collections ;
39
45
import java .util .List ;
40
46
import java .util .concurrent .TimeUnit ;
41
47
@@ -46,18 +52,35 @@ public class Javac {
46
52
private List <JavaSourceFromString > compilationUnits ;
47
53
private JavaCompiler compiler ;
48
54
private StandardJavaFileManager fileManager ;
55
+ private File classOutputDir ;
49
56
50
57
@ Setup
51
- public void prepare () {
58
+ public void prepare () throws IOException {
52
59
String helloWorld = "class Apan { \n " + " public static void main(String args[]) {\n "
53
60
+ " System.out.println(\" hej apa\" );\n " + " }\n " + "}\n " ;
54
61
55
62
compiler = ToolProvider .getSystemJavaCompiler ();
63
+
56
64
fileManager = compiler .getStandardFileManager (null , null , null );
65
+ classOutputDir = Files .createTempDirectory (Javac .class .getName ()).toFile ();
66
+ fileManager .setLocation (StandardLocation .CLASS_OUTPUT , Collections .singleton (classOutputDir ));
67
+
57
68
compilationUnits = new ArrayList <>();
58
69
compilationUnits .add (new JavaSourceFromString ("Apan" , helloWorld ));
59
70
}
60
71
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
+
61
84
@ Benchmark
62
85
public Boolean testCompile () throws Exception {
63
86
JavaCompiler .CompilationTask task = compiler .getTask (null , fileManager , null , null , null , compilationUnits );
0 commit comments