41
41
import org .eclipse .jdt .core .dom .Modifier ;
42
42
import org .eclipse .jdt .core .dom .Name ;
43
43
import org .eclipse .jdt .core .dom .NullLiteral ;
44
+ import org .eclipse .jdt .core .dom .ParenthesizedExpression ;
44
45
import org .eclipse .jdt .core .dom .PrimitiveType ;
45
46
import org .eclipse .jdt .core .dom .QualifiedName ;
46
47
import org .eclipse .jdt .core .dom .ReturnStatement ;
@@ -447,12 +448,29 @@ public boolean visit(CastExpression node) {
447
448
|| pType .getPrimitiveTypeCode () == PrimitiveType .LONG ) {
448
449
if ("char" .equals (name )) {
449
450
buffer .append ("(" );
450
- expression .accept (this );
451
+ if (expression instanceof ParenthesizedExpression ) {
452
+ ParenthesizedExpression pe = (ParenthesizedExpression ) expression ;
453
+ pe .getExpression ().accept (this );
454
+ } else {
455
+ expression .accept (this );
456
+ }
451
457
buffer .append (").charCodeAt (0)" );
452
458
return false ;
453
459
} else if ("float" .equals (name ) || "double" .equals (name )) {
454
- buffer .append ("Math.round (" );
455
- expression .accept (this );
460
+ //buffer.append("Math.round (");
461
+ buffer .append ("Clazz." );
462
+ buffer .append (name );
463
+ buffer .append ("To" );
464
+ String targetType = pType .getPrimitiveTypeCode ().toString ();
465
+ buffer .append (targetType .substring (0 , 1 ).toUpperCase ());
466
+ buffer .append (targetType .substring (1 ));
467
+ buffer .append (" (" );
468
+ if (expression instanceof ParenthesizedExpression ) {
469
+ ParenthesizedExpression pe = (ParenthesizedExpression ) expression ;
470
+ pe .getExpression ().accept (this );
471
+ } else {
472
+ expression .accept (this );
473
+ }
456
474
buffer .append (")" );
457
475
return false ;
458
476
}
@@ -465,10 +483,18 @@ public boolean visit(CastExpression node) {
465
483
// return false;
466
484
} else if ("float" .equals (name ) || "double" .equals (name )) {
467
485
// TODO:
468
- buffer .append ("String.fromCharCode (" );
469
- buffer .append ("Math.round (" );
470
- expression .accept (this );
471
- buffer .append (")" );
486
+ buffer .append ("Clazz." );
487
+ buffer .append (name );
488
+ buffer .append ("ToChar (" );
489
+ // buffer.append("String.fromCharCode (");
490
+ // buffer.append("Math.round (");
491
+ if (expression instanceof ParenthesizedExpression ) {
492
+ ParenthesizedExpression pe = (ParenthesizedExpression ) expression ;
493
+ pe .getExpression ().accept (this );
494
+ } else {
495
+ expression .accept (this );
496
+ }
497
+ // buffer.append (")");
472
498
buffer .append (")" );
473
499
return false ;
474
500
} else if ("int" .equals (name ) || "byte" .equals (name )
@@ -499,7 +525,12 @@ public boolean visit(CastExpression node) {
499
525
}
500
526
}
501
527
buffer .append ("String.fromCharCode (" );
502
- expression .accept (this );
528
+ if (expression instanceof ParenthesizedExpression ) {
529
+ ParenthesizedExpression pe = (ParenthesizedExpression ) expression ;
530
+ pe .getExpression ().accept (this );
531
+ } else {
532
+ expression .accept (this );
533
+ }
503
534
buffer .append (")" );
504
535
return false ;
505
536
}
@@ -615,7 +646,9 @@ public boolean visit(ClassInstanceCreation node) {
615
646
currentBlockForVisit = blockLevel ;
616
647
visitedVars = variableVisitor .visitedVars = new ArrayList ();
617
648
variableVisitor .normalVars = new ArrayList ();
649
+ methodDeclareStack .push (binding .getKey ());
618
650
anonDeclare .accept (this );
651
+ methodDeclareStack .pop ();
619
652
buffer .append (", " );
620
653
621
654
buffer .append ("Clazz.innerTypeInstance (" );
@@ -774,14 +807,14 @@ private void visitArgumentItem(ASTNode element,
774
807
buffer .append (typeStr .replaceFirst ("^\\ $wt." , "org.eclipse.swt." ));
775
808
buffer .append ("\" )" );
776
809
} else {
777
- boxingNode (element );
778
810
Expression exp = (Expression ) element ;
779
811
ITypeBinding typeBinding = exp .resolveTypeBinding ();
780
812
String typeName = null ;
781
813
if (typeBinding != null ) {
782
814
typeName = typeBinding .getName ();
783
815
}
784
- if ("char" .equals (typeName ) && "int" .equals (parameterTypeName )) {
816
+ int idx1 = buffer .length ();
817
+ if ("char" .equals (typeName ) && !"char" .equals (parameterTypeName )) {
785
818
boolean ignored = false ;
786
819
/*
787
820
for (int j = 0; j < ignores.length / 3; j++) {
@@ -805,9 +838,38 @@ private void visitArgumentItem(ASTNode element,
805
838
ignored = (position == 0
806
839
&& (/*"append".equals(methodName) || */ "indexOf" .equals (methodName ) || "lastIndexOf" .equals (methodName ))
807
840
&& ("java.lang.String" .equals (Bindings .removeBrackets (clazzName ))));
841
+
842
+ if (!ignored && exp instanceof CharacterLiteral ) {
843
+ CharacterLiteral cl = (CharacterLiteral ) exp ;
844
+ buffer .append (0 + cl .charValue ());
845
+ ignored = true ;
846
+ } else {
847
+ boxingNode (element );
848
+ }
808
849
if (!ignored ) {
809
- buffer .append (".charCodeAt (0)" );
850
+ boolean appendingCode = true ;
851
+ int length = buffer .length ();
852
+ if (exp instanceof MethodInvocation ) {
853
+ MethodInvocation m = (MethodInvocation ) exp ;
854
+ if ("charAt" .equals (m .getName ().toString ())) {
855
+ int idx2 = buffer .indexOf (".charAt " , idx1 );
856
+ if (idx2 != -1 ) {
857
+ StringBuffer newMethodBuffer = new StringBuffer ();
858
+ newMethodBuffer .append (buffer .substring (idx1 , idx2 ));
859
+ newMethodBuffer .append (".charCodeAt " );
860
+ newMethodBuffer .append (buffer .substring (idx2 + 8 , length ));
861
+ buffer .delete (idx1 , length );
862
+ buffer .append (newMethodBuffer .toString ());
863
+ appendingCode = false ;
864
+ }
865
+ }
866
+ }
867
+ if (appendingCode ) {
868
+ buffer .append (".charCodeAt (0)" );
869
+ }
810
870
}
871
+ } else {
872
+ boxingNode (element );
811
873
}
812
874
}
813
875
}
@@ -1272,6 +1334,8 @@ && checkSameName(typeBinding, fieldName)) {
1272
1334
PrimitiveType pType = (PrimitiveType ) node .getType ();
1273
1335
if (pType .getPrimitiveTypeCode () == PrimitiveType .BOOLEAN ) {
1274
1336
buffer .append ("false" );
1337
+ } else if (pType .getPrimitiveTypeCode () == PrimitiveType .CHAR ) {
1338
+ buffer .append ("'\\ 0'" );
1275
1339
} else {
1276
1340
buffer .append ("0" );
1277
1341
}
@@ -1319,7 +1383,8 @@ private void charVisit(ASTNode node, boolean beCare) {
1319
1383
boxingNode (node );
1320
1384
return ;
1321
1385
}
1322
- ITypeBinding binding = ((Expression ) node ).resolveTypeBinding ();
1386
+ Expression exp = (Expression ) node ;
1387
+ ITypeBinding binding = exp .resolveTypeBinding ();
1323
1388
if (binding .isPrimitive () && "char" .equals (binding .getName ())) {
1324
1389
if (node instanceof CharacterLiteral ) {
1325
1390
CharacterLiteral cl = (CharacterLiteral ) node ;
@@ -1328,9 +1393,31 @@ private void charVisit(ASTNode node, boolean beCare) {
1328
1393
boxingNode (node );
1329
1394
buffer .append (".charCodeAt (0)" );
1330
1395
} else {
1396
+ int idx1 = buffer .length ();
1331
1397
buffer .append ("(" );
1332
1398
boxingNode (node );
1333
- buffer .append (").charCodeAt (0)" );
1399
+ buffer .append (")" );
1400
+
1401
+ boolean appendingCode = true ;
1402
+ int length = buffer .length ();
1403
+ if (exp instanceof MethodInvocation ) {
1404
+ MethodInvocation m = (MethodInvocation ) exp ;
1405
+ if ("charAt" .equals (m .getName ().toString ())) {
1406
+ int idx2 = buffer .indexOf (".charAt " , idx1 );
1407
+ if (idx2 != -1 ) {
1408
+ StringBuffer newMethodBuffer = new StringBuffer ();
1409
+ newMethodBuffer .append (buffer .substring (idx1 + 1 , idx2 ));
1410
+ newMethodBuffer .append (".charCodeAt " );
1411
+ newMethodBuffer .append (buffer .substring (idx2 + 8 , length - 1 ));
1412
+ buffer .delete (idx1 , length );
1413
+ buffer .append (newMethodBuffer .toString ());
1414
+ appendingCode = false ;
1415
+ }
1416
+ }
1417
+ }
1418
+ if (appendingCode ) {
1419
+ buffer .append (".charCodeAt (0)" );
1420
+ }
1334
1421
}
1335
1422
} else {
1336
1423
boxingNode (node );
@@ -1353,8 +1440,9 @@ public boolean visit(InfixExpression node) {
1353
1440
Expression right = node .getRightOperand ();
1354
1441
ITypeBinding typeBinding = left .resolveTypeBinding ();
1355
1442
1356
- if ((left instanceof SimpleName || left instanceof CharacterLiteral ) && (right instanceof SimpleName || right instanceof CharacterLiteral )
1357
- && (">" .equals (operator ) || "<" .equals (operator ) || ">=" .equals (operator ) || "<=" .equals (operator ))) {
1443
+ if (/*(left instanceof SimpleName || left instanceof CharacterLiteral) && (right instanceof SimpleName || right instanceof CharacterLiteral)
1444
+ && */ (">" .equals (operator ) || "<" .equals (operator ) || ">=" .equals (operator ) || "<=" .equals (operator )
1445
+ || "==" .equals (operator ) || "!=" .equals (operator ))) {
1358
1446
ITypeBinding rightBinding = right .resolveTypeBinding ();
1359
1447
if (typeBinding .isPrimitive () && "char" .equals (typeBinding .getName ())
1360
1448
&& rightBinding .isPrimitive () && "char" .equals (rightBinding .getName ())) {
@@ -1374,7 +1462,9 @@ public boolean visit(InfixExpression node) {
1374
1462
StringBuffer tmpBuffer = buffer ;
1375
1463
buffer = new StringBuffer ();
1376
1464
1377
- buffer .append ("Math.floor (" );
1465
+ //buffer.append("Math.floor (");
1466
+ // TODO
1467
+ buffer .append ("Clazz.doubleToInt (" );
1378
1468
charVisit (left , beCare );
1379
1469
buffer .append (' ' );
1380
1470
buffer .append (operator );
@@ -1390,7 +1480,8 @@ public boolean visit(InfixExpression node) {
1390
1480
Expression exp = (Expression ) element ;
1391
1481
ITypeBinding expBinding = exp .resolveTypeBinding ();
1392
1482
if (isIntegerType (expBinding .getName ())) {
1393
- buffer .insert (0 , "Math.floor (" );
1483
+ //buffer.insert(0, "Math.floor (");
1484
+ buffer .insert (0 , "Clazz.doubleToInt (" );
1394
1485
is2Floor = true ;
1395
1486
}
1396
1487
}
@@ -1650,7 +1741,11 @@ public boolean visit(MethodDeclaration node) {
1650
1741
}
1651
1742
1652
1743
if (node .isConstructor ()) {
1653
- buffer .append ("Clazz.makeConstructor (" );
1744
+ if (getJ2STag (node , "@j2sOverride" ) != null ) {
1745
+ buffer .append ("Clazz.overrideConstructor (" );
1746
+ } else {
1747
+ buffer .append ("Clazz.makeConstructor (" );
1748
+ }
1654
1749
} else {
1655
1750
if ((node .getModifiers () & Modifier .STATIC ) != 0 ) {
1656
1751
/* replace full class name with short variable name */
@@ -3011,7 +3106,7 @@ private String prepareSimpleSerializable(TypeDeclaration node, List bodyDeclarat
3011
3106
if (isSimpleSerializable ) {
3012
3107
List fragments = fieldDeclaration .fragments ();
3013
3108
int modifiers = fieldDeclaration .getModifiers ();
3014
- if ((Modifier .isPublic (modifiers ) || Modifier .isProtected (modifiers ))
3109
+ if ((Modifier .isPublic (modifiers )/* || Modifier.isProtected(modifiers)*/ )
3015
3110
&& !Modifier .isStatic (modifiers ) && !Modifier .isTransient (modifiers )) {
3016
3111
Type type = fieldDeclaration .getType ();
3017
3112
int dims = 0 ;
@@ -3044,6 +3139,22 @@ private String prepareSimpleSerializable(TypeDeclaration node, List bodyDeclarat
3044
3139
ITypeBinding resolveBinding = type .resolveBinding ();
3045
3140
if ("java.lang.String" .equals (resolveBinding .getQualifiedName ())) {
3046
3141
mark = "s" ;
3142
+ } else {
3143
+ ITypeBinding t = resolveBinding ;
3144
+ do {
3145
+ String typeName = t .getQualifiedName ();
3146
+ if ("java.lang.Object" .equals (typeName )) {
3147
+ break ;
3148
+ }
3149
+ if ("net.sf.j2s.ajax.SimpleSerializable" .equals (typeName )) {
3150
+ mark = "O" ;
3151
+ break ;
3152
+ }
3153
+ t = t .getSuperclass ();
3154
+ if (t == null ) {
3155
+ break ;
3156
+ }
3157
+ } while (true );
3047
3158
}
3048
3159
if (mark != null ) {
3049
3160
for (Iterator xiter = fragments .iterator (); xiter .hasNext ();) {
0 commit comments