Skip to content

Commit effc971

Browse files
author
zhourenjian
committed
Fixed bugs:
1. "void.class" is not correctly compiled. 2. "package hello.delete;" compilation violates JavaScript keyword "delete" 3. "\t
1 parent 88bf1f4 commit effc971

File tree

7 files changed

+130
-29
lines changed

7 files changed

+130
-29
lines changed

.settings/org.eclipse.jdt.core.prefs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Wed Aug 29 20:57:27 CST 2007
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=disabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.4
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=warning
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning
12+
org.eclipse.jdt.core.compiler.source=1.3

build.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ bin.includes = META-INF/,\
66
schema/
77
src.includes = META-INF/,\
88
plugin.xml
9+
javacSource = 1.3
10+
javacTarget = 1.2

src/net/sf/j2s/core/astvisitors/ASTKeywordVisitor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ protected void boxingNode(ASTNode element) {
9393
((ASTTigerVisitor) getAdaptable(ASTTigerVisitor.class)).boxingNode(element);
9494
}
9595

96+
protected String assureQualifiedName(String name) {
97+
return ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class)).assureQualifiedName(name);
98+
}
99+
96100
protected String shortenQualifiedName(String name) {
97101
return ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class)).shortenQualifiedName(name);
98102
}

src/net/sf/j2s/core/astvisitors/ASTScriptVisitor.java

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public boolean visit(AnonymousClassDeclaration node) {
266266

267267
int lastIndexOf = fullClassName.lastIndexOf ('.');
268268
if (lastIndexOf != -1) {
269-
buffer.append(shortenPackageName(fullClassName));
269+
buffer.append(assureQualifiedName(shortenPackageName(fullClassName)));
270270
buffer.append(", \"" + fullClassName.substring(lastIndexOf + 1) + "\"");
271271
} else {
272272
buffer.append("null, \"" + fullClassName + "\"");
@@ -276,7 +276,7 @@ public boolean visit(AnonymousClassDeclaration node) {
276276
ITypeBinding superclass = binding.getSuperclass();
277277
if (superclass != null) {
278278
String clazzName = superclass.getQualifiedName();
279-
clazzName = shortenQualifiedName(clazzName);
279+
clazzName = assureQualifiedName(shortenQualifiedName(clazzName));
280280
if (clazzName != null && clazzName.length() != 0
281281
&& !"Object".equals(clazzName)) {
282282
buffer.append(", ");
@@ -286,7 +286,7 @@ public boolean visit(AnonymousClassDeclaration node) {
286286
if (declaredTypes != null && declaredTypes.length > 0) {
287287
clazzName = declaredTypes[0].getQualifiedName();
288288
if (clazzName != null && clazzName.length() != 0) {
289-
clazzName = shortenQualifiedName(clazzName);
289+
clazzName = assureQualifiedName(shortenQualifiedName(clazzName));
290290
buffer.append(", null, ");
291291
buffer.append(clazzName);
292292
}
@@ -448,9 +448,9 @@ public boolean visit(ClassInstanceCreation node) {
448448
if ((binding.getModifiers() & Modifier.STATIC) == 0) {
449449
buffer.append("Clazz.innerTypeInstance (");
450450
if (binding.isAnonymous() || binding.isLocal()) {
451-
buffer.append(shortenQualifiedName(binding.getBinaryName()));
451+
buffer.append(assureQualifiedName(shortenQualifiedName(binding.getBinaryName())));
452452
} else {
453-
buffer.append(shortenQualifiedName(binding.getQualifiedName()));
453+
buffer.append(assureQualifiedName(shortenQualifiedName(binding.getQualifiedName())));
454454
}
455455
buffer.append(", this, ");
456456
buffer.append("null"); // No final variables for non-anonymous class
@@ -470,7 +470,7 @@ public boolean visit(ClassInstanceCreation node) {
470470
} else {
471471
buffer.append(" new ");
472472
if (fqName != null) {
473-
fqName = shortenQualifiedName(fqName);
473+
fqName = assureQualifiedName(shortenQualifiedName(fqName));
474474
buffer.append(fqName);
475475
}
476476
}
@@ -512,7 +512,7 @@ public boolean visit(ClassInstanceCreation node) {
512512
/*
513513
* TODO: Should get the class by type binding
514514
*/
515-
buffer.append(shortenQualifiedName(getFullClassName()));
515+
buffer.append(assureQualifiedName(shortenQualifiedName(getFullClassName())));
516516
buffer.append("$" + anonCount + ", innerThis, finalVars");
517517
if (argSize > 0) {
518518
buffer.append(", ");
@@ -613,7 +613,7 @@ public void endVisit(EnumDeclaration node) {
613613

614614
int lastIndexOf = fullClassName.lastIndexOf ('.');
615615
if (lastIndexOf != -1) {
616-
buffer.append(shortenPackageName(fullClassName));
616+
buffer.append(assureQualifiedName(shortenPackageName(fullClassName)));
617617
buffer.append(", \"" + fullClassName.substring(lastIndexOf + 1) + "\"");
618618
} else {
619619
buffer.append("null, \"" + fullClassName + "\"");
@@ -634,7 +634,7 @@ public void endVisit(EnumDeclaration node) {
634634
ITypeBinding binding = ((Type) element).resolveBinding();
635635
if (binding != null) {
636636
String clazzName = binding.getQualifiedName();
637-
clazzName = shortenQualifiedName(clazzName);
637+
clazzName = assureQualifiedName(shortenQualifiedName(clazzName));
638638
buffer.append(clazzName);
639639
} else {
640640
buffer.append(element);
@@ -1165,6 +1165,26 @@ public boolean visit(MethodDeclaration node) {
11651165
return false;
11661166
}
11671167
}
1168+
String[] pipeMethods = new String[] {
1169+
"isPipeLive",
1170+
"keepPipeLive",
1171+
"pipeDestroy",
1172+
"pipeSetup",
1173+
"pipeThrough",
1174+
"through",
1175+
"pipeInit",
1176+
"pipeMonitoring",
1177+
"pipeMonitoringInterval",
1178+
"setPipeHelper",
1179+
"updateStatus"
1180+
};
1181+
for (int i = 0; i < pipeMethods.length; i++) {
1182+
if (Bindings.isMethodInvoking(mBinding, "net.sf.j2s.ajax.SimplePipeRunnable", pipeMethods[i])) {
1183+
if (getJ2SDocTag(node, "@j2sKeep") == null) {
1184+
return false;
1185+
}
1186+
}
1187+
}
11681188
if (mBinding != null) {
11691189
methodDeclareStack.push(mBinding.getKey());
11701190
}
@@ -1348,7 +1368,7 @@ public boolean visit(MethodDeclaration node) {
13481368
blockLevel++;
13491369
buffer.append("{\r\n");
13501370
buffer.append("Clazz.superConstructor (this, ");
1351-
buffer.append(shortenQualifiedName(getFullClassName()));
1371+
buffer.append(assureQualifiedName(shortenQualifiedName(getFullClassName())));
13521372
buffer.append(", []);\r\n");
13531373
visitList(statements, "");
13541374
//buffer.append("}");
@@ -1584,7 +1604,7 @@ public boolean visit(MethodInvocation node) {
15841604
typeStr = "Number";
15851605
}
15861606
} else if (!nullTypeBinding.isTypeVariable()) {
1587-
typeStr = shortenQualifiedName(nullTypeBinding.getQualifiedName());
1607+
typeStr = assureQualifiedName(shortenQualifiedName(nullTypeBinding.getQualifiedName()));
15881608
}
15891609
}
15901610
}
@@ -1657,11 +1677,11 @@ && isInheritedFieldName(declaringClass, fieldName)) {
16571677
ITypeBinding binding2 = node.resolveTypeBinding();
16581678
if (binding != null) {
16591679
String name = binding2.getQualifiedName();
1660-
name = shortenQualifiedName(name);
1680+
name = assureQualifiedName(shortenQualifiedName(name));
16611681
buffer.append(name);
16621682
} else {
16631683
String nodeId = getJ2SName(node);
1664-
buffer.append(shortenQualifiedName(nodeId));
1684+
buffer.append(assureQualifiedName(shortenQualifiedName(nodeId)));
16651685
}
16661686
return false;
16671687
}
@@ -1685,7 +1705,7 @@ && isInheritedFieldName(declaringClass, fieldName)) {
16851705
// String name = NameConverterUtil.getJ2SName(node);
16861706
if (typeBinding != null) {
16871707
String name = typeBinding.getQualifiedName();
1688-
name = shortenQualifiedName(name);
1708+
name = assureQualifiedName(shortenQualifiedName(name));
16891709
if (checkKeyworkViolation(name)) {
16901710
buffer.append('$');
16911711
}
@@ -1716,7 +1736,7 @@ private void simpleNameInVarBinding(SimpleName node, char ch, IVariableBinding v
17161736
// with my anonymous class name!
17171737
name = declaringClass.getBinaryName();
17181738
}
1719-
name = shortenQualifiedName(name);
1739+
name = assureQualifiedName(shortenQualifiedName(name));
17201740
if (name.length() != 0) {
17211741
buffer.append(name);
17221742
buffer.append(".");
@@ -1809,7 +1829,7 @@ private void simpleNameInMethodBinding(SimpleName node, char ch, IMethodBinding
18091829
MethodInvocation mthInv = (MethodInvocation) parent;
18101830
if (mthInv.getExpression() == null) {
18111831
String name = declaringClass.getQualifiedName();
1812-
name = shortenQualifiedName(name);
1832+
name = assureQualifiedName(shortenQualifiedName(name));
18131833
if (name.length() != 0) {
18141834
buffer.append(name);
18151835
buffer.append(".");
@@ -1916,7 +1936,7 @@ private void appendFieldName(ASTNode parent, ITypeBinding declaringClass) {
19161936
public boolean visit(SimpleType node) {
19171937
ITypeBinding binding = node.resolveBinding();
19181938
if (binding != null) {
1919-
buffer.append(shortenQualifiedName(binding.getQualifiedName()));
1939+
buffer.append(assureQualifiedName(shortenQualifiedName(binding.getQualifiedName())));
19201940
} else {
19211941
buffer.append(node);
19221942
}
@@ -1977,7 +1997,7 @@ public boolean visit(SuperConstructorInvocation node) {
19771997
* TODO: expression before the "super" should be considered.
19781998
*/
19791999
buffer.append("Clazz.superConstructor (this, ");
1980-
buffer.append(shortenQualifiedName(getFullClassName()));
2000+
buffer.append(assureQualifiedName(shortenQualifiedName(getFullClassName())));
19812001
List arguments = node.arguments();
19822002
if (arguments.size() > 0) {
19832003
buffer.append(", [");
@@ -2034,7 +2054,7 @@ public boolean visit(SuperFieldAccess node) {
20342054

20352055
public boolean visit(SuperMethodInvocation node) {
20362056
buffer.append("Clazz.superCall (this, ");
2037-
buffer.append(shortenQualifiedName(getFullClassName()));
2057+
buffer.append(assureQualifiedName(shortenQualifiedName(getFullClassName())));
20382058
buffer.append(", \"");
20392059
String name = getJ2SName(node.getName());
20402060
buffer.append(name);
@@ -2128,7 +2148,7 @@ public void endVisit(TypeDeclaration node) {
21282148
buffer.append("Clazz.declareInterface (");
21292149
int lastIndexOf = fullClassName.lastIndexOf ('.');
21302150
if (lastIndexOf != -1) {
2131-
buffer.append(shortenPackageName(fullClassName));
2151+
buffer.append(assureQualifiedName(shortenPackageName(fullClassName)));
21322152
buffer.append(", \"" + fullClassName.substring(lastIndexOf + 1) + "\"");
21332153
} else {
21342154
buffer.append("null, \"" + fullClassName + "\"");
@@ -2137,7 +2157,7 @@ public void endVisit(TypeDeclaration node) {
21372157
} else {
21382158
int lastIndexOf = fullClassName.lastIndexOf ('.');
21392159
if (lastIndexOf != -1) {
2140-
buffer.append(shortenPackageName(fullClassName));
2160+
buffer.append(assureQualifiedName(shortenPackageName(fullClassName)));
21412161
buffer.append(", \"" + fullClassName.substring(lastIndexOf + 1) + "\"");
21422162
} else {
21432163
buffer.append("null, \"" + fullClassName + "\"");
@@ -2151,7 +2171,7 @@ public void endVisit(TypeDeclaration node) {
21512171
ITypeBinding superclass = typeBinding.getSuperclass();
21522172
if (superclass != null) {
21532173
String clazzName = superclass.getQualifiedName();
2154-
clazzName = shortenQualifiedName(clazzName);
2174+
clazzName = assureQualifiedName(shortenQualifiedName(clazzName));
21552175
if (clazzName != null && clazzName.length() != 0
21562176
&& !"Object".equals(clazzName)) {
21572177
buffer.append(clazzName);
@@ -2178,7 +2198,7 @@ public void endVisit(TypeDeclaration node) {
21782198
ITypeBinding binding = ((Type) element).resolveBinding();
21792199
if (binding != null) {
21802200
String clazzName = binding.getQualifiedName();
2181-
clazzName = shortenQualifiedName(clazzName);
2201+
clazzName = assureQualifiedName(shortenQualifiedName(clazzName));
21822202
buffer.append(clazzName);
21832203
} else {
21842204
buffer.append(element);
@@ -2200,7 +2220,7 @@ public void endVisit(TypeDeclaration node) {
22002220
if (binding != null && !binding.isTopLevel()) {
22012221
if ((binding.getModifiers() & Modifier.STATIC) == 0) {
22022222
buffer.append(", Clazz.innerTypeInstance (");
2203-
buffer.append(shortenQualifiedName(binding.getQualifiedName()));
2223+
buffer.append(assureQualifiedName(shortenQualifiedName(binding.getQualifiedName())));
22042224
buffer.append(", this, null, Clazz.inheritArgs");
22052225
buffer.append(")");
22062226
}

src/net/sf/j2s/core/astvisitors/ASTTypeVisitor.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,46 @@ public String getTypeStringName(Type type) {
228228
return null;
229229
}
230230

231+
public String assureQualifiedName(String name) {
232+
if (name == null || name.length() == 0) {
233+
return name;
234+
}
235+
String[] keywords = ASTFieldVisitor.keywods;
236+
String[] packages = null;
237+
boolean existedKeyword = false;
238+
for (int i = 0; i < keywords.length; i++) {
239+
if (name.indexOf(keywords[i]) != -1) {
240+
if (packages == null) {
241+
packages = name.split("\\.");
242+
}
243+
for (int j = 0; j < packages.length; j++) {
244+
if (keywords[i].equals(packages[j])) {
245+
packages[j] = "[\"" + packages[j] + "\"]";
246+
existedKeyword = true;
247+
}
248+
}
249+
}
250+
}
251+
if (existedKeyword) {
252+
StringBuffer sb = new StringBuffer();
253+
for (int i = 0; i < packages.length; i++) {
254+
if (packages[i].charAt(0) == '[') {
255+
if (i == 0) {
256+
sb.append("window");
257+
}
258+
sb.append(packages[i]);
259+
} else {
260+
if (i != 0) {
261+
sb.append('.');
262+
}
263+
sb.append(packages[i]);
264+
}
265+
}
266+
return sb.toString();
267+
} else {
268+
return name;
269+
}
270+
}
231271

232272
public boolean isIntegerType(String type) {
233273
if ("int".equals(type)

src/net/sf/j2s/core/astvisitors/ASTVariableVisitor.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,36 @@ protected String checkConstantValue(Expression node) {
207207
if (constValue != null && (constValue instanceof String)) {
208208
StringBuffer buffer = new StringBuffer();
209209
String str = (String) constValue;
210-
if (str.length() > 20) {
210+
int length = str.length();
211+
if (length > 20) {
211212
return null;
212213
}
213214
buffer.append("\"");
214-
buffer.append(str.replaceAll("\\\\", "\\\\\\\\")
215-
.replaceAll("\r", "\\\\r")
216-
.replaceAll("\n", "\\\\n")
217-
.replaceAll("\"", "\\\\\""));
215+
for (int i = 0; i < length; i++) {
216+
char c = str.charAt(i);
217+
if (c == '\\' || c == '\'' || c == '\"') {
218+
buffer.append('\\');
219+
buffer.append(c);
220+
} else if (c == '\r') {
221+
buffer.append("\\r");
222+
} else if (c == '\n') {
223+
buffer.append("\\n");
224+
} else if (c == '\t') {
225+
buffer.append("\\t");
226+
} else if (c == '\f') {
227+
buffer.append("\\f");
228+
} else if (c < 32 || c > 127) {
229+
buffer.append("\\u");
230+
String hexStr = Integer.toHexString(c);
231+
int zeroLen = 4 - hexStr.length();
232+
for (int k = 0; k < zeroLen; k++) {
233+
buffer.append('0');
234+
}
235+
buffer.append(hexStr);
236+
} else {
237+
buffer.append(c);
238+
}
239+
}
218240
buffer.append("\"");
219241
return buffer.toString();
220242
}

src/net/sf/j2s/core/astvisitors/DependencyASTVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ public boolean isQualifiedNameOK(String qualifiedName, ASTNode node) {
489489
&& !"byte".equals(qualifiedName)
490490
&& !"char".equals(qualifiedName)
491491
&& !"boolean".equals(qualifiedName)
492+
&& !"void".equals(qualifiedName)
492493
&& !qualifiedName.startsWith("org.w3c.dom.")
493494
&& !qualifiedName.startsWith("org.eclipse.swt.internal.xhtml.")) {
494495
ASTNode root = node.getRoot();

0 commit comments

Comments
 (0)