Skip to content

Commit 1b12af0

Browse files
author
jossonsmith
committed
Merge /trunk/ to [236]
Fix bug that TypeDeclaration inside MethodDeclaration do not correct class name under Java 5.
1 parent 6506b5a commit 1b12af0

File tree

5 files changed

+156
-10
lines changed

5 files changed

+156
-10
lines changed

src/net/sf/j2s/core/JavaModelManager.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,11 @@ private void saveBuiltState(PerProjectInfo info) throws CoreException {
308308
* Sets the last built state for the given project, or null to reset it.
309309
*/
310310
public void setLastBuiltState(IProject project, Object state) {
311+
PerProjectInfo info = null;
311312
if (Java2ScriptProject.hasJava2ScriptNature(project)) {
312313
// should never be requested on non-Java projects
313-
PerProjectInfo info = getPerProjectInfo(project, true /*create if missing*/);
314+
//PerProjectInfo info = getPerProjectInfo(project, true /*create if missing*/);
315+
info = getPerProjectInfo(project, true /*create if missing*/);
314316
info.triedRead = true; // no point trying to re-read once using setter
315317
info.savedState = state;
316318
}
@@ -322,6 +324,15 @@ public void setLastBuiltState(IProject project, Object state) {
322324
} catch(SecurityException se) {
323325
// could not delete file: cannot do much more
324326
}
327+
} else {
328+
if (info != null) {
329+
try {
330+
saveBuiltState(info);
331+
} catch (CoreException e) {
332+
// TODO Auto-generated catch block
333+
e.printStackTrace();
334+
}
335+
}
325336
}
326337
}
327338
}

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

Lines changed: 136 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.eclipse.jdt.core.dom.PrimitiveType;
4141
import org.eclipse.jdt.core.dom.QualifiedName;
4242
import org.eclipse.jdt.core.dom.QualifiedType;
43+
import org.eclipse.jdt.core.dom.ReturnStatement;
4344
import org.eclipse.jdt.core.dom.SimpleName;
4445
import org.eclipse.jdt.core.dom.SimpleType;
4546
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
@@ -176,7 +177,26 @@ public boolean visit(AnonymousClassDeclaration node) {
176177
buffer.append("Clazz.instantialize (this, arguments);\r\n");
177178

178179
buffer.append("}, ");
180+
181+
182+
String emptyFun = "Clazz.decorateAsClass (function () {\r\n" +
183+
"Clazz.instantialize (this, arguments);\r\n" +
184+
"}, ";
185+
int idx = buffer.lastIndexOf(emptyFun);
179186

187+
if (idx != -1 && idx == buffer.length() - emptyFun.length()) {
188+
buffer.replace(idx, buffer.length(), "Clazz.declareType (");
189+
} else {
190+
emptyFun = "Clazz.decorateAsClass (function () {\r\n" +
191+
"Clazz.prepareCallback (this, arguments);\r\n" +
192+
"Clazz.instantialize (this, arguments);\r\n" +
193+
"}, ";
194+
idx = buffer.lastIndexOf(emptyFun);
195+
196+
if (idx != -1 && idx == buffer.length() - emptyFun.length()) {
197+
buffer.replace(idx, buffer.length(), "Clazz.declareAnonymous (");
198+
}
199+
}
180200
// buffer.append("Clazz.decorateAsType (");
181201
// buffer.append("cla$$");
182202
// buffer.append(fullClassName);
@@ -325,6 +345,7 @@ public boolean visit(CastExpression node) {
325345
buffer.append("Math.round (");
326346
node.getExpression().accept(this);
327347
buffer.append (")");
348+
return false;
328349
// } else if ("int".equals(name) || "byte".equals(name)
329350
// || "double".equals(name) || "float".equals(name)
330351
// || "short".equals(name) || "long".equals(name)) {
@@ -385,7 +406,11 @@ public boolean visit(ClassInstanceCreation node) {
385406
if (!binding.isTopLevel()) {
386407
if ((binding.getModifiers() & Modifier.STATIC) == 0) {
387408
buffer.append("Clazz.innerTypeInstance (");
388-
buffer.append(JavaLangUtil.ripJavaLang(binding.getQualifiedName()));
409+
if (binding.isAnonymous() || binding.isLocal()) {
410+
buffer.append(JavaLangUtil.ripJavaLang(binding.getBinaryName()));
411+
} else {
412+
buffer.append(JavaLangUtil.ripJavaLang(binding.getQualifiedName()));
413+
}
389414
buffer.append(", this, ");
390415
/*
391416
String scope = null;
@@ -567,6 +592,17 @@ public void endVisit(EnumDeclaration node) {
567592
// }
568593

569594
buffer.append("}, ");
595+
596+
597+
String emptyFun = "Clazz.decorateAsClass (function () {\r\n" +
598+
"Clazz.instantialize (this, arguments);\r\n" +
599+
"}, ";
600+
int idx = buffer.lastIndexOf(emptyFun);
601+
602+
if (idx != -1 && idx == buffer.length() - emptyFun.length()) {
603+
buffer.replace(idx, buffer.length(), "Clazz.declareType (");
604+
}
605+
570606
String fullClassName = null;//getFullClassName();
571607
if (thisPackageName != null && thisPackageName.length() != 0) {
572608
fullClassName = thisPackageName + '.' + thisClassName;
@@ -1314,6 +1350,57 @@ public boolean visit(MethodDeclaration node) {
13141350
return false;
13151351
}
13161352
}
1353+
/*
1354+
* To skip those methods or constructors which are just overriding with
1355+
* default super methods or constructors.
1356+
*/
1357+
Block body = node.getBody();
1358+
boolean needToCheckArgs = false;
1359+
List argsList = null;
1360+
if (body != null && body.statements().size() == 1) {
1361+
Object statement = body.statements().get(0);
1362+
if (statement instanceof ReturnStatement) {
1363+
ReturnStatement ret = (ReturnStatement) statement;
1364+
Expression exp = ret.getExpression();
1365+
if (exp instanceof SuperMethodInvocation) {
1366+
SuperMethodInvocation superRet = (SuperMethodInvocation) exp;
1367+
if (superRet.getName().toString().equals(node.getName().toString())) {
1368+
// same method name
1369+
needToCheckArgs = true;
1370+
argsList = superRet.arguments();
1371+
}
1372+
}
1373+
} else if (statement instanceof SuperConstructorInvocation) {
1374+
SuperConstructorInvocation superConstructor = (SuperConstructorInvocation) statement;
1375+
needToCheckArgs = true;
1376+
argsList = superConstructor.arguments();
1377+
}
1378+
}
1379+
if (needToCheckArgs) {
1380+
List params = node.parameters();
1381+
if (params.size() == argsList.size()) {
1382+
// same parameters count
1383+
boolean isOnlySuper = true;
1384+
for (Iterator iter = params.iterator(), itr = argsList.iterator(); iter.hasNext();) {
1385+
ASTNode astNode = (ASTNode) iter.next();
1386+
ASTNode argNode = (ASTNode) itr.next();
1387+
if (astNode instanceof SingleVariableDeclaration
1388+
&& argNode instanceof SimpleName) {
1389+
SingleVariableDeclaration varDecl = (SingleVariableDeclaration) astNode;
1390+
String paramID = varDecl.getName().getIdentifier();
1391+
String argID = ((SimpleName) argNode).getIdentifier();
1392+
if (!paramID.equals(argID)) {
1393+
// not with the same order, break out
1394+
isOnlySuper = false;
1395+
break;
1396+
}
1397+
}
1398+
}
1399+
if (isOnlySuper) {
1400+
return false;
1401+
}
1402+
}
1403+
}
13171404
if ((node.getModifiers() & Modifier.PRIVATE) != 0) {
13181405
MethodReferenceASTVisitor methodRefVisitor = new MethodReferenceASTVisitor();
13191406
methodRefVisitor.setMethodSignature(node.resolveBinding().getKey());
@@ -1938,8 +2025,14 @@ public boolean visit(SimpleName node) {
19382025
name = typeBinding.getQualifiedName();
19392026
if ((name == null || name.length() == 0) && typeBinding.isLocal()) {
19402027
name = typeBinding.getBinaryName();
1941-
int idx1 = name.indexOf('$');
2028+
int idx0 = name.lastIndexOf(".");
2029+
if (idx0 == -1) {
2030+
idx0 = 0;
2031+
}
2032+
int idx1 = name.indexOf('$', idx0);
19422033
if (idx1 != -1) {
2034+
// TODO: Comment the following codes!
2035+
// I can't understand why now -- Josson
19432036
int idx2 = name.indexOf('$', idx1 + 1);
19442037
String parentAnon = "";
19452038
if (idx2 == -1) { // maybe the name is already "$1$2..." for Java5.0+ in Eclipse 3.2+
@@ -2090,7 +2183,11 @@ public boolean visit(SimpleName node) {
20902183
name = typeBinding.getQualifiedName();
20912184
if ((name == null || name.length() == 0) && typeBinding.isLocal()) {
20922185
name = typeBinding.getBinaryName();
2093-
int idx1 = name.indexOf('$');
2186+
int idx0 = name.lastIndexOf(".");
2187+
if (idx0 == -1) {
2188+
idx0 = 0;
2189+
}
2190+
int idx1 = name.indexOf('$', idx0);
20942191
if (idx1 != -1) {
20952192
int idx2 = name.indexOf('$', idx1 + 1);
20962193
String parentAnon = "";
@@ -2350,15 +2447,25 @@ public boolean visit(ThisExpression node) {
23502447
}
23512448

23522449
public void endVisit(TypeDeclaration node) {
2353-
if (node != rootTypeNode && node.getParent() != null && node.getParent() instanceof AbstractTypeDeclaration) {
2354-
2450+
if (node != rootTypeNode && node.getParent() != null
2451+
&& (node.getParent() instanceof AbstractTypeDeclaration
2452+
|| node.getParent() instanceof TypeDeclarationStatement)) {
23552453
return ;
23562454
}
23572455
if (!node.isInterface()) {
23582456
buffer.append("Clazz.instantialize (this, arguments);\r\n");
23592457
//buffer.append("};\r\n");
23602458
buffer.append("}, ");
23612459
}
2460+
2461+
String emptyFun = "Clazz.decorateAsClass (function () {\r\n" +
2462+
"Clazz.instantialize (this, arguments);\r\n" +
2463+
"}, ";
2464+
int idx = buffer.lastIndexOf(emptyFun);
2465+
2466+
if (idx != -1 && idx == buffer.length() - emptyFun.length()) {
2467+
buffer.replace(idx, buffer.length(), "Clazz.declareType (");
2468+
}
23622469

23632470

23642471
String fullClassName = null;
@@ -2620,6 +2727,9 @@ public void endVisit(TypeDeclaration node) {
26202727
// //buffer.append(fullClassName);
26212728
// buffer.append(".");
26222729
VariableDeclarationFragment vdf = (VariableDeclarationFragment) fragments.get(j);
2730+
if ("serialVersionUID".equals(vdf.getName().getIdentifier())) {
2731+
continue;
2732+
}
26232733
Expression initializer = vdf.getInitializer();
26242734
refVisitor.setReferenced(false);
26252735
if (initializer != null) {
@@ -2687,6 +2797,9 @@ public void endVisit(TypeDeclaration node) {
26872797
List fragments = field.fragments();
26882798
for (int j = 0; j < fragments.size(); j++) {
26892799
VariableDeclarationFragment vdf = (VariableDeclarationFragment) fragments.get(j);
2800+
if ("serialVersionUID".equals(vdf.getName().getIdentifier())) {
2801+
continue;
2802+
}
26902803
Expression initializer = vdf.getInitializer();
26912804
refVisitor.setReferenced(false);
26922805
if (initializer != null) {
@@ -2967,7 +3080,11 @@ public boolean visit(TypeDeclaration node) {
29673080
// if (thisClassName == null || thisClassName.trim().length() == 0) {
29683081
// thisClassName = node.getName().toString();
29693082
// }
2970-
if ((node != rootTypeNode) && node.getParent() != null && node.getParent() instanceof AbstractTypeDeclaration) {
3083+
System.out.println();
3084+
3085+
if ((node != rootTypeNode) && node.getParent() != null
3086+
&& (node.getParent() instanceof AbstractTypeDeclaration
3087+
|| node.getParent() instanceof TypeDeclarationStatement)) {
29713088
/* inner static class */
29723089
ASTScriptVisitor visitor = null;
29733090
try {
@@ -2976,7 +3093,16 @@ public boolean visit(TypeDeclaration node) {
29763093
visitor = new ASTScriptVisitor(); // Default visitor
29773094
}
29783095
visitor.rootTypeNode = node;
2979-
visitor.thisClassName = thisClassName + "." + node.getName();
3096+
if (node.getParent() instanceof TypeDeclarationStatement) {
3097+
anonymousCount++;
3098+
if (node.resolveBinding().getBinaryName().matches(".*\\$[0-9]+\\$.*")) {
3099+
visitor.thisClassName = thisClassName + "$" + anonymousCount + "$" + node.getName();
3100+
} else {
3101+
visitor.thisClassName = thisClassName + "$" + anonymousCount + node.getName();
3102+
}
3103+
} else {
3104+
visitor.thisClassName = thisClassName + "." + node.getName();
3105+
}
29803106
visitor.thisPackageName = thisPackageName;
29813107
// System.out.println(visitor.thisClassName);
29823108
// System.out.println(visitor.thisPackageName);
@@ -3100,7 +3226,9 @@ public static void main(String[] args) {
31003226

31013227
buffer.append("function () {\r\n");
31023228
if (node == rootTypeNode && (node.getModifiers() & Modifier.STATIC) == 0
3103-
&& !((TypeDeclaration) node.getParent()).isInterface()) {
3229+
&& ((node.getParent() instanceof TypeDeclaration
3230+
&& !((TypeDeclaration) node.getParent()).isInterface())
3231+
|| node.getParent() instanceof TypeDeclarationStatement)) {
31043232
buffer.append("Clazz.prepareCallback (this, arguments);\r\n");
31053233
}
31063234
List bodyDeclarations = node.bodyDeclarations();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public boolean visit(TypeLiteral node) {
446446
*/
447447
public boolean visit(TypeDeclaration node) {
448448
ITypeBinding resolveBinding = node.resolveBinding();
449-
if (resolveBinding.isTopLevel()) {
449+
if (resolveBinding != null && resolveBinding.isTopLevel()) {
450450
String thisClassName = resolveBinding.getQualifiedName();
451451
classNameSet.add(thisClassName);
452452
classBindingSet.add(resolveBinding);
@@ -502,6 +502,7 @@ public boolean visit(EnumDeclaration node) {
502502
}
503503
}
504504
}
505+
musts.add("java.lang.Enum");
505506
visitForMusts(node);
506507
visitForRequires(node);
507508
visitForOptionals(node);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ public static String ripJavaLang(String name) {
2424
|| ((ch = name.charAt(index + 10)) >= 'A' && ch <= 'Z'))) {
2525
// ((idx2 = name.indexOf('.', index + 10)) == -1
2626
// || !name.substring(index + 10, idx2).startsWith ("ref"))) {
27+
/*
2728
if (name.indexOf ("Error") != -1 || name.indexOf("Exception") != -1
2829
|| name.indexOf("ThreadDeath") != -1) {
2930
3031
} else {
3132
name = name.substring(10);
3233
}
34+
*/
35+
name = name.substring(10);
3336
}
3437
String swt = "org.eclipse.swt.SWT";
3538
index = name.indexOf(swt);

src/net/sf/j2s/core/compiler/Java2ScriptCompiler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,9 @@ public static void outputJavaScript(ASTScriptVisitor visitor, DependencyASTVisit
460460

461461
public static String[] getClazzAbbrMap() {
462462
String[] clazzAll = new String[] {
463+
"Clazz.load", "L", //
464+
"Clazz.declareAnonymous", "W", //
465+
"Clazz.declareType", "T", //
463466
"Clazz.declarePackage", "J", //
464467
"Clazz.decorateAsClass", "C", //
465468
"Clazz.instantialize", "Z", //

0 commit comments

Comments
 (0)