1313 *******************************************************************************/
1414package net .sf .j2s .core .astvisitors ;
1515
16+ import java .text .SimpleDateFormat ;
17+ import java .util .ArrayList ;
18+ import java .util .Date ;
1619import java .util .HashMap ;
17- import java .util .HashSet ;
1820import java .util .Hashtable ;
1921import java .util .List ;
2022import java .util .Map ;
117119 */
118120public class ASTEmptyVisitor extends ASTVisitor {
119121
122+
123+ /**
124+ * includes @j2sDebug blocks; from j2s.compiler.mode=debug in .j2s
125+ *
126+ */
127+ protected boolean global_j2sFlag_isDebugging = false ;
128+
129+ public void setDebugging (boolean isDebugging ) {
130+ this .global_j2sFlag_isDebugging = isDebugging ;
131+ }
132+
133+ /**
134+ * separates top-level classes found in a source file
135+ *
136+ */
137+ private static final String ELEMENT_KEY = "__@J2S_ELEMENT__" ;
138+
139+ /**
140+ * Add the top-level class name with the element key.
141+ *
142+ * @param className
143+ */
144+ protected void appendElementKey (String className ) {
145+ buffer .append (ELEMENT_KEY + className + "\r \n " );
146+ }
147+
148+ /**
149+ * track the names for I$$[...]
150+ */
151+ protected StringBuffer global_includes = new StringBuffer ();
152+
153+ /**
154+ * map class names to I$$[] index
155+ *
156+ */
157+ protected Map <String , Integer >global_htIncludeNames = new Hashtable <>();
158+
159+ /**
160+ * I$$[] index counter
161+ *
162+ */
163+ protected int [] global_includeCount = new int [1 ];
164+
165+ /**
166+ * Register a qualified static name as an import var I$[n] unless it ends
167+ * with "Exception". create loads of inner classes pkg.Foo.Bar as
168+ * Clazz.load(['pkg.Foo','.Bar'])
169+ *
170+ * If caching, put into the code (I$[n]||$incl$(n)), where n is
171+ * the index into the I$[] array
172+ *
173+ * @param className
174+ * @param doCache
175+ * @return
176+ */
177+ protected String getNestedClazzLoads (String className , boolean doCache ) {
178+ String [] parts = className .split ("\\ ." );
179+ String s = parts [0 ];
180+ if (s .equals ("P$" )) {
181+ // can't do this with Clazz.load
182+ s = global_PackageName ;
183+ }
184+ int i = 1 ;
185+ // loop through packages and outer Class
186+ while (i < parts .length && (i == 1 || !Character .isUpperCase (parts [i - 1 ].charAt (0 ))))
187+ s += "." + parts [i ++];
188+ s = "'" + s + "'" ;
189+ // int nlast = parts.length;
190+ if (i < parts .length ) {
191+ s = "[" + s ;
192+ while (i < parts .length )
193+ s += ",'." + parts [i ++] + "'" ;
194+ s += "]" ;
195+ }
196+ if (doCache ) {
197+ Integer n = global_htIncludeNames .get (s );
198+ if (n == null && !s .endsWith ("Exception'" )) {
199+ global_htIncludeNames .put (s , n = new Integer (++global_includeCount [0 ]));
200+ global_includes .append (global_includeCount [0 ] == 1 ? ",I$=[[" : "," ).append (s );
201+ }
202+ if (n != null )
203+ return "(I$[" + n + "]||$incl$(" + n + "))" ;
204+
205+ }
206+ return "Clazz.load(" + s + ")" ;
207+ }
208+
209+
210+
211+ /**
212+ * Separate the buffer into a list so that all top-level elements can be in
213+ * their own file (as is done in Java). Provide a common include list
214+ *
215+ * We do not have to worry about inner classes, as they are never referenced
216+ * directly.
217+ *
218+ * @return List {elementName, js, elementName, js, ....}
219+ */
220+ public List <String > getElementList () {
221+ String trailer = "//Created " + new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" ).format (new Date ()) + "\n " ;
222+ List <String > elements = new ArrayList <String >();
223+ String js = buffer .toString ();
224+ String [] parts = js .split (ELEMENT_KEY );
225+ String header = parts [0 ];
226+ String header_noIncludes = header .replace (",I$=[[]]" , "" );
227+ header = header .replace (",I$=[]" , global_includes .length () == 0 ? "" : global_includes .append ("]],$incl$=function(i){return I$[i]=Clazz.load(I$[0][i-1])}" ));
228+ System .err .println (header );
229+ for (int i = 1 ; i < parts .length ; i ++) {
230+ js = parts [i ];
231+ int pt = js .indexOf ("\r \n " );
232+ elements .add (js .substring (0 , pt ));
233+ js = js .substring (pt + 2 );
234+ String head = "(function(){" + (js .indexOf ("(I$[" ) < 0 ? header_noIncludes : header );
235+
236+
237+ elements .add (head + js + "})();\r \n " + trailer );
238+ }
239+ return elements ;
240+ }
241+
120242 ASTEmptyVisitor () {
121243 super ();
122244 }
123245
124- protected String thisPackageName ;
246+ protected String global_PackageName ;
247+
248+ public String getPackageName () {
249+ return global_PackageName ;
250+ }
125251
126252 public static String [] basePackages = {
127253 "java.lang" ,
@@ -137,7 +263,7 @@ public class ASTEmptyVisitor extends ASTVisitor {
137263
138264 public boolean isBasePackage () {
139265 for (int i = 0 ; i < basePackages .length ; i ++)
140- if (basePackages [i ].equals (thisPackageName ))
266+ if (basePackages [i ].equals (global_PackageName ))
141267 return true ;
142268 return false ;
143269 }
@@ -165,13 +291,13 @@ protected static boolean isClassKnown(String qualifiedName) {
165291 return false ;
166292 }
167293
168- /**
169- * allow @j2sXHTML and @j2sXCSS extensions for Javadoc
170- *
171- * experimental; not implemented; uses adapters.ExtendedAdapter
172- *
173- */
174- protected boolean allowExtensions = false ;
294+ // /**
295+ // * allow @j2sXHTML and @j2sXCSS extensions for Javadoc
296+ // *
297+ // * experimental; not implemented; uses adapters.ExtendedAdapter
298+ // *
299+ // */
300+ // protected boolean global_allowExtensions = false;
175301
176302// /**
177303// * not implemented
@@ -184,27 +310,11 @@ protected static boolean isClassKnown(String qualifiedName) {
184310//
185311
186312
187- protected Map <String , Integer >htStaticNames = new Hashtable <>();
188- protected int [] staticCount = new int [1 ];
189-
190- /**
191- * Register a qualified static name as an import var I$[n]
192- * unless it ends with "Exception".
193- * @param name
194- * @return the next available index for this compilation unit
195- */
196- protected Integer getStaticNameIndex (String name ) {
197- Integer n = htStaticNames .get (name );
198- if (n == null && !name .endsWith ("Exception" ))
199- htStaticNames .put (name , n = new Integer (staticCount [0 ]++));
200- return n ;
201- }
202-
203- protected HashSet <String > definedPackageNames ;
204-
205- public void setPackageNames (HashSet <String > definedPackageNames ) {
206- this .definedPackageNames = definedPackageNames ;
207- }
313+ // protected HashSet<String> global_definedPackageNames;
314+ //
315+ // public void setPackageNames(HashSet<String> definedPackageNames) {
316+ // this.global_definedPackageNames = definedPackageNames;
317+ // }
208318
209319 protected static boolean isFinal (BodyDeclaration b ) {
210320 return Modifier .isFinal (b .getModifiers ());
@@ -235,10 +345,6 @@ protected String getNormalVariableName(String name) {
235345 return ((VariableAdapter ) getAdaptable (VariableAdapter .class )).getNormalVariableName (name );
236346 }
237347
238- public String getPackageName () {
239- return thisPackageName ;
240- }
241-
242348 /**
243349 * Shorten fully qualified class names starting with java.lang and will
244350 * replace a class name with C$. Use static getShortenedName(null, name,
@@ -273,10 +379,6 @@ protected void setClassName(String className) {
273379 ((TypeAdapter ) getAdaptable (TypeAdapter .class )).setClassName (className );
274380 }
275381
276- protected void setPackageName (String packageName ) {
277- thisPackageName = packageName ;
278- }
279-
280382// public void setToCompileVariableName(boolean toCompress) {
281383// ((ASTVariableAdapter) getAdaptable(ASTVariableAdapter.class)).setToCompileVariableName(toCompress);
282384// }
0 commit comments