69 |
69 |
70 private final Map<String, OutputMemoryJavaFileObject> classObjects = new TreeMap<>(); |
70 private final Map<String, OutputMemoryJavaFileObject> classObjects = new TreeMap<>(); |
71 |
71 |
72 private ClassFileCreationListener classListener = null; |
72 private ClassFileCreationListener classListener = null; |
73 |
73 |
74 private final ClassLoader loader = new REPLClassLoader(); |
|
75 |
|
76 private final JShell proc; |
74 private final JShell proc; |
77 |
75 |
78 // Upcoming Jigsaw |
76 // Upcoming Jigsaw |
79 private Method inferModuleNameMethod = null; |
77 private Method inferModuleNameMethod = null; |
80 private Method listModuleLocationsMethod = null; |
78 private Method listModuleLocationsMethod = null; |
166 public InputStream openInputStream() throws IOException { |
164 public InputStream openInputStream() throws IOException { |
167 return new ByteArrayInputStream(getBytes()); |
165 return new ByteArrayInputStream(getBytes()); |
168 } |
166 } |
169 } |
167 } |
170 |
168 |
171 // For restoring process-local execution support |
|
172 class REPLClassLoader extends ClassLoader { |
|
173 |
|
174 @Override |
|
175 protected Class<?> findClass(String name) throws ClassNotFoundException { |
|
176 OutputMemoryJavaFileObject fo = classObjects.get(name); |
|
177 proc.debug(DBG_FMGR, "findClass %s = %s\n", name, fo); |
|
178 if (fo == null) { |
|
179 throw new ClassNotFoundException("Not ours"); |
|
180 } |
|
181 byte[] b = fo.getBytes(); |
|
182 return super.defineClass(name, b, 0, b.length, null); |
|
183 } |
|
184 } |
|
185 |
|
186 public MemoryFileManager(StandardJavaFileManager standardManager, JShell proc) { |
169 public MemoryFileManager(StandardJavaFileManager standardManager, JShell proc) { |
187 this.stdFileManager = standardManager; |
170 this.stdFileManager = standardManager; |
188 this.proc = proc; |
171 this.proc = proc; |
189 } |
172 } |
190 |
173 |
195 // For debugging dumps |
178 // For debugging dumps |
196 public void dumpClasses() { |
179 public void dumpClasses() { |
197 for (OutputMemoryJavaFileObject co : generatedClasses()) { |
180 for (OutputMemoryJavaFileObject co : generatedClasses()) { |
198 co.dump(); |
181 co.dump(); |
199 } |
182 } |
200 } |
|
201 |
|
202 // For restoring process-local execution support |
|
203 public Class<?> findGeneratedClass(String genClassFullName) throws ClassNotFoundException { |
|
204 for (OutputMemoryJavaFileObject co : generatedClasses()) { |
|
205 if (co.className.equals(genClassFullName)) { |
|
206 Class<?> klass = loadClass(co.className); |
|
207 proc.debug(DBG_FMGR, "Loaded %s\n", klass); |
|
208 return klass; |
|
209 } |
|
210 } |
|
211 return null; |
|
212 } |
|
213 |
|
214 // For restoring process-local execution support |
|
215 public byte[] findGeneratedBytes(String genClassFullName) throws ClassNotFoundException { |
|
216 for (OutputMemoryJavaFileObject co : generatedClasses()) { |
|
217 if (co.className.equals(genClassFullName)) { |
|
218 return co.getBytes(); |
|
219 } |
|
220 } |
|
221 return null; |
|
222 } |
|
223 |
|
224 // For restoring process-local execution support |
|
225 public Class<?> loadClass(String name) throws ClassNotFoundException { |
|
226 return getClassLoader(null).loadClass(name); |
|
227 } |
183 } |
228 |
184 |
229 public JavaFileObject createSourceFileObject(Object origin, String name, String code) { |
185 public JavaFileObject createSourceFileObject(Object origin, String name, String code) { |
230 return new SourceMemoryJavaFileObject(origin, name, code); |
186 return new SourceMemoryJavaFileObject(origin, name, code); |
231 } |
187 } |
286 * and this file manager cannot be reopened |
242 * and this file manager cannot be reopened |
287 */ |
243 */ |
288 @Override @DefinedBy(Api.COMPILER) |
244 @Override @DefinedBy(Api.COMPILER) |
289 public ClassLoader getClassLoader(JavaFileManager.Location location) { |
245 public ClassLoader getClassLoader(JavaFileManager.Location location) { |
290 proc.debug(DBG_FMGR, "getClassLoader: location\n", location); |
246 proc.debug(DBG_FMGR, "getClassLoader: location\n", location); |
291 return loader; |
247 return stdFileManager.getClassLoader(location); |
292 } |
248 } |
293 |
249 |
294 /** |
250 /** |
295 * Lists all file objects matching the given criteria in the given |
251 * Lists all file objects matching the given criteria in the given |
296 * location. List file objects in "subpackages" if recurse is |
252 * location. List file objects in "subpackages" if recurse is |