1919import org .eclipse .jdt .core .dom .ArrayType ;
2020import org .eclipse .jdt .core .dom .Block ;
2121import org .eclipse .jdt .core .dom .CastExpression ;
22+ import org .eclipse .jdt .core .dom .CharacterLiteral ;
2223import org .eclipse .jdt .core .dom .ClassInstanceCreation ;
2324import org .eclipse .jdt .core .dom .ConstructorInvocation ;
2425import org .eclipse .jdt .core .dom .EnumConstantDeclaration ;
@@ -434,8 +435,9 @@ public boolean visit(CastExpression node) {
434435 * TODO: some casting should have its meaning!
435436 * int to byte, int to short, long to int will lost values
436437 */
438+ Expression expression = node .getExpression ();
437439 if (type .isPrimitiveType ()) {
438- ITypeBinding resolveTypeBinding = node . getExpression () .resolveTypeBinding ();
440+ ITypeBinding resolveTypeBinding = expression .resolveTypeBinding ();
439441 if (resolveTypeBinding != null ){
440442 String name = resolveTypeBinding .getName ();
441443 PrimitiveType pType = (PrimitiveType ) type ;
@@ -445,12 +447,12 @@ public boolean visit(CastExpression node) {
445447 || pType .getPrimitiveTypeCode () == PrimitiveType .LONG ) {
446448 if ("char" .equals (name )) {
447449 buffer .append ("(" );
448- node . getExpression () .accept (this );
450+ expression .accept (this );
449451 buffer .append (").charCodeAt (0)" );
450452 return false ;
451453 } else if ("float" .equals (name ) || "double" .equals (name )) {
452454 buffer .append ("Math.round (" );
453- node . getExpression () .accept (this );
455+ expression .accept (this );
454456 buffer .append (")" );
455457 return false ;
456458 }
@@ -465,22 +467,46 @@ public boolean visit(CastExpression node) {
465467 // TODO:
466468 buffer .append ("String.fromCharCode (" );
467469 buffer .append ("Math.round (" );
468- node . getExpression () .accept (this );
470+ expression .accept (this );
469471 buffer .append (")" );
470472 buffer .append (")" );
471473 return false ;
472474 } else if ("int" .equals (name ) || "byte" .equals (name )
473- || "double" .equals (name ) || "float" .equals (name )
475+ // || "double".equals(name) || "float".equals(name)
474476 || "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+ }
475501 buffer .append ("String.fromCharCode (" );
476- node . getExpression () .accept (this );
502+ expression .accept (this );
477503 buffer .append (")" );
478504 return false ;
479505 }
480506 }
481507 }
482508 }
483- node . getExpression () .accept (this );
509+ expression .accept (this );
484510 return false ;
485511 }
486512
@@ -1295,9 +1321,17 @@ private void charVisit(ASTNode node, boolean beCare) {
12951321 }
12961322 ITypeBinding binding = ((Expression ) node ).resolveTypeBinding ();
12971323 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+ }
13011335 } else {
13021336 boxingNode (node );
13031337 }
@@ -1316,11 +1350,25 @@ public boolean visit(InfixExpression node) {
13161350 }
13171351 String operator = node .getOperator ().toString ();
13181352 Expression left = node .getLeftOperand ();
1353+ Expression right = node .getRightOperand ();
13191354 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+ }
13201369 if ("/" .equals (operator )) {
13211370 if (typeBinding != null && typeBinding .isPrimitive ()) {
13221371 if (isIntegerType (typeBinding .getName ())) {
1323- Expression right = node .getRightOperand ();
13241372 ITypeBinding rightTypeBinding = right .resolveTypeBinding ();
13251373 if (isIntegerType (rightTypeBinding .getName ())) {
13261374 StringBuffer tmpBuffer = buffer ;
@@ -1375,20 +1423,20 @@ public boolean visit(InfixExpression node) {
13751423 }
13761424 }
13771425
1378- charVisit (node . getLeftOperand () , beCare );
1426+ charVisit (left , beCare );
13791427 buffer .append (' ' );
13801428 buffer .append (operator );
13811429 if ("==" .equals (operator ) || "!=" .equals (operator )) {
13821430 if (typeBinding != null && !typeBinding .isPrimitive ()
1383- && !(node . getLeftOperand () instanceof NullLiteral )
1384- && !(node . getRightOperand () instanceof NullLiteral )
1431+ && !(left instanceof NullLiteral )
1432+ && !(right instanceof NullLiteral )
13851433 /*&& !(node.getLeftOperand() instanceof StringLiteral) // "abc" == ...
13861434 && !(node.getRightOperand() instanceof StringLiteral)*/ ) {
13871435 buffer .append ('=' );
13881436 }
13891437 }
13901438 buffer .append (' ' );
1391- charVisit (node . getRightOperand () , beCare );
1439+ charVisit (right , beCare );
13921440 List extendedOperands = node .extendedOperands ();
13931441 if (extendedOperands .size () > 0 ) {
13941442 for (Iterator iter = extendedOperands .iterator (); iter .hasNext ();) {
@@ -1742,6 +1790,8 @@ public boolean visit(MethodDeclaration node) {
17421790 PrimitiveType pType = (PrimitiveType ) type ;
17431791 if (pType .getPrimitiveTypeCode () == PrimitiveType .BOOLEAN ) {
17441792 buffer .append ("~B" ); // Boolean
1793+ } else if (pType .getPrimitiveTypeCode () == PrimitiveType .CHAR ) {
1794+ buffer .append ("~S" ); // String for char
17451795 } else {
17461796 buffer .append ("~N" ); // Number
17471797 }
0 commit comments