@@ -150,11 +150,31 @@ protected void visitList(List list, String seperator, int begin, int end) {
150150 public boolean visit (ArrayAccess node ) {
151151 node .getArray ().accept (this );
152152 buffer .append ('[' );
153+ int idx1 = buffer .length ();
153154 Expression index = node .getIndex ();
154155 index .accept (this );
155156 ITypeBinding rightTypeBinding = index .resolveTypeBinding ();
156157 if (rightTypeBinding != null && "char" .equals (rightTypeBinding .getName ())) {
157- buffer .append (".charCodeAt (0)" );
158+ boolean appendingCode = true ;
159+ int length = buffer .length ();
160+ if (index instanceof MethodInvocation ) {
161+ MethodInvocation m = (MethodInvocation ) index ;
162+ if ("charAt" .equals (m .getName ().toString ())) {
163+ int idx2 = buffer .indexOf (".charAt " , idx1 );
164+ if (idx2 != -1 ) {
165+ StringBuffer newMethodBuffer = new StringBuffer ();
166+ newMethodBuffer .append (buffer .substring (idx1 , idx2 ));
167+ newMethodBuffer .append (".charCodeAt " );
168+ newMethodBuffer .append (buffer .substring (idx2 + 8 , length ));
169+ buffer .delete (idx1 , length );
170+ buffer .append (newMethodBuffer .toString ());
171+ appendingCode = false ;
172+ }
173+ }
174+ }
175+ if (appendingCode ) {
176+ buffer .append (".charCodeAt (0)" );
177+ }
158178 }
159179 buffer .append (']' );
160180 return false ;
@@ -179,15 +199,21 @@ public boolean visit(ArrayCreation node) {
179199 || "byte" .equals (typeCode )
180200 || "long" .equals (typeCode )
181201 || "short" .equals (typeCode )) {
182- buffer .append (" Clazz.newArray (" );
202+ //buffer.append(" Clazz.newArray (");
203+ buffer .append (" Clazz.new" );
204+ buffer .append (typeCode .substring (0 , 1 ).toUpperCase ());
205+ buffer .append (typeCode .substring (1 ));
206+ buffer .append ("Array (" );
183207 visitList (dim , ", " );
184208 buffer .append (", 0)" );
185209 } else if ("char" .equals (typeCode )) {
186- buffer .append (" Clazz.newArray (" );
210+ //buffer.append(" Clazz.newArray (");
211+ buffer .append (" Clazz.newCharArray (" );
187212 visitList (dim , ", " );
188213 buffer .append (", '\\ 0')" );
189214 } else if ("boolean" .equals (typeCode )) {
190- buffer .append (" Clazz.newArray (" );
215+ //buffer.append(" Clazz.newArray (");
216+ buffer .append (" Clazz.newBooleanArray (" );
191217 visitList (dim , ", " );
192218 buffer .append (", false)" );
193219 } else {
@@ -386,31 +412,47 @@ public boolean visit(Assignment node) {
386412 * FIXME: Bug here!: v[count++] = 'a';
387413 */
388414 left .accept (this );
389- buffer .append (" = String.fromCharCode (" );
390- if (isMixedOp ) {
391- buffer .append ("(" );
392- left .accept (this );
393- buffer .append (").charCodeAt (0) " );
394- buffer .append (op .charAt (0 ));
395- }
396- if (right instanceof InfixExpression ) {
397- buffer .append (" (" );
415+ if ("char" .equals (rightTypeBinding .getName ())) {
416+ buffer .append (" = " );
398417 right .accept (this );
399- buffer .append (')' );
400- if ("char" .equals (rightTypeBinding .getName ())) {
401- buffer .append (".charCodeAt (0)" );
402- }
403418 } else {
419+ buffer .append (" = String.fromCharCode (" );
420+ if (isMixedOp ) {
421+ if (left instanceof SimpleName || left instanceof QualifiedName ) {
422+ left .accept (this );
423+ } else {
424+ buffer .append ("(" );
425+ left .accept (this );
426+ buffer .append (")" );
427+ }
428+ buffer .append (".charCodeAt (0) " );
429+ buffer .append (op .charAt (0 ));
430+ }
404431 buffer .append (' ' );
405- if ("char" .equals (rightTypeBinding .getName ())) {
406- buffer .append (" (" );
407- right .accept (this );
408- buffer .append (").charCodeAt (0)" );
432+ if (right instanceof InfixExpression ) {
433+ String constValue = checkConstantValue (right );
434+ if (constValue != null ) {
435+ buffer .append (constValue );
436+ } else {
437+ buffer .append ("(" );
438+ right .accept (this );
439+ buffer .append (')' );
440+ }
441+ if ("char" .equals (rightTypeBinding .getName ())) {
442+ buffer .append (".charCodeAt (0)" );
443+ }
409444 } else {
410- right .accept (this );
445+ if ("char" .equals (rightTypeBinding .getName ())) {
446+ buffer .append ("(" );
447+ right .accept (this );
448+ buffer .append (")" );
449+ buffer .append (".charCodeAt (0)" );
450+ } else {
451+ right .accept (this );
452+ }
411453 }
454+ buffer .append (')' );
412455 }
413- buffer .append (')' );
414456 return false ;
415457 }
416458 }
@@ -420,9 +462,46 @@ public boolean visit(Assignment node) {
420462 buffer .append (' ' );
421463 ITypeBinding binding = right .resolveTypeBinding ();
422464 if (binding != null && "char" .equals (binding .getName ())) {
423- buffer .append ('(' );
424- right .accept (this );
425- buffer .append (").charCodeAt (0)" );
465+ String typeBindingName = (typeBinding != null ) ? typeBinding .getName () : null ;
466+ if (right instanceof CharacterLiteral ) {
467+ CharacterLiteral cl = (CharacterLiteral ) right ;
468+ if ("char" .equals (typeBindingName ) || typeBindingName .indexOf ("String" ) != -1 ) {
469+ String constValue = checkConstantValue (right );
470+ buffer .append (constValue );
471+ } else {
472+ buffer .append (0 + cl .charValue ());
473+ }
474+ } else {
475+ if (typeBindingName != null && ("char" .equals (typeBindingName ) || typeBindingName .indexOf ("String" ) != -1 )) {
476+ right .accept (this );
477+ } else {
478+ int idx1 = buffer .length ();
479+ buffer .append ('(' );
480+ right .accept (this );
481+ buffer .append (")" );
482+
483+ boolean appendingCode = true ;
484+ int length = buffer .length ();
485+ if (right instanceof MethodInvocation ) {
486+ MethodInvocation m = (MethodInvocation ) right ;
487+ if ("charAt" .equals (m .getName ().toString ())) {
488+ int idx2 = buffer .indexOf (".charAt " , idx1 );
489+ if (idx2 != -1 ) {
490+ StringBuffer newMethodBuffer = new StringBuffer ();
491+ newMethodBuffer .append (buffer .substring (idx1 + 1 , idx2 ));
492+ newMethodBuffer .append (".charCodeAt " );
493+ newMethodBuffer .append (buffer .substring (idx2 + 8 , length - 1 ));
494+ buffer .delete (idx1 , length );
495+ buffer .append (newMethodBuffer .toString ());
496+ appendingCode = false ;
497+ }
498+ }
499+ }
500+ if (appendingCode ) {
501+ buffer .append (".charCodeAt (0)" );
502+ }
503+ }
504+ }
426505 } else {
427506 boxingNode (right );
428507 }
@@ -520,15 +599,16 @@ public boolean visit(ConditionalExpression node) {
520599 }
521600
522601 public boolean visit (ContinueStatement node ) {
523- buffer .append ("continue " );
602+ buffer .append ("continue" );
524603 /*
525604 * TODO: verify that label is not supported!
526605 */
527606 SimpleName label = node .getLabel ();
528607 if (label != null ) {
608+ buffer .append (' ' );
529609 label .accept (this );
530610 }
531- buffer .append (";" );
611+ buffer .append (";\r \n " );
532612 return false ;
533613 }
534614
@@ -1076,9 +1156,10 @@ public boolean visit(QualifiedName node) {
10761156 }
10771157
10781158 public boolean visit (ReturnStatement node ) {
1079- buffer .append ("return " );
1159+ buffer .append ("return" );
10801160 Expression expression = node .getExpression ();
10811161 if (expression != null ) {
1162+ buffer .append (' ' );
10821163 expression .accept (this );
10831164 }
10841165 buffer .append (";\r \n " );
@@ -1227,11 +1308,43 @@ public boolean visit(VariableDeclarationFragment node) {
12271308 if (typeBinding != null && "char" .equals (typeBinding .getName ())) {
12281309 ITypeBinding nameTypeBinding = name .resolveTypeBinding ();
12291310 String nameType = nameTypeBinding .getName ();
1230- if (!"char" .equals (nameType ) && nameType .indexOf ("String" ) == -1 ) {
1231- buffer .append ("(" );
1232- initializer .accept (this );
1233- buffer .append (").charCodeAt (0)" );
1311+ if (initializer instanceof CharacterLiteral ) {
1312+ CharacterLiteral cl = (CharacterLiteral ) initializer ;
1313+ if ("char" .equals (nameType )) {
1314+ String constValue = checkConstantValue (initializer );
1315+ buffer .append (constValue );
1316+ } else {
1317+ buffer .append (0 + cl .charValue ());
1318+ }
12341319 return false ;
1320+ } else {
1321+ if (nameType != null && !"char" .equals (nameType ) && nameType .indexOf ("String" ) == -1 ) {
1322+ int idx1 = buffer .length ();
1323+ buffer .append ("(" );
1324+ initializer .accept (this );
1325+ buffer .append (")" );
1326+ boolean appendingCode = true ;
1327+ int length = buffer .length ();
1328+ if (initializer instanceof MethodInvocation ) {
1329+ MethodInvocation m = (MethodInvocation ) initializer ;
1330+ if ("charAt" .equals (m .getName ().toString ())) {
1331+ int idx2 = buffer .indexOf (".charAt " , idx1 );
1332+ if (idx2 != -1 ) {
1333+ StringBuffer newMethodBuffer = new StringBuffer ();
1334+ newMethodBuffer .append (buffer .substring (idx1 + 1 , idx2 ));
1335+ newMethodBuffer .append (".charCodeAt " );
1336+ newMethodBuffer .append (buffer .substring (idx2 + 8 , length - 1 ));
1337+ buffer .delete (idx1 , length );
1338+ buffer .append (newMethodBuffer .toString ());
1339+ appendingCode = false ;
1340+ }
1341+ }
1342+ }
1343+ if (appendingCode ) {
1344+ buffer .append (".charCodeAt (0)" );
1345+ }
1346+ return false ;
1347+ }
12351348 }
12361349 }
12371350 ITypeBinding nameTypeBinding = name .resolveTypeBinding ();
0 commit comments