2626import javax .tools .Diagnostic ;
2727import javax .tools .DiagnosticListener ;
2828import javax .tools .JavaFileObject ;
29+ import javax .tools .StandardJavaFileManager ;
30+ import java .io .Closeable ;
2931import java .io .File ;
3032import java .io .IOException ;
3133import java .io .PrintWriter ;
32- import java .util .Collections ;
33- import java .util .HashMap ;
34- import java .util .LinkedHashMap ;
35- import java .util .Map ;
36- import java .util .WeakHashMap ;
34+ import java .util .*;
3735
38- import static net .openhft .compiler .CompilerUtils .writeBytes ;
39- import static net .openhft .compiler .CompilerUtils .writeText ;
36+ import static net .openhft .compiler .CompilerUtils .*;
4037
4138@ SuppressWarnings ("StaticNonFinalField" )
42- public class CachedCompiler {
39+ public class CachedCompiler implements Closeable {
4340 private static final Logger LOG = LoggerFactory .getLogger (CachedCompiler .class );
44- private static final Map <ClassLoader , Map <String , Class >> loadedClassesMap = new WeakHashMap <ClassLoader , Map <String , Class >>();
4541 private static final PrintWriter DEFAULT_WRITER = new PrintWriter (System .err );
4642
43+ private final Map <ClassLoader , Map <String , Class >> loadedClassesMap = Collections .synchronizedMap (new WeakHashMap <ClassLoader , Map <String , Class >>());
44+ private final Map <ClassLoader , MyJavaFileManager > fileManagerMap = Collections .synchronizedMap (new WeakHashMap <ClassLoader , MyJavaFileManager >());
45+
4746 @ Nullable
4847 private final File sourceDir ;
4948 @ Nullable
@@ -57,9 +56,11 @@ public CachedCompiler(@Nullable File sourceDir, @Nullable File classDir) {
5756 this .classDir = classDir ;
5857 }
5958
60- public static void close () {
59+ public void close () {
6160 try {
62- CompilerUtils .s_fileManager .close ();
61+ for (MyJavaFileManager fileManager : fileManagerMap .values ()) {
62+ fileManager .close ();
63+ }
6364 } catch (IOException e ) {
6465 throw new AssertionError (e );
6566 }
@@ -76,35 +77,36 @@ public Class loadFromJava(@NotNull ClassLoader classLoader,
7677 }
7778
7879 @ NotNull
79- Map <String , byte []> compileFromJava (@ NotNull String className , @ NotNull String javaCode ) {
80- return compileFromJava (className , javaCode , DEFAULT_WRITER );
80+ Map <String , byte []> compileFromJava (@ NotNull String className , @ NotNull String javaCode , MyJavaFileManager fileManager ) {
81+ return compileFromJava (className , javaCode , DEFAULT_WRITER , fileManager );
8182 }
8283
8384 @ NotNull
8485 Map <String , byte []> compileFromJava (@ NotNull String className ,
8586 @ NotNull String javaCode ,
86- final @ NotNull PrintWriter writer ) {
87+ final @ NotNull PrintWriter writer ,
88+ MyJavaFileManager fileManager ) {
8789 Iterable <? extends JavaFileObject > compilationUnits ;
8890 if (sourceDir != null ) {
8991 String filename = className .replaceAll ("\\ ." , '\\' + File .separator ) + ".java" ;
9092 File file = new File (sourceDir , filename );
9193 writeText (file , javaCode );
92- compilationUnits = CompilerUtils . s_standardJavaFileManager .getJavaFileObjects (file );
94+ compilationUnits = s_standardJavaFileManager .getJavaFileObjects (file );
9395
9496 } else {
9597 javaFileObjects .put (className , new JavaSourceFromString (className , javaCode ));
9698 compilationUnits = javaFileObjects .values ();
9799 }
98100 // reuse the same file manager to allow caching of jar files
99- boolean ok = CompilerUtils . s_compiler .getTask (writer , CompilerUtils . s_fileManager , new DiagnosticListener <JavaFileObject >() {
101+ boolean ok = s_compiler .getTask (writer , fileManager , new DiagnosticListener <JavaFileObject >() {
100102 @ Override
101103 public void report (Diagnostic <? extends JavaFileObject > diagnostic ) {
102104 if (diagnostic .getKind () == Diagnostic .Kind .ERROR ) {
103105 writer .println (diagnostic );
104106 }
105107 }
106108 }, null , null , compilationUnits ).call ();
107- Map <String , byte []> result = CompilerUtils . s_fileManager .getAllBuffers ();
109+ Map <String , byte []> result = fileManager .getAllBuffers ();
108110 if (!ok ) {
109111 // compilation error, so we want to exclude this file from future compilation passes
110112 if (sourceDir == null )
@@ -132,7 +134,13 @@ public Class loadFromJava(@NotNull ClassLoader classLoader,
132134 PrintWriter printWriter = (writer == null ? DEFAULT_WRITER : writer );
133135 if (clazz != null )
134136 return clazz ;
135- for (Map .Entry <String , byte []> entry : compileFromJava (className , javaCode , printWriter ).entrySet ()) {
137+
138+ MyJavaFileManager fileManager = fileManagerMap .get (classLoader );
139+ if (fileManager == null ) {
140+ StandardJavaFileManager standardJavaFileManager = s_compiler .getStandardFileManager (null , null , null );
141+ fileManagerMap .put (classLoader , fileManager = new MyJavaFileManager (standardJavaFileManager ));
142+ }
143+ for (Map .Entry <String , byte []> entry : compileFromJava (className , javaCode , printWriter , fileManager ).entrySet ()) {
136144 String className2 = entry .getKey ();
137145 synchronized (loadedClassesMap ) {
138146 if (loadedClasses .containsKey (className2 ))
0 commit comments