@@ -35,31 +35,32 @@ class MoveStatics extends MiniPhaseTransform with SymTransformer { thisTransform
35
35
val (classes, others) = trees.partition(x => x.isInstanceOf [TypeDef ] && x.symbol.isClass)
36
36
val pairs = classes.groupBy(_.symbol.name.stripModuleClassSuffix).asInstanceOf [Map [Name , List [TypeDef ]]]
37
37
38
+ def rebuild (orig : TypeDef , newBody : List [Tree ]): Tree = {
39
+ if (orig eq null ) return EmptyTree
40
+
41
+ val staticFields = newBody.filter(x => x.isInstanceOf [ValDef ] && x.symbol.hasAnnotation(defn.ScalaStaticAnnot )).asInstanceOf [List [ValDef ]]
42
+ val newBodyWithStaticConstr =
43
+ if (staticFields.nonEmpty) {
44
+ /* do NOT put Flags.JavaStatic here. It breaks .enclosingClass */
45
+ val staticCostructor = ctx.newSymbol(orig.symbol, Names .STATIC_CONSTRUCTOR , Flags .Synthetic | Flags .Method , MethodType (Nil , defn.UnitType ))
46
+ staticCostructor.addAnnotation(Annotation (defn.ScalaStaticAnnot ))
47
+ staticCostructor.entered
48
+
49
+ val staticAssigns = staticFields.map(x => Assign (ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor)))
50
+ tpd.DefDef (staticCostructor, Block (staticAssigns, tpd.unitLiteral)) :: newBody
51
+ } else newBody
52
+
53
+ val oldTemplate = orig.rhs.asInstanceOf [Template ]
54
+ cpy.TypeDef (orig)(rhs = cpy.Template (orig.rhs)(oldTemplate.constr, oldTemplate.parents, oldTemplate.self, newBodyWithStaticConstr))
55
+ }
56
+
38
57
def move (module : TypeDef , companion : TypeDef ): List [Tree ] = {
39
58
if (! module.symbol.is(Flags .Module )) move(companion, module)
40
59
else {
41
60
val allMembers =
42
61
(if (companion ne null ) {companion.rhs.asInstanceOf [Template ].body} else Nil ) ++
43
62
module.rhs.asInstanceOf [Template ].body
44
63
val (newModuleBody, newCompanionBody) = allMembers.partition(x => {assert(x.symbol.exists); x.symbol.owner == module.symbol})
45
- def rebuild (orig : TypeDef , newBody : List [Tree ]): Tree = {
46
- if (orig eq null ) return EmptyTree
47
-
48
- val staticFields = newBody.filter(x => x.isInstanceOf [ValDef ] && x.symbol.hasAnnotation(defn.ScalaStaticAnnot )).asInstanceOf [List [ValDef ]]
49
- val newBodyWithStaticConstr =
50
- if (staticFields.nonEmpty) {
51
- /* do NOT put Flags.JavaStatic here. It breaks .enclosingClass */
52
- val staticCostructor = ctx.newSymbol(orig.symbol, Names .STATIC_CONSTRUCTOR , Flags .Synthetic | Flags .Method , MethodType (Nil , defn.UnitType ))
53
- staticCostructor.addAnnotation(Annotation (defn.ScalaStaticAnnot ))
54
- staticCostructor.entered
55
-
56
- val staticAssigns = staticFields.map(x => Assign (ref(x.symbol), x.rhs.changeOwner(x.symbol, staticCostructor)))
57
- tpd.DefDef (staticCostructor, Block (staticAssigns, tpd.unitLiteral)) :: newBody
58
- } else newBody
59
-
60
- val oldTemplate = orig.rhs.asInstanceOf [Template ]
61
- cpy.TypeDef (orig)(rhs = cpy.Template (orig.rhs)(oldTemplate.constr, oldTemplate.parents, oldTemplate.self, newBodyWithStaticConstr))
62
- }
63
64
Trees .flatten(rebuild(companion, newCompanionBody) :: rebuild(module, newModuleBody) :: Nil )
64
65
}
65
66
}
@@ -68,7 +69,7 @@ class MoveStatics extends MiniPhaseTransform with SymTransformer { thisTransform
68
69
yield
69
70
if (classes.tail.isEmpty)
70
71
if (classes.head.symbol.is(Flags .Module )) move(classes.head, null )
71
- else List (classes.head)
72
+ else List (rebuild( classes.head, classes.head.rhs. asInstanceOf [ Template ].body) )
72
73
else move(classes.head, classes.tail.head)
73
74
Trees .flatten(newPairs.toList.flatten ++ others)
74
75
} else trees
0 commit comments