|
31 | 31 | // TODO: superclass inheritance for JAXB XmlAccessorType |
32 | 32 | // TODO: Transpiler bug allows static String name, but JavaScript function().name is read-only and will be "clazz" |
33 | 33 |
|
34 | | -//BH 2024.02.22 -- 3.3.1-v7 fixes long extension issue causing MutableBitInteger to miscalculate subtraction(no change in version #) |
| 34 | +//BH 2024.07.14 -- 5.0.1-v4 fixes numerical array initializer using characters ['a','b',...], but not new int[] { "test".charAt(3) } |
| 35 | +//BH 2024.02.22 -- 5.0.1-v3 fixes long extension issue causing MutableBitInteger to miscalculate subtraction(no change in version #) |
| 36 | +//BH 2023.11.27 -- 5.0.1-v2 final refactoring and creatiton of J2SUtil |
| 37 | +//BH 2023.11.21 -- 5.0.1-v2 adds Java8 syntaxes for try and switch; removes dependency for instanceOf and exception checking |
| 38 | +//BH 2023.11.09 -- 5.0.1-v1 merges Jmol legacy (.j2sjmol) with Java8//11 (.j2s) |
35 | 39 | //BH 2023.03.29 -- 3.3.1-v7 fixes outer static method call from within lambda expression. |
36 | 40 | //BH 2023.02.09 -- 3.3.1.v6 fixes j2s.excluded.paths needing /src/xxxx |
37 | 41 | //BH 2022.06.27 -- 3.3.1-v5 fixes missing method annotations |
@@ -3015,15 +3019,63 @@ private void addArrayConstructor(ITypeBinding binding, List<ASTNode> dim) { |
3015 | 3019 |
|
3016 | 3020 | public boolean visit(ArrayInitializer node) { |
3017 | 3021 | // as in: public String[] d = {"1", "2"}; |
3018 | | - buffer.append(clazzArray(node.resolveTypeBinding(), ARRAY_INITIALIZED)); |
| 3022 | + ITypeBinding type = node.resolveTypeBinding(); |
| 3023 | + buffer.append(clazzArray(type, ARRAY_INITIALIZED)); |
3019 | 3024 | buffer.append(", ["); |
| 3025 | + int toChar = toCharType(type); |
3020 | 3026 | @SuppressWarnings("unchecked") |
3021 | | - List<ASTNode> expressions = node.expressions(); |
3022 | | - visitList(expressions, ", "); |
| 3027 | + List<ASTNode> ex = node.expressions(); |
| 3028 | + if (toChar == 0) { |
| 3029 | + visitList(ex, ", "); |
| 3030 | + } else { |
| 3031 | + fixCharArrayInit(buffer, ex, toChar); |
| 3032 | + } |
3023 | 3033 | buffer.append("])"); |
3024 | 3034 | return false; |
3025 | 3035 | } |
3026 | 3036 |
|
| 3037 | + private static int toCharType(ITypeBinding type) { |
| 3038 | + switch (type == null ? "" : type.getName()) { |
| 3039 | + case "char[]": |
| 3040 | + return 1; |
| 3041 | + case "byte[]": |
| 3042 | + case "short[]": |
| 3043 | + case "int[]": |
| 3044 | + case "long[]": |
| 3045 | + case "float[]": |
| 3046 | + case "double[]": |
| 3047 | + return -1; |
| 3048 | + default: |
| 3049 | + return 0; |
| 3050 | + } |
| 3051 | + } |
| 3052 | + |
| 3053 | + private void fixCharArrayInit(StringBuffer buffer, List<ASTNode> list, int toChar) { |
| 3054 | + for (Iterator<ASTNode> iter = list.iterator(); iter.hasNext();) { |
| 3055 | + int pt = buffer.length(); |
| 3056 | + appendBoxingNode(iter.next(), false, null, false, false); |
| 3057 | + if (toChar != 0) { |
| 3058 | + String s = buffer.substring(pt); |
| 3059 | + // only fix "x" and number |
| 3060 | + // not fixed: int[] a = new int[] {"test".charAt(0)} |
| 3061 | + try { |
| 3062 | + if (pt > 0 && (s.charAt(0) == '"') != (toChar == 1)) { |
| 3063 | + buffer.replace(pt, buffer.length(), |
| 3064 | + (toChar == 1 ? |
| 3065 | + "\"" + ((char) Integer.parseInt(s)) + "\"" |
| 3066 | + : s.length() == 3 ? "" + (byte) s.charAt(1) : s) |
| 3067 | + ); |
| 3068 | + } |
| 3069 | + } catch (@SuppressWarnings("unused") Exception e) { |
| 3070 | + // ignore |
| 3071 | + } |
| 3072 | + } |
| 3073 | + if (!iter.hasNext()) |
| 3074 | + return; |
| 3075 | + buffer.append(", "); |
| 3076 | + } |
| 3077 | + } |
| 3078 | + |
3027 | 3079 | public boolean visit(Assignment node) { |
3028 | 3080 | // note that this is not |
3029 | 3081 | // var x = ..... -- that is a visit(VariableDeclaration) |
@@ -4431,8 +4483,6 @@ private void addNonCharacter(Expression exp) { |
4431 | 4483 | switch (name) { |
4432 | 4484 | case "char": |
4433 | 4485 | case "Character": |
4434 | | - addOperand(exp, false); |
4435 | | - break; |
4436 | 4486 | case "Byte": |
4437 | 4487 | case "Short": |
4438 | 4488 | case "Integer": |
|
0 commit comments