Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2910a8f

Browse files
committed
array initialization fix -- SwingJS only
//BH 2024.07.14 -- 5.0.1-v4 fixes numerical array initializer using characters ['a','b',...], but not new int[] { "test".charAt(3) }
1 parent ea846db commit 2910a8f

File tree

8 files changed

+77
-13
lines changed

8 files changed

+77
-13
lines changed
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20240627180429
1+
20240814092323
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20240627180429
1+
20240814092323

sources/net.sf.j2s.core/src/j2s/CorePlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ public class CorePlugin extends Plugin {
2626
* the actual "x.y.z" version is specified in plugin.xml.
2727
*
2828
*/
29-
public static String VERSION = "5.0.1-v2";
29+
public static String VERSION = "5.0.1-v4";
3030

3131
// if you change the x.x.x number, be sure to also indicate that in
3232
// j2sApplet.js and also (Bob only) update.bat, update-clean.bat
3333

34+
// BH 2024.07.14 -- 5.0.1-v4 fixes numerical array initializer using characters ['a','b',...]
35+
// BH 2024.02.22 -- 5.0.1-v3 fixes long extension issue causing MutableBitInteger to miscalculate subtraction(no change in version #)
3436
// BH 2023.11.27 -- 5.0.1-v2 final refactoring and creatiton of J2SUtil
3537
// BH 2023.11.21 -- 5.0.1-v2 adds Java8 syntaxes for try and switch; removes dependency for instanceOf and exception checking
3638
// BH 2023.11.09 -- 5.0.1-v1 merges Jmol legacy (.j2sjmol) with Java8//11 (.j2s)

sources/net.sf.j2s.core/src/j2s/jmol/J2SLegacyVisitor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2100,7 +2100,7 @@ public boolean visit(ArrayCreation node) {
21002100
buffer.append(" Clazz.newBooleanArray(");
21012101
boxList(dim, ", ");
21022102
buffer.append(", false)");
2103-
} else {
2103+
} else {
21042104
if (dim != null && dim.size() > 1) {
21052105
buffer.append(" Clazz.newArray(");
21062106
boxList(dim, ", ");
@@ -2140,6 +2140,8 @@ public boolean visit(ArrayInitializer node) {
21402140
buffer.append("]");
21412141
return false;
21422142
}
2143+
// note that this does NOT allow for new byte[] {'a', 'b', 'c'}
2144+
// only implemented that for SwingJS
21432145
if (elementType.isPrimitive()) {
21442146
String typeCode = elementType.getName();
21452147
if ("int".equals(typeCode)

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

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
// TODO: superclass inheritance for JAXB XmlAccessorType
3232
// TODO: Transpiler bug allows static String name, but JavaScript function().name is read-only and will be "clazz"
3333

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)
3539
//BH 2023.03.29 -- 3.3.1-v7 fixes outer static method call from within lambda expression.
3640
//BH 2023.02.09 -- 3.3.1.v6 fixes j2s.excluded.paths needing /src/xxxx
3741
//BH 2022.06.27 -- 3.3.1-v5 fixes missing method annotations
@@ -3015,15 +3019,63 @@ private void addArrayConstructor(ITypeBinding binding, List<ASTNode> dim) {
30153019

30163020
public boolean visit(ArrayInitializer node) {
30173021
// 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));
30193024
buffer.append(", [");
3025+
int toChar = toCharType(type);
30203026
@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+
}
30233033
buffer.append("])");
30243034
return false;
30253035
}
30263036

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+
30273079
public boolean visit(Assignment node) {
30283080
// note that this is not
30293081
// var x = ..... -- that is a visit(VariableDeclaration)
@@ -4431,8 +4483,6 @@ private void addNonCharacter(Expression exp) {
44314483
switch (name) {
44324484
case "char":
44334485
case "Character":
4434-
addOperand(exp, false);
4435-
break;
44364486
case "Byte":
44374487
case "Short":
44384488
case "Integer":

sources/net.sf.j2s.java.core/src/test/Test_Array.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Test_Array extends Test_ {
99

10-
static Object ax1 =new int[2][];
10+
static Object ax1 =new int[2][];
1111
static Object ax = new int[2][3][];
1212

1313

@@ -60,8 +60,18 @@ class Test_Array extends Test_ {
6060
{ { 0, 1, 2 }, { 3, 4, 5 }, { 0, 1, 2 }, { 3, 4, 5 }, { 0, 1, 2 }, { 3, 4, 5 } } };
6161

6262
public static void main(String[] args) {
63-
64-
63+
char cc = Character.valueOf('c');
64+
byte b = Byte.valueOf((byte)3);
65+
char[] chs = new char[] {65, 'B', cc, Character.valueOf('c')};
66+
int[] ints = new int[] {1,2, 'a', 3, b + 1}; // NOT "3".charAt(0)};
67+
short[] shorts = new short[] {1,2, 'a', +3};
68+
long[] longs = new long[] {1,2, 'a', 3};
69+
float[] floats = new float[] {1,2, 'a', 3};
70+
double[] dbls = new double[] {1,2, 'a', 3};
71+
byte[] by = new byte[] { 0, 'a', 'b', 'c' + 2, b };
72+
byte[][] by2 = new byte[][] { {0, 'd'}, {'b', 'c' + 1} };
73+
System.out.println(Arrays.toString(by));
74+
6575
int[] a = new int[] {1,2,3};
6676
int i = 2, jj;
6777
a[--i] *= a[i + 1];

0 commit comments

Comments
 (0)