@@ -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 \n Clazz.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 \n Clazz.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 );
0 commit comments