40
40
import org .eclipse .jdt .core .dom .PrimitiveType ;
41
41
import org .eclipse .jdt .core .dom .QualifiedName ;
42
42
import org .eclipse .jdt .core .dom .QualifiedType ;
43
+ import org .eclipse .jdt .core .dom .ReturnStatement ;
43
44
import org .eclipse .jdt .core .dom .SimpleName ;
44
45
import org .eclipse .jdt .core .dom .SimpleType ;
45
46
import org .eclipse .jdt .core .dom .SingleVariableDeclaration ;
@@ -176,7 +177,26 @@ public boolean visit(AnonymousClassDeclaration node) {
176
177
buffer .append ("Clazz.instantialize (this, arguments);\r \n " );
177
178
178
179
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 );
179
186
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
+ }
180
200
// buffer.append("Clazz.decorateAsType (");
181
201
// buffer.append("cla$$");
182
202
// buffer.append(fullClassName);
@@ -325,6 +345,7 @@ public boolean visit(CastExpression node) {
325
345
buffer .append ("Math.round (" );
326
346
node .getExpression ().accept (this );
327
347
buffer .append (")" );
348
+ return false ;
328
349
// } else if ("int".equals(name) || "byte".equals(name)
329
350
// || "double".equals(name) || "float".equals(name)
330
351
// || "short".equals(name) || "long".equals(name)) {
@@ -385,7 +406,11 @@ public boolean visit(ClassInstanceCreation node) {
385
406
if (!binding .isTopLevel ()) {
386
407
if ((binding .getModifiers () & Modifier .STATIC ) == 0 ) {
387
408
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
+ }
389
414
buffer .append (", this, " );
390
415
/*
391
416
String scope = null;
@@ -567,6 +592,17 @@ public void endVisit(EnumDeclaration node) {
567
592
// }
568
593
569
594
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
+
570
606
String fullClassName = null ;//getFullClassName();
571
607
if (thisPackageName != null && thisPackageName .length () != 0 ) {
572
608
fullClassName = thisPackageName + '.' + thisClassName ;
@@ -1314,6 +1350,57 @@ public boolean visit(MethodDeclaration node) {
1314
1350
return false ;
1315
1351
}
1316
1352
}
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
+ }
1317
1404
if ((node .getModifiers () & Modifier .PRIVATE ) != 0 ) {
1318
1405
MethodReferenceASTVisitor methodRefVisitor = new MethodReferenceASTVisitor ();
1319
1406
methodRefVisitor .setMethodSignature (node .resolveBinding ().getKey ());
@@ -1938,8 +2025,14 @@ public boolean visit(SimpleName node) {
1938
2025
name = typeBinding .getQualifiedName ();
1939
2026
if ((name == null || name .length () == 0 ) && typeBinding .isLocal ()) {
1940
2027
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 );
1942
2033
if (idx1 != -1 ) {
2034
+ // TODO: Comment the following codes!
2035
+ // I can't understand why now -- Josson
1943
2036
int idx2 = name .indexOf ('$' , idx1 + 1 );
1944
2037
String parentAnon = "" ;
1945
2038
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) {
2090
2183
name = typeBinding .getQualifiedName ();
2091
2184
if ((name == null || name .length () == 0 ) && typeBinding .isLocal ()) {
2092
2185
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 );
2094
2191
if (idx1 != -1 ) {
2095
2192
int idx2 = name .indexOf ('$' , idx1 + 1 );
2096
2193
String parentAnon = "" ;
@@ -2350,15 +2447,25 @@ public boolean visit(ThisExpression node) {
2350
2447
}
2351
2448
2352
2449
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 )) {
2355
2453
return ;
2356
2454
}
2357
2455
if (!node .isInterface ()) {
2358
2456
buffer .append ("Clazz.instantialize (this, arguments);\r \n " );
2359
2457
//buffer.append("};\r\n");
2360
2458
buffer .append ("}, " );
2361
2459
}
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
+ }
2362
2469
2363
2470
2364
2471
String fullClassName = null ;
@@ -2620,6 +2727,9 @@ public void endVisit(TypeDeclaration node) {
2620
2727
// //buffer.append(fullClassName);
2621
2728
// buffer.append(".");
2622
2729
VariableDeclarationFragment vdf = (VariableDeclarationFragment ) fragments .get (j );
2730
+ if ("serialVersionUID" .equals (vdf .getName ().getIdentifier ())) {
2731
+ continue ;
2732
+ }
2623
2733
Expression initializer = vdf .getInitializer ();
2624
2734
refVisitor .setReferenced (false );
2625
2735
if (initializer != null ) {
@@ -2687,6 +2797,9 @@ public void endVisit(TypeDeclaration node) {
2687
2797
List fragments = field .fragments ();
2688
2798
for (int j = 0 ; j < fragments .size (); j ++) {
2689
2799
VariableDeclarationFragment vdf = (VariableDeclarationFragment ) fragments .get (j );
2800
+ if ("serialVersionUID" .equals (vdf .getName ().getIdentifier ())) {
2801
+ continue ;
2802
+ }
2690
2803
Expression initializer = vdf .getInitializer ();
2691
2804
refVisitor .setReferenced (false );
2692
2805
if (initializer != null ) {
@@ -2967,7 +3080,11 @@ public boolean visit(TypeDeclaration node) {
2967
3080
// if (thisClassName == null || thisClassName.trim().length() == 0) {
2968
3081
// thisClassName = node.getName().toString();
2969
3082
// }
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 )) {
2971
3088
/* inner static class */
2972
3089
ASTScriptVisitor visitor = null ;
2973
3090
try {
@@ -2976,7 +3093,16 @@ public boolean visit(TypeDeclaration node) {
2976
3093
visitor = new ASTScriptVisitor (); // Default visitor
2977
3094
}
2978
3095
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
+ }
2980
3106
visitor .thisPackageName = thisPackageName ;
2981
3107
// System.out.println(visitor.thisClassName);
2982
3108
// System.out.println(visitor.thisPackageName);
@@ -3100,7 +3226,9 @@ public static void main(String[] args) {
3100
3226
3101
3227
buffer .append ("function () {\r \n " );
3102
3228
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 )) {
3104
3232
buffer .append ("Clazz.prepareCallback (this, arguments);\r \n " );
3105
3233
}
3106
3234
List bodyDeclarations = node .bodyDeclarations ();
0 commit comments