19
19
import org .eclipse .jdt .core .dom .ArrayType ;
20
20
import org .eclipse .jdt .core .dom .Block ;
21
21
import org .eclipse .jdt .core .dom .CastExpression ;
22
+ import org .eclipse .jdt .core .dom .CharacterLiteral ;
22
23
import org .eclipse .jdt .core .dom .ClassInstanceCreation ;
23
24
import org .eclipse .jdt .core .dom .ConstructorInvocation ;
24
25
import org .eclipse .jdt .core .dom .EnumConstantDeclaration ;
@@ -434,8 +435,9 @@ public boolean visit(CastExpression node) {
434
435
* TODO: some casting should have its meaning!
435
436
* int to byte, int to short, long to int will lost values
436
437
*/
438
+ Expression expression = node .getExpression ();
437
439
if (type .isPrimitiveType ()) {
438
- ITypeBinding resolveTypeBinding = node . getExpression () .resolveTypeBinding ();
440
+ ITypeBinding resolveTypeBinding = expression .resolveTypeBinding ();
439
441
if (resolveTypeBinding != null ){
440
442
String name = resolveTypeBinding .getName ();
441
443
PrimitiveType pType = (PrimitiveType ) type ;
@@ -445,12 +447,12 @@ public boolean visit(CastExpression node) {
445
447
|| pType .getPrimitiveTypeCode () == PrimitiveType .LONG ) {
446
448
if ("char" .equals (name )) {
447
449
buffer .append ("(" );
448
- node . getExpression () .accept (this );
450
+ expression .accept (this );
449
451
buffer .append (").charCodeAt (0)" );
450
452
return false ;
451
453
} else if ("float" .equals (name ) || "double" .equals (name )) {
452
454
buffer .append ("Math.round (" );
453
- node . getExpression () .accept (this );
455
+ expression .accept (this );
454
456
buffer .append (")" );
455
457
return false ;
456
458
}
@@ -465,22 +467,46 @@ public boolean visit(CastExpression node) {
465
467
// TODO:
466
468
buffer .append ("String.fromCharCode (" );
467
469
buffer .append ("Math.round (" );
468
- node . getExpression () .accept (this );
470
+ expression .accept (this );
469
471
buffer .append (")" );
470
472
buffer .append (")" );
471
473
return false ;
472
474
} else if ("int" .equals (name ) || "byte" .equals (name )
473
- || "double" .equals (name ) || "float" .equals (name )
475
+ // || "double".equals(name) || "float".equals(name)
474
476
|| "short" .equals (name ) || "long" .equals (name )) {
477
+ Object constantValue = expression .resolveConstantExpressionValue ();
478
+ if (constantValue != null ) {
479
+ if (constantValue instanceof Integer ) {
480
+ int value = ((Integer ) constantValue ).intValue ();
481
+ if ((value >= '0' && value <= '9' )
482
+ || (value >= 'A' && value <= 'Z' )
483
+ || (value >= 'a' && value <= 'z' )) {
484
+ buffer .append ('\'' );
485
+ buffer .append ((char ) value );
486
+ buffer .append ('\'' );
487
+ return false ;
488
+ }
489
+ } else if (constantValue instanceof Long ) {
490
+ long value = ((Long ) constantValue ).longValue ();
491
+ if ((value >= '0' && value <= '9' )
492
+ || (value >= 'A' && value <= 'Z' )
493
+ || (value >= 'a' && value <= 'z' )) {
494
+ buffer .append ('\'' );
495
+ buffer .append ((char ) value );
496
+ buffer .append ('\'' );
497
+ return false ;
498
+ }
499
+ }
500
+ }
475
501
buffer .append ("String.fromCharCode (" );
476
- node . getExpression () .accept (this );
502
+ expression .accept (this );
477
503
buffer .append (")" );
478
504
return false ;
479
505
}
480
506
}
481
507
}
482
508
}
483
- node . getExpression () .accept (this );
509
+ expression .accept (this );
484
510
return false ;
485
511
}
486
512
@@ -1295,9 +1321,17 @@ private void charVisit(ASTNode node, boolean beCare) {
1295
1321
}
1296
1322
ITypeBinding binding = ((Expression ) node ).resolveTypeBinding ();
1297
1323
if (binding .isPrimitive () && "char" .equals (binding .getName ())) {
1298
- buffer .append ("(" );
1299
- boxingNode (node );
1300
- buffer .append (").charCodeAt (0)" );
1324
+ if (node instanceof CharacterLiteral ) {
1325
+ CharacterLiteral cl = (CharacterLiteral ) node ;
1326
+ buffer .append (0 + cl .charValue ());
1327
+ } else if (node instanceof SimpleName || node instanceof QualifiedName ) {
1328
+ boxingNode (node );
1329
+ buffer .append (".charCodeAt (0)" );
1330
+ } else {
1331
+ buffer .append ("(" );
1332
+ boxingNode (node );
1333
+ buffer .append (").charCodeAt (0)" );
1334
+ }
1301
1335
} else {
1302
1336
boxingNode (node );
1303
1337
}
@@ -1316,11 +1350,25 @@ public boolean visit(InfixExpression node) {
1316
1350
}
1317
1351
String operator = node .getOperator ().toString ();
1318
1352
Expression left = node .getLeftOperand ();
1353
+ Expression right = node .getRightOperand ();
1319
1354
ITypeBinding typeBinding = left .resolveTypeBinding ();
1355
+
1356
+ if ((left instanceof SimpleName || left instanceof CharacterLiteral ) && (right instanceof SimpleName || right instanceof CharacterLiteral )
1357
+ && (">" .equals (operator ) || "<" .equals (operator ) || ">=" .equals (operator ) || "<=" .equals (operator ))) {
1358
+ ITypeBinding rightBinding = right .resolveTypeBinding ();
1359
+ if (typeBinding .isPrimitive () && "char" .equals (typeBinding .getName ())
1360
+ && rightBinding .isPrimitive () && "char" .equals (rightBinding .getName ())) {
1361
+ boxingNode (left );
1362
+ buffer .append (' ' );
1363
+ buffer .append (operator );
1364
+ buffer .append (' ' );
1365
+ boxingNode (right );
1366
+ return false ;
1367
+ }
1368
+ }
1320
1369
if ("/" .equals (operator )) {
1321
1370
if (typeBinding != null && typeBinding .isPrimitive ()) {
1322
1371
if (isIntegerType (typeBinding .getName ())) {
1323
- Expression right = node .getRightOperand ();
1324
1372
ITypeBinding rightTypeBinding = right .resolveTypeBinding ();
1325
1373
if (isIntegerType (rightTypeBinding .getName ())) {
1326
1374
StringBuffer tmpBuffer = buffer ;
@@ -1375,20 +1423,20 @@ public boolean visit(InfixExpression node) {
1375
1423
}
1376
1424
}
1377
1425
1378
- charVisit (node . getLeftOperand () , beCare );
1426
+ charVisit (left , beCare );
1379
1427
buffer .append (' ' );
1380
1428
buffer .append (operator );
1381
1429
if ("==" .equals (operator ) || "!=" .equals (operator )) {
1382
1430
if (typeBinding != null && !typeBinding .isPrimitive ()
1383
- && !(node . getLeftOperand () instanceof NullLiteral )
1384
- && !(node . getRightOperand () instanceof NullLiteral )
1431
+ && !(left instanceof NullLiteral )
1432
+ && !(right instanceof NullLiteral )
1385
1433
/*&& !(node.getLeftOperand() instanceof StringLiteral) // "abc" == ...
1386
1434
&& !(node.getRightOperand() instanceof StringLiteral)*/ ) {
1387
1435
buffer .append ('=' );
1388
1436
}
1389
1437
}
1390
1438
buffer .append (' ' );
1391
- charVisit (node . getRightOperand () , beCare );
1439
+ charVisit (right , beCare );
1392
1440
List extendedOperands = node .extendedOperands ();
1393
1441
if (extendedOperands .size () > 0 ) {
1394
1442
for (Iterator iter = extendedOperands .iterator (); iter .hasNext ();) {
@@ -1742,6 +1790,8 @@ public boolean visit(MethodDeclaration node) {
1742
1790
PrimitiveType pType = (PrimitiveType ) type ;
1743
1791
if (pType .getPrimitiveTypeCode () == PrimitiveType .BOOLEAN ) {
1744
1792
buffer .append ("~B" ); // Boolean
1793
+ } else if (pType .getPrimitiveTypeCode () == PrimitiveType .CHAR ) {
1794
+ buffer .append ("~S" ); // String for char
1745
1795
} else {
1746
1796
buffer .append ("~N" ); // Number
1747
1797
}
0 commit comments