7
7
package com .example ;
8
8
9
9
import java .io .IOException ;
10
- import java .io .InputStream ;
11
10
import java .nio .file .Files ;
12
11
import java .nio .file .Path ;
13
12
import java .util .ArrayList ;
14
13
import java .util .List ;
15
- import java .util .Map ;
14
+ import java .util .stream . Stream ;
16
15
17
16
import javax .tools .Diagnostic ;
18
17
import javax .tools .DiagnosticListener ;
18
+ import javax .tools .JavaFileManager ;
19
19
import javax .tools .JavaFileObject ;
20
20
import javax .tools .StandardLocation ;
21
21
22
22
import com .example .preload .PreLoadedCompiler ;
23
23
import com .example .preload .PreLoadedFiles ;
24
24
25
25
public class JavacCompilerWrapper {
26
- private static JavaFileManagerImpl fileManagerInstance = null ;
27
-
28
- /**
29
- * Initializes the filesystem.
30
- * <p>
31
- * This is usually done lazily, call this method if you want to control when this happens.
32
- */
33
- public static void init () {
34
- System .setProperty ("java.home" , PreLoadedFiles .JAVA_HOME );
35
- getFm ();
36
- }
26
+ private static JavaFileManager fileManagerInstance = null ;
37
27
38
- public static JavaFileManagerImpl getFm () {
28
+ public static JavaFileManager getFm () {
39
29
if (fileManagerInstance == null ) {
40
30
try {
41
31
fileManagerInstance = PreLoadedFiles .initFileSystem ();
@@ -47,9 +37,26 @@ public static JavaFileManagerImpl getFm() {
47
37
return fileManagerInstance ;
48
38
}
49
39
40
+ static void deleteDirectory (Path directoryToBeDeleted ) throws IOException {
41
+ try (Stream <Path > paths = Files .list (directoryToBeDeleted )) {
42
+ paths .forEach (f -> {
43
+ try {
44
+ if (Files .isDirectory (f )) {
45
+ deleteDirectory (directoryToBeDeleted );
46
+ }
47
+
48
+ Files .delete (f );
49
+ } catch (IOException e ) {
50
+ throw new RuntimeException (e );
51
+ }
52
+ });
53
+ }
54
+ }
55
+
50
56
public static Result compileFiles (List <String > options , FileContent [] files ) throws IOException {
51
- JavaFileManagerImpl fm = getFm ();
52
- fm .generatedClasses .clear ();
57
+ JavaFileManager fm = getFm ();
58
+ Path outputPath = Path .of (PreLoadedFiles .OUTPUT_PATH );
59
+ deleteDirectory (outputPath );
53
60
List <JavaFileObject > inputs = new ArrayList <>();
54
61
55
62
for (FileContent content : files ) {
@@ -60,19 +67,18 @@ public static Result compileFiles(List<String> options, FileContent[] files) thr
60
67
Path fileNamePath = sourceFile .getFileName ();
61
68
assert fileNamePath != null : "Could not get filename for source file path" ;
62
69
String fileName = fileNamePath .toString ();
63
- String className = JavaFileManagerImpl .removeExtension (fileName );
70
+ String className = PackageNamingUtil .removeExtension (fileName );
64
71
65
72
Path parent = sourceFile .getParent ();
66
73
if (parent != null ) {
67
74
Files .createDirectories (parent );
68
75
}
69
76
70
77
Files .write (sourceFile , source );
78
+ System .err .println ("Added file at " + sourceFile );
71
79
JavaFileObject fileObject = fm .getJavaFileForInput (StandardLocation .SOURCE_PATH , className , JavaFileObject .Kind .SOURCE );
72
- assert fileObject != null : "JavaFileManager.getJavaFileForInput returned null" ;
80
+ assert fileObject != null : "JavaFileManager.getJavaFileForInput returned null for class name " + className ;
73
81
inputs .add (fileObject );
74
-
75
- System .err .println ("Added file at " + sourceFile );
76
82
}
77
83
78
84
System .err .println ("Compiling with options: " + options );
@@ -84,15 +90,17 @@ public static Result compileFiles(List<String> options, FileContent[] files) thr
84
90
return Result .failure (collector .diagnostics );
85
91
}
86
92
87
- List <FileContent > outputFile = new ArrayList <>(fm . generatedClasses . size () );
93
+ List <FileContent > outputFiles = new ArrayList <>();
88
94
89
- for (Map .Entry <String , JavaFileObject > entry : fm .generatedClasses .entrySet ()) {
90
- try (InputStream os = entry .getValue ().openInputStream ()) {
91
- outputFile .add (new FileContent (entry .getKey () + entry .getValue ().getKind ().extension , os .readAllBytes ()));
95
+ try (Stream <Path > s = Files .walk (outputPath )) {
96
+ for (Path f : s .toList ()) {
97
+ if (Files .isRegularFile (f )) {
98
+ outputFiles .add (new FileContent (outputPath .relativize (f ).toString (), Files .readAllBytes (f )));
99
+ }
92
100
}
93
101
}
94
102
95
- return Result .success (outputFile , collector .diagnostics );
103
+ return Result .success (outputFiles , collector .diagnostics );
96
104
}
97
105
98
106
public record FileContent (String name , byte [] content ) {
0 commit comments