77package com .example ;
88
99import java .io .IOException ;
10- import java .io .InputStream ;
1110import java .nio .file .Files ;
1211import java .nio .file .Path ;
1312import java .util .ArrayList ;
1413import java .util .List ;
15- import java .util .Map ;
14+ import java .util .stream . Stream ;
1615
1716import javax .tools .Diagnostic ;
1817import javax .tools .DiagnosticListener ;
18+ import javax .tools .JavaFileManager ;
1919import javax .tools .JavaFileObject ;
2020import javax .tools .StandardLocation ;
2121
2222import com .example .preload .PreLoadedCompiler ;
2323import com .example .preload .PreLoadedFiles ;
2424
2525public 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 ;
3727
38- public static JavaFileManagerImpl getFm () {
28+ public static JavaFileManager getFm () {
3929 if (fileManagerInstance == null ) {
4030 try {
4131 fileManagerInstance = PreLoadedFiles .initFileSystem ();
@@ -47,9 +37,26 @@ public static JavaFileManagerImpl getFm() {
4737 return fileManagerInstance ;
4838 }
4939
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+
5056 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 );
5360 List <JavaFileObject > inputs = new ArrayList <>();
5461
5562 for (FileContent content : files ) {
@@ -60,19 +67,18 @@ public static Result compileFiles(List<String> options, FileContent[] files) thr
6067 Path fileNamePath = sourceFile .getFileName ();
6168 assert fileNamePath != null : "Could not get filename for source file path" ;
6269 String fileName = fileNamePath .toString ();
63- String className = JavaFileManagerImpl .removeExtension (fileName );
70+ String className = PackageNamingUtil .removeExtension (fileName );
6471
6572 Path parent = sourceFile .getParent ();
6673 if (parent != null ) {
6774 Files .createDirectories (parent );
6875 }
6976
7077 Files .write (sourceFile , source );
78+ System .err .println ("Added file at " + sourceFile );
7179 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 ;
7381 inputs .add (fileObject );
74-
75- System .err .println ("Added file at " + sourceFile );
7682 }
7783
7884 System .err .println ("Compiling with options: " + options );
@@ -84,15 +90,17 @@ public static Result compileFiles(List<String> options, FileContent[] files) thr
8490 return Result .failure (collector .diagnostics );
8591 }
8692
87- List <FileContent > outputFile = new ArrayList <>(fm . generatedClasses . size () );
93+ List <FileContent > outputFiles = new ArrayList <>();
8894
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+ }
92100 }
93101 }
94102
95- return Result .success (outputFile , collector .diagnostics );
103+ return Result .success (outputFiles , collector .diagnostics );
96104 }
97105
98106 public record FileContent (String name , byte [] content ) {
0 commit comments