2929import java .lang .reflect .InvocationTargetException ;
3030import java .lang .reflect .Method ;
3131import java .net .URI ;
32- import java .util .Iterator ;
33- import java .util .LinkedHashMap ;
34- import java .util .Map ;
35- import java .util .Set ;
32+ import java .util .*;
3633
3734class MyJavaFileManager implements JavaFileManager {
3835 private final static Unsafe unsafe ;
@@ -51,7 +48,9 @@ class MyJavaFileManager implements JavaFileManager {
5148 }
5249
5350 private final StandardJavaFileManager fileManager ;
54- private final Map <String , ByteArrayOutputStream > buffers = new LinkedHashMap <>();
51+
52+ // synchronizing due to ConcurrentModificationException
53+ private final Map <String , ByteArrayOutputStream > buffers = Collections .synchronizedMap (new LinkedHashMap <>());
5554
5655 MyJavaFileManager (StandardJavaFileManager fileManager ) {
5756 this .fileManager = fileManager ;
@@ -90,14 +89,23 @@ public boolean hasLocation(Location location) {
9089 }
9190
9291 public JavaFileObject getJavaFileForInput (Location location , String className , Kind kind ) throws IOException {
93- if (location == StandardLocation .CLASS_OUTPUT && buffers .containsKey (className ) && kind == Kind .CLASS ) {
94- final byte [] bytes = buffers .get (className ).toByteArray ();
95- return new SimpleJavaFileObject (URI .create (className ), kind ) {
96- @ NotNull
97- public InputStream openInputStream () {
98- return new ByteArrayInputStream (bytes );
99- }
100- };
92+
93+ if (location == StandardLocation .CLASS_OUTPUT ) {
94+ boolean success = false ;
95+ final byte [] bytes ;
96+ synchronized (buffers ) {
97+ success = buffers .containsKey (className ) && kind == Kind .CLASS ;
98+ bytes = buffers .get (className ).toByteArray ();
99+ }
100+ if (success ) {
101+
102+ return new SimpleJavaFileObject (URI .create (className ), kind ) {
103+ @ NotNull
104+ public InputStream openInputStream () {
105+ return new ByteArrayInputStream (bytes );
106+ }
107+ };
108+ }
101109 }
102110 return fileManager .getJavaFileForInput (location , className , kind );
103111 }
@@ -140,11 +148,13 @@ public void clearBuffers() {
140148
141149 @ NotNull
142150 public Map <String , byte []> getAllBuffers () {
143- Map <String , byte []> ret = new LinkedHashMap <>(buffers .size () * 2 );
144- for (Map .Entry <String , ByteArrayOutputStream > entry : buffers .entrySet ()) {
145- ret .put (entry .getKey (), entry .getValue ().toByteArray ());
151+ synchronized (buffers ) {
152+ Map <String , byte []> ret = new LinkedHashMap <>(buffers .size () * 2 );
153+ for (Map .Entry <String , ByteArrayOutputStream > entry : buffers .entrySet ()) {
154+ ret .put (entry .getKey (), entry .getValue ().toByteArray ());
155+ }
156+ return ret ;
146157 }
147- return ret ;
148158 }
149159
150160 @ SuppressWarnings ("unchecked" )
0 commit comments