Skip to content

Commit 8ef4197

Browse files
committed
refactoring for $incl$ - saves 3% in coreswingjs.z.js
1 parent 1cbe880 commit 8ef4197

File tree

7 files changed

+348
-317
lines changed

7 files changed

+348
-317
lines changed

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTEmptyVisitor.java

Lines changed: 141 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
*******************************************************************************/
1414
package net.sf.j2s.core.astvisitors;
1515

16+
import java.text.SimpleDateFormat;
17+
import java.util.ArrayList;
18+
import java.util.Date;
1619
import java.util.HashMap;
17-
import java.util.HashSet;
1820
import java.util.Hashtable;
1921
import java.util.List;
2022
import java.util.Map;
@@ -117,11 +119,135 @@
117119
*/
118120
public 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
// }

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTJ2SDocVisitor.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import org.eclipse.jdt.core.dom.MethodDeclaration;
3131
import org.eclipse.jdt.core.dom.TagElement;
3232
import org.eclipse.jdt.core.dom.TextElement;
33-
34-
import net.sf.j2s.core.astvisitors.adapters.Bindings;
3533
//import net.sf.j2s.core.astvisitors.adapters.ExtendedAdapter;
3634

3735
/**
@@ -44,18 +42,8 @@
4442
*/
4543
public class ASTJ2SDocVisitor extends ASTEmptyVisitor {
4644

47-
private boolean isDebugging = false;
48-
4945
private ArrayList<Javadoc> rootJavaDocs;
5046

51-
public boolean isDebugging() {
52-
return isDebugging;
53-
}
54-
55-
public void setDebugging(boolean isDebugging) {
56-
this.isDebugging = isDebugging;
57-
}
58-
5947
public boolean visit(Block node) {
6048
ASTNode parent = node.getParent();
6149
BodyDeclaration dec = (parent instanceof MethodDeclaration && !((MethodDeclaration)parent).isConstructor()
@@ -80,14 +68,14 @@ public boolean visit(Block node) {
8068
protected boolean addJavadocForBlock(Javadoc javadoc, Block node) {
8169
List<?> tags = (javadoc == null ? null : javadoc.tags());
8270
return (tags != null && tags.size() > 0 && (getTag(tags, "@j2sIgnore") != null
83-
|| isDebugging() && addSourceForTag(getTag(tags, "@j2sDebug"), "", "")
71+
|| global_j2sFlag_isDebugging && addSourceForTag(getTag(tags, "@j2sDebug"), "", "")
8472
// note that isToCompileVariableName() always returns false
8573
//|| !isToCompileVariableName() && (tagEl = getTag(tags, "@j2sNativeSrc", node)) != null
8674
|| addSourceForTag(getTag(tags, "@j2sNative"), "", "")
87-
|| allowExtensions && (
88-
addSourceForTagExtended(getTag(tags, "@j2sXHTML"), "", "")
89-
|| addSourceForTagExtended(getTag(tags, "@j2sXCSS"), "", "")
90-
)
75+
// || global_allowExtensions && (
76+
// addSourceForTagExtended(getTag(tags, "@j2sXHTML"), "", "")
77+
// || addSourceForTagExtended(getTag(tags, "@j2sXCSS"), "", "")
78+
// )
9179
));
9280
}
9381

@@ -149,8 +137,9 @@ boolean readSources(BodyDeclaration node, String tagName, String prefix, String
149137
boolean haveJ2SJavaDoc = false;
150138
Javadoc javadoc = node.getJavadoc();
151139
if (javadoc != null && javadoc.tags().size() > 1)
152-
haveJ2SJavaDoc = (isExtended ? addSourceForTagExtended(getTag(javadoc.tags(), tagName), prefix, suffix)
153-
: addSourceForTag(getTag(javadoc.tags(), tagName), prefix, suffix));
140+
haveJ2SJavaDoc =
141+
//isExtended ? addSourceForTagExtended(getTag(javadoc.tags(), tagName), prefix, suffix) :
142+
addSourceForTag(getTag(javadoc.tags(), tagName), prefix, suffix);
154143
// only classes allow both
155144
if (haveJ2SJavaDoc && !allowBoth)
156145
return haveJ2SJavaDoc;
@@ -256,16 +245,16 @@ private String fixCommentBlock(String text) {
256245
.matcher(text).replaceAll("/*$1*/").replaceAll("<@>","@"));
257246
}
258247

259-
private boolean addSourceForTagExtended(TagElement tagEl, String prefix, String suffix) {
248+
// private boolean addSourceForTagExtended(TagElement tagEl, String prefix, String suffix) {
260249
// if (tagEl == null)
261250
// return false;
262251
// StringBuffer buf = new StringBuffer();
263252
// String firstLine = getSource(tagEl, buf, true);
264253
// buffer.append(prefix);
265254
// buffer.append(ExtendedAdapter.buildXSource(getQualifiedClassName(), tagEl.getTagName(), firstLine, buf.toString().trim()));
266255
// buffer.append(suffix);
267-
return true;
268-
}
256+
// return true;
257+
// }
269258

270259
/**
271260
* Get the nearest Javadoc that is after any other element but before this block.

0 commit comments

Comments
 (0)