11package com .github .pfmiles .dropincc .impl .hotcompile ;
22
33import java .io .IOException ;
4+ import java .util .ArrayList ;
5+ import java .util .List ;
46import java .util .Map ;
7+ import java .util .Set ;
58
69import javax .tools .FileObject ;
710import javax .tools .ForwardingJavaFileManager ;
811import javax .tools .JavaFileObject ;
912import javax .tools .JavaFileObject .Kind ;
1013import javax .tools .StandardJavaFileManager ;
14+ import javax .tools .StandardLocation ;
1115
1216/**
1317 * A forwarding file manager which is used to decouple the file system
1418 * dependency. That is, the compilation process write compiled class files into
1519 * memory instead of file system.
1620 *
21+ * <p>
22+ * These following methods would be invoked during the compilation progress, so
23+ * they should behave well for sure:
24+ * </p>
25+ *
26+ * <pre>
27+ * close
28+ * flush
29+ * getClassLoader
30+ * getJavaFileForOutput
31+ * handleOption
32+ * hasLocation
33+ * inferBinaryName
34+ * list
35+ * </pre>
36+ *
1737 * @author pf-miles
1838 *
1939 */
2040public class MemClsFileManager extends ForwardingJavaFileManager <StandardJavaFileManager > {
2141
2242 private Map <String , JavaMemCls > destFiles ;
43+ private Map <String , JavaStringSource > srcFiles ;
2344
24- protected MemClsFileManager (StandardJavaFileManager fileManager , Map <String , JavaMemCls > destFiles ) {
45+ protected MemClsFileManager (StandardJavaFileManager fileManager , Map <String , JavaMemCls > destFiles , Map < String , JavaStringSource > srcFiles ) {
2546 super (fileManager );
2647 this .destFiles = destFiles ;
48+ this .srcFiles = srcFiles ;
2749 }
2850
51+ // redirects class file output to memory
2952 public JavaFileObject getJavaFileForOutput (Location location , String className , Kind kind , FileObject sibling ) throws IOException {
53+ if (!(Kind .CLASS .equals (kind ) && StandardLocation .CLASS_OUTPUT .equals (location )))
54+ return super .getJavaFileForOutput (location , className , kind , sibling );
3055 if (destFiles .containsKey (className )) {
3156 return destFiles .get (className );
3257 } else {
@@ -41,4 +66,51 @@ public void close() throws IOException {
4166 this .destFiles = null ;
4267 }
4368
69+ public Iterable <JavaFileObject > list (Location location , String packageName , Set <Kind > kinds , boolean recurse ) throws IOException {
70+ List <JavaFileObject > ret = new ArrayList <JavaFileObject >();
71+ if ((StandardLocation .CLASS_OUTPUT .equals (location ) || StandardLocation .CLASS_PATH .equals (location )) && kinds .contains (Kind .CLASS )) {
72+ for (Map .Entry <String , JavaMemCls > e : destFiles .entrySet ()) {
73+ String pkgName = resolvePkgName (e .getKey ());
74+ if (recurse ) {
75+ if (pkgName .contains (packageName ))
76+ ret .add (e .getValue ());
77+ } else {
78+ if (pkgName .equals (packageName ))
79+ ret .add (e .getValue ());
80+ }
81+ }
82+ } else if (StandardLocation .SOURCE_PATH .equals (location ) && kinds .contains (Kind .SOURCE )) {
83+ for (Map .Entry <String , JavaStringSource > e : srcFiles .entrySet ()) {
84+ String pkgName = resolvePkgName (e .getKey ());
85+ if (recurse ) {
86+ if (pkgName .contains (packageName ))
87+ ret .add (e .getValue ());
88+ } else {
89+ if (pkgName .equals (packageName ))
90+ ret .add (e .getValue ());
91+ }
92+ }
93+ }
94+ // 也包含super.list
95+ Iterable <JavaFileObject > superList = super .list (location , packageName , kinds , recurse );
96+ if (superList != null )
97+ for (JavaFileObject f : superList )
98+ ret .add (f );
99+ return ret ;
100+ }
101+
102+ private String resolvePkgName (String fullQualifiedClsName ) {
103+ return fullQualifiedClsName .substring (0 , fullQualifiedClsName .lastIndexOf ('.' ));
104+ }
105+
106+ public String inferBinaryName (Location location , JavaFileObject file ) {
107+ if (file instanceof JavaMemCls ) {
108+ return ((JavaMemCls ) file ).getClsName ();
109+ } else if (file instanceof JavaStringSource ) {
110+ return ((JavaStringSource ) file ).getClsName ();
111+ } else {
112+ return super .inferBinaryName (location , file );
113+ }
114+ }
115+
44116}
0 commit comments