@@ -13,7 +13,7 @@ import symtab.Flags._
1313/** This phase converts classes with parameters into Java-like classes with
1414 * fields, which are assigned to from constructors.
1515 */
16- abstract class Constructors extends Transform with ast.TreeDSL {
16+ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
1717 import global ._
1818 import definitions ._
1919
@@ -199,7 +199,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
199199 detectUsages walk auxConstructorBuf
200200 }
201201 }
202- def mustbeKept (sym : Symbol ) = ! omittables(sym)
202+ def mustBeKept (sym : Symbol ) = ! omittables(sym)
203203
204204 } // OmittablesHelper
205205
@@ -458,7 +458,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
458458 } // GuardianOfCtorStmts
459459
460460 private class TemplateTransformer (val unit : CompilationUnit , val impl : Template )
461- extends Transformer
461+ extends StaticsTransformer
462462 with DelayedInitHelper
463463 with OmittablesHelper
464464 with GuardianOfCtorStmts {
@@ -607,7 +607,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
607607 // follow the primary constructor
608608 val auxConstructorBuf = new ListBuffer [Tree ]
609609
610- // The list of statements that go into constructor after and including the superclass constructor call
610+ // The list of statements that go into the constructor after and including the superclass constructor call
611611 val constrStatBuf = new ListBuffer [Tree ]
612612
613613 // The list of early initializer statements that go into constructor before the superclass constructor call
@@ -616,6 +616,9 @@ abstract class Constructors extends Transform with ast.TreeDSL {
616616 // The early initialized field definitions of the class (these are the class members)
617617 val presupers = treeInfo.preSuperFields(stats)
618618
619+ // The list of statements that go into the class initializer
620+ val classInitStatBuf = new ListBuffer [Tree ]
621+
619622 // generate code to copy pre-initialized fields
620623 for (stat <- constrBody.stats) {
621624 constrStatBuf += stat
@@ -644,7 +647,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
644647 else if (stat.symbol.isConstructor) auxConstructorBuf += stat
645648 else defBuf += stat
646649 }
647- case ValDef (_ , _, _, rhs) =>
650+ case ValDef (mods , _, _, rhs) if ! mods.hasStaticFlag =>
648651 // val defs with constant right-hand sides are eliminated.
649652 // for all other val defs, an empty valdef goes into the template and
650653 // the initializer goes as an assignment into the constructor
@@ -659,6 +662,11 @@ abstract class Constructors extends Transform with ast.TreeDSL {
659662 }
660663 defBuf += deriveValDef(stat)(_ => EmptyTree )
661664 }
665+ case ValDef (_, _, _, rhs) =>
666+ // Add static initializer statements to classInitStatBuf and remove the rhs from the val def.
667+ classInitStatBuf += mkAssign(stat.symbol, rhs)
668+ defBuf += deriveValDef(stat)(_ => EmptyTree )
669+
662670 case ClassDef (_, _, _, _) =>
663671 // classes are treated recursively, and left in the template
664672 defBuf += new ConstructorTransformer (unit).transform(stat)
@@ -670,7 +678,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
670678 populateOmittables()
671679
672680 // Initialize all parameters fields that must be kept.
673- val paramInits = paramAccessors filter mustbeKept map { acc =>
681+ val paramInits = paramAccessors filter mustBeKept map { acc =>
674682 // Check for conflicting symbol amongst parents: see bug #1960.
675683 // It would be better to mangle the constructor parameter name since
676684 // it can only be used internally, but I think we need more robust name
@@ -710,11 +718,13 @@ abstract class Constructors extends Transform with ast.TreeDSL {
710718 defBuf ++= auxConstructorBuf
711719
712720 // Unlink all fields that can be dropped from class scope
713- for (sym <- clazz.info.decls ; if ! mustbeKept (sym))
721+ for (sym <- clazz.info.decls ; if ! mustBeKept (sym))
714722 clazz.info.decls unlink sym
715723
716724 // Eliminate all field definitions that can be dropped from template
717- val transformed : Template = deriveTemplate(impl)(_ => defBuf.toList filter (stat => mustbeKept(stat.symbol)))
725+ val templateWithoutOmittables : Template = deriveTemplate(impl)(_ => defBuf.toList filter (stat => mustBeKept(stat.symbol)))
726+ // Add the static initializers
727+ val transformed : Template = addStaticInits(templateWithoutOmittables, classInitStatBuf, localTyper)
718728
719729 } // TemplateTransformer
720730
0 commit comments