Skip to content

Commit 50e4e2b

Browse files
committed
test.Test_init
checks that first-pass/second-pass on field initialization is done correctly.
1 parent 6baa383 commit 50e4e2b

File tree

3 files changed

+108
-38
lines changed

3 files changed

+108
-38
lines changed

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

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,7 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
14211421

14221422
init0Buffer = new StringBuffer();
14231423

1424+
int len = buffer.length();
14241425
buffer.append("\r\nClazz.newMethod$(C$, '$init$', function () {\r\n");
14251426
// we include all field definitions here and all nonstatic
14261427
// initializers
@@ -1436,11 +1437,19 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
14361437
buffer.append("}, 1);\r\n");
14371438

14381439
if (init0Buffer.length() > 0) {
1440+
1441+
String buf = buffer.substring(len);
1442+
buffer.setLength(len);
1443+
14391444
buffer.append("\r\nClazz.newMethod$(C$, '$init0$', function () {\r\n");
14401445
buffer.append("var c;if((c = C$.superClazz) && (c = c.$init0$))c.apply(this);\r\n");
14411446
buffer.append(init0Buffer);
1442-
buffer.append("}, 1);\r\n");
1447+
buffer.append("}, 1);\r\n");
1448+
1449+
buffer.append(buf);
14431450
}
1451+
1452+
init0Buffer = null;
14441453
}
14451454

14461455

@@ -1605,50 +1614,55 @@ private void addFieldDeclaration(FieldDeclaration node, boolean isStatic) {
16051614
if (isFinal && constantValue != null)
16061615
continue;
16071616
int len = buffer.length();
1608-
buffer.append(isStatic ? "C$." : "this.");
1609-
buffer.append(getCheckedFieldName(J2SMapAdapter.getJ2SName(fragment.getName()), classBinding, false));
1617+
String prefix = (isStatic ? "C$." : "this.")
1618+
+ getCheckedFieldName(J2SMapAdapter.getJ2SName(fragment.getName()), classBinding, false);
1619+
buffer.append(prefix);
16101620
Code code = (nodeType == null || !nodeType.isPrimitiveType() ? null
16111621
: ((PrimitiveType) nodeType).getPrimitiveTypeCode());
1612-
if (isStatic || initializer == null) {
1622+
int len1 = buffer.length();
1623+
if (isStatic ? initializer == null : classBinding.getSuperclass() != null) {
1624+
// Route default for this to the $init0$ buffer
1625+
// if static and not initialized or nonstatic and there is a superclass
1626+
16131627
buffer.append(" = ");
16141628
buffer.append(code == null ? "null" : getPrimitiveDefault(code));
16151629
buffer.append(";\r\n");
1616-
if (!isStatic) {
1617-
// Route this to the $init0$ buffer, which will be processed
1618-
// next here,
1619-
// but will be executed first:
1620-
//
1621-
// $clinit$ -- statics; once only
1622-
// $init0$ -- from within Clazz.newInstance$, before any
1623-
// constructors
1624-
// $init$ -- from the constructor, just after any super()
1625-
// call or whenever there is no this() call
1626-
1627-
// (This visit is from within addClassOrInterface.)
1628-
// We must not initialize fields that aren't initialized in
1629-
// Java.
1630-
//
1631-
// com.falstad.Diffraction.CrossAperature initialization was
1632-
// failing. Sequence was:
1633-
1634-
// Aperature<init>: calls setDefaults() (new double[][]
1635-
// lineXLocations)
1636-
// BlockAperature<init> sets lineXLocations = null
1637-
// CrossAperature<init> needs the defaults set and fails
1638-
1639-
// but needed to be:
1640-
1641-
// Aperature<init>: calls setDefaults() (new double[][]
1642-
// lineXLocations)
1643-
// BlockAperature<init> defines but does not set
1644-
// lineXLocations
1645-
// CrossAperature<init> sees the created lineXLocations
1646-
// created in Aperature<init>
1647-
1648-
init0Buffer.append(buffer.substring(len));
1630+
//
1631+
// $clinit$ -- statics; once only
1632+
// $init0$ -- from within Clazz.newInstance$, before any
1633+
// constructors
1634+
// $init$ -- from the constructor, just after any super()
1635+
// call or whenever there is no this() call
1636+
1637+
// com.falstad.Diffraction.CrossAperature initialization was
1638+
// failing. Sequence was:
1639+
1640+
// Aperature<init>: calls setDefaults() (new double[][]
1641+
// lineXLocations)
1642+
// BlockAperature<init> sets lineXLocations = null
1643+
// CrossAperature<init> needs the defaults set and fails
1644+
1645+
// but needed to be:
1646+
1647+
// Aperature<init>: calls setDefaults() (new double[][]
1648+
// lineXLocations)
1649+
// BlockAperature<init> defines but does not set
1650+
// lineXLocations
1651+
// CrossAperature<init> sees the created lineXLocations
1652+
// created in Aperature<init>
1653+
1654+
// if (code != null) // should define all in init0
1655+
if (isStatic)
1656+
return;
1657+
init0Buffer.append(buffer.substring(len));
1658+
if (initializer == null) {
16491659
buffer.setLength(len);
1660+
return;
16501661
}
1651-
} else if (constantValue != null) {
1662+
buffer.setLength(len1);
1663+
}
1664+
1665+
if (constantValue != null) {
16521666
buffer.append(" = ");
16531667
buffer.append(constantValue);
16541668
fixPrimitiveRightSide(code);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package test;
2+
3+
/**
4+
* Check for fields initialization timing.
5+
*
6+
* @author RM
7+
*
8+
*/
9+
public class Test_Init extends Test_init0 {
10+
11+
int testing1;
12+
int testing2 = 3;
13+
14+
String s1;
15+
String s2 = "testing";
16+
17+
static String s;
18+
19+
Test_Init() {
20+
super();
21+
checkTesting(2);
22+
}
23+
24+
@Override
25+
void checkTesting(int pass) {
26+
System.out.println("testing:" + testing1 + " " + testing2);
27+
System.out.println("s:" + s1 + " " + s2);
28+
assert(testing2 == (pass == 1 ? 0 : 3));
29+
assert(s2 == (pass == 1 ? null : "testing"));
30+
// note: this next setting will be ignored!
31+
if (pass == 1)
32+
testing2 = 2;
33+
}
34+
35+
public static void main(String[] args) {
36+
new Test_Init();
37+
System.out.println("Test_Init OK");
38+
}
39+
40+
41+
42+
}
43+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test;
2+
3+
public class Test_init0 extends Test_ {
4+
5+
void checkTesting(int pass) {
6+
System.out.println("--");
7+
}
8+
9+
public Test_init0() {
10+
checkTesting(1);
11+
};
12+
13+
}

0 commit comments

Comments
 (0)