Skip to content

Commit d5b8801

Browse files
committed
fix for(char c : Character[] ca) and related conversions
1 parent feda0b8 commit d5b8801

File tree

6 files changed

+67
-28
lines changed

6 files changed

+67
-28
lines changed
295 Bytes
Binary file not shown.
295 Bytes
Binary file not shown.

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/Java2ScriptVisitor.java

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ? ")" : "");
85 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
// Google closure compiler cannot handle Clazz.new or Clazz.super
99

10+
// BH 7/2/2018 12:50:55 PM Character.objectValue(), for enhanced FOR in transpiler
1011
// BH 6/29/2018 10:13:51 AM array.equals$O, fixes array.clone
1112
// BH 6/28/2018 7:34:58 AM fix for array.clone not copying array in the case of objects
1213
// BH 6/27/2018 3:11:50 PM fix for class String not indicating its name
@@ -3253,14 +3254,6 @@ Boolean = java.lang.Boolean = Boolean || function(){
32533254
if (typeof arguments[0] != "object")this.c$(arguments[0]);
32543255
});
32553256

3256-
Integer.prototype.objectValue =
3257-
Short.prototype.objectValue =
3258-
Long.prototype.objectValue =
3259-
Float.prototype.objectValue =
3260-
Double.prototype.objectValue = function() {return this.valueOf()};
3261-
3262-
3263-
32643257
//if (supportsNativeObject) {
32653258
extendObject(Boolean);
32663259
//}
@@ -3975,6 +3968,7 @@ if (typeof arguments[0] != "object")this.c$(arguments[0]);
39753968
Clazz._setDeclared("java.lang.Character", java.lang.Character);
39763969
setJ2STypeclass(Character, "char", "C");
39773970

3971+
39783972
m$(C$,"getName",
39793973
function(){
39803974
return "?";
@@ -4122,6 +4116,16 @@ m$(C$,"charCount", function(codePoint){
41224116
return codePoint >= 0x010000 ? 2 : 1;
41234117
}, 1);
41244118

4119+
4120+
Integer.prototype.objectValue =
4121+
Short.prototype.objectValue =
4122+
Long.prototype.objectValue =
4123+
Float.prototype.objectValue =
4124+
Double.prototype.objectValue = function() {return this.valueOf()};
4125+
Character.prototype.objectValue = function() { return this.value }
4126+
4127+
4128+
41254129
// TODO: Only asking for problems declaring Date. This is not necessary
41264130

41274131
Clazz._setDeclared("java.util.Date", java.util.Date=Date);

sources/net.sf.j2s.java.core/srcjs/swingjs2.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13091,6 +13091,7 @@ J2S._getResourcePath = function(path, isJavaPath) {
1309113091

1309213092
// Google closure compiler cannot handle Clazz.new or Clazz.super
1309313093

13094+
// BH 7/2/2018 12:50:55 PM Character.objectValue(), for enhanced FOR in transpiler
1309413095
// BH 6/29/2018 10:13:51 AM array.equals$O, fixes array.clone
1309513096
// BH 6/28/2018 7:34:58 AM fix for array.clone not copying array in the case of objects
1309613097
// BH 6/27/2018 3:11:50 PM fix for class String not indicating its name
@@ -16337,14 +16338,6 @@ Boolean = java.lang.Boolean = Boolean || function(){
1633716338
if (typeof arguments[0] != "object")this.c$(arguments[0]);
1633816339
});
1633916340

16340-
Integer.prototype.objectValue =
16341-
Short.prototype.objectValue =
16342-
Long.prototype.objectValue =
16343-
Float.prototype.objectValue =
16344-
Double.prototype.objectValue = function() {return this.valueOf()};
16345-
16346-
16347-
1634816341
//if (supportsNativeObject) {
1634916342
extendObject(Boolean);
1635016343
//}
@@ -17059,6 +17052,7 @@ if (typeof arguments[0] != "object")this.c$(arguments[0]);
1705917052
Clazz._setDeclared("java.lang.Character", java.lang.Character);
1706017053
setJ2STypeclass(Character, "char", "C");
1706117054

17055+
1706217056
m$(C$,"getName",
1706317057
function(){
1706417058
return "?";
@@ -17206,6 +17200,16 @@ m$(C$,"charCount", function(codePoint){
1720617200
return codePoint >= 0x010000 ? 2 : 1;
1720717201
}, 1);
1720817202

17203+
17204+
Integer.prototype.objectValue =
17205+
Short.prototype.objectValue =
17206+
Long.prototype.objectValue =
17207+
Float.prototype.objectValue =
17208+
Double.prototype.objectValue = function() {return this.valueOf()};
17209+
Character.prototype.objectValue = function() { return this.value }
17210+
17211+
17212+
1720917213
// TODO: Only asking for problems declaring Date. This is not necessary
1721017214

1721117215
Clazz._setDeclared("java.util.Date", java.util.Date=Date);

0 commit comments

Comments
 (0)