@@ -501,27 +501,59 @@ public boolean visit(EmptyStatement node) {
501501 public boolean visit (EnhancedForStatement node ) {
502502 SimpleName name = node .getParameter ().getName ();
503503 String varName = getQualifiedSimpleName (name );
504+ ITypeBinding vtype = name .resolveTypeBinding ();
504505 buffer .append ("for (var " );
505506 acceptVariableFinal (name , 1 );
506- writeReplaceV (", $V = " , "V" , varName );
507+ writeReplaceV (", $V = " , "V" , varName , null , null );
507508 Expression exp = node .getExpression ();
508- ITypeBinding typeBinding = exp .resolveTypeBinding ();
509- if (typeBinding .isArray ()) {
510- writeReplaceV ("0, $$V = " , "V" , varName );
509+ ITypeBinding eType = exp .resolveTypeBinding ();
510+ ITypeBinding arrayType =eType .getComponentType ();
511+
512+ if (arrayType == null ) {
511513 exp .accept (this );
512- writeReplaceV ("; $V<$$V.length&&((V=$$V[$V]),1);$V++" , "V" , varName );
514+ writeReplaceV (".iterator(); $V.hasNext()&&((V=" , "V" , varName , null , null );
515+ writeReplaceV ("($V.next())" , "V" , varName , vtype , eType );
516+ writeReplaceV ("),1);" , "V" , varName , null , null );
513517 } else {
518+ writeReplaceV ("0, $$V = " , "V" , varName , null , null );
514519 exp .accept (this );
515- writeReplaceV (".iterator(); $V.hasNext()&&((V=$V.next()),1);" , "V" , varName );
520+ writeReplaceV ("; $V<$$V.length&&((V=" , "V" , varName , null , null );
521+ writeReplaceV ("($$V[$V])" , "V" , varName , vtype , arrayType );
522+ writeReplaceV ("),1);$V++" , "V" , varName , null , null );
516523 }
517524 buffer .append (") " );
518525 node .getBody ().accept (this );
519526 buffer .append ("\r \n " );
520527 return false ;
521528 }
522529
523- private void writeReplaceV (String template , String v , String varName ) {
524- buffer .append (template .replace (v , varName ));
530+ /**
531+ * allow for primitive boxing or unboxing. See test.Test_Chars.java
532+ * @param template
533+ * @param v
534+ * @param varName
535+ * @param vType
536+ * @param eType
537+ */
538+ private void writeReplaceV (String template , String v , String varName , ITypeBinding vType , ITypeBinding eType ) {
539+ String s = template .replace (v , varName );
540+ if (vType != eType ) {
541+ if (!eType .isPrimitive ()) {
542+ // So we know the expression is boxed -- Character, for example
543+ // Character does not use .objectValue, but we implement it here
544+ s += ".objectValue()" ;
545+ if (vType .getName ().equals ("int" ))
546+ s += CHARCODEAT0 ;
547+ } else if (!vType .isPrimitive ()) {
548+ // So we know the expression is unboxed -- char, for example
549+ // but it could be Character to int
550+ s = "new " + getPrimitiveTYPE (eType .getName ()) + s ;
551+ } else {
552+ // this is a conversion of an char[] to an int -- the only possibility allowed, I think
553+ s += CHARCODEAT0 ;
554+ }
555+ }
556+ buffer .append (s );
525557 }
526558
527559 public boolean visit (EnumDeclaration node ) {
@@ -1889,7 +1921,7 @@ private static boolean isObjectOrNull(ITypeBinding type) {
18891921 private static final String getPrimitiveTYPE (String name ) {
18901922 int pt = primitiveTypeEquivalents .indexOf (name .substring (1 )) - 1 ;
18911923 String type = primitiveTypeEquivalents .substring (pt );
1892- return type .substring (0 , type .indexOf ("," )) + ".TYPE" ;
1924+ return type .substring (0 , type .indexOf ("," ));
18931925 }
18941926
18951927 private boolean isArray = false ;
@@ -2704,7 +2736,6 @@ public boolean visit(QualifiedName node) {
27042736 if (varBinding != null ) {
27052737 if (qualifier instanceof SimpleName ) {
27062738 addQualifiedNameFromBinding (varBinding , false );
2707- // buffer.append("<qsn<");
27082739 } else {
27092740 buffer .append ('(' );
27102741 if (className == null )
@@ -2963,7 +2994,7 @@ public boolean visit(TypeLiteral node) {
29632994 ITypeBinding binding = type .resolveBinding ();
29642995 if (type .isPrimitiveType ()) {
29652996 // adds Integer.TYPE, Float.TYPE, etc.
2966- buffer .append (getPrimitiveTYPE (binding .getName ()));
2997+ buffer .append (getPrimitiveTYPE (binding .getName ()) + ".TYPE" );
29672998 } else if (type instanceof ArrayType ) {
29682999 buffer .append (clazzArray (binding , ARRAY_CLASS_LITERAL ));
29693000 } else {
@@ -3216,7 +3247,7 @@ private String clazzArray(ITypeBinding type, int dimFlag) {
32163247 ITypeBinding ebinding = type .getElementType ();
32173248 if (ebinding == null )
32183249 ebinding = type ; // creating for Enum
3219- String params = (ebinding .isPrimitive () ? getPrimitiveTYPE (ebinding .getName ())
3250+ String params = (ebinding .isPrimitive () ? getPrimitiveTYPE (ebinding .getName ()) + ".TYPE"
32203251 : getQualifiedStaticName (null , ebinding .getQualifiedName (), true , true , false ))
32213252 + (dimFlag == ARRAY_DIM_ONLY ? "" : ", " + (Math .abs (dimFlag ) * type .getDimensions () * -1 ));
32223253 return "Clazz.array(" + params + (dimFlag > 0 ? ")" : "" );
0 commit comments