@@ -35,14 +35,17 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
3535 // Because it adds additional parameters to some constructors
3636
3737 def transformInfo (tp : Type , sym : Symbol )(implicit ctx : Context ): Type =
38- if (sym.isConstructor && derivesFromJEnum(sym.owner)) addConstrParams(sym.info)
38+ if (sym.isConstructor && (
39+ sym == defn.JavaEnumClass .primaryConstructor ||
40+ derivesFromJavaEnum(sym.owner)))
41+ addConstrParams(sym.info)
3942 else tp
4043
4144 /** Is `sym` a Scala enum class that derives (directly) from `java.lang.Enum`?
4245 */
43- private def derivesFromJEnum (sym : Symbol )(implicit ctx : Context ) =
46+ private def derivesFromJavaEnum (sym : Symbol )(implicit ctx : Context ) =
4447 sym.is(Enum , butNot = Case ) &&
45- sym.info.parents.exists(p => p.typeSymbol == defn.JEnumClass )
48+ sym.info.parents.exists(p => p.typeSymbol == defn.JavaEnumClass )
4649
4750 /** Add constructor parameters `$name: String` and `$ordinal: Int` to the end of
4851 * the last parameter list of (method- or poly-) type `tp`.
@@ -98,10 +101,10 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
98101 */
99102 override def transformDefDef (tree : DefDef )(implicit ctx : Context ): DefDef = {
100103 val sym = tree.symbol
101- if (sym.isConstructor && derivesFromJEnum (sym.owner))
104+ if (sym.isConstructor && derivesFromJavaEnum (sym.owner))
102105 cpy.DefDef (tree)(
103106 vparamss = tree.vparamss.init :+ (tree.vparamss.last ++ addedParams(sym, Param )))
104- else if (sym.name == nme.DOLLAR_NEW && derivesFromJEnum (sym.owner.linkedClass)) {
107+ else if (sym.name == nme.DOLLAR_NEW && derivesFromJavaEnum (sym.owner.linkedClass)) {
105108 val Block ((tdef @ TypeDef (tpnme.ANON_CLASS , templ : Template )) :: Nil , call) = tree.rhs
106109 val args = tree.vparamss.last.takeRight(2 ).map(param => ref(param.symbol)).reverse
107110 val templ1 = cpy.Template (templ)(
@@ -113,8 +116,7 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
113116 }
114117
115118 /** 1. If this is an enum class, add $name and $ordinal parameters to its
116- * parameter accessors and pass them on to the java.lang.Enum constructor,
117- * replacing the dummy arguments that were passed before.
119+ * parameter accessors and pass them on to the java.lang.Enum constructor.
118120 *
119121 * 2. If this is an anonymous class that implement a value enum case,
120122 * pass $name and $ordinal parameters to the enum superclass. The class
@@ -135,20 +137,15 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
135137 */
136138 override def transformTemplate (templ : Template )(implicit ctx : Context ): Template = {
137139 val cls = templ.symbol.owner
138- if (derivesFromJEnum (cls)) {
140+ if (derivesFromJavaEnum (cls)) {
139141 val (params, rest) = decomposeTemplateBody(templ.body)
140142 val addedDefs = addedParams(cls, ParamAccessor )
141143 val addedSyms = addedDefs.map(_.symbol.entered)
142- val parents1 = templ.parents.map {
143- case app @ Apply (fn, _) if fn.symbol.owner == defn.JEnumClass =>
144- cpy.Apply (app)(fn, addedSyms.map(ref))
145- case p => p
146- }
147144 cpy.Template (templ)(
148- parents = parents1 ,
145+ parents = addEnumConstrArgs(defn. JavaEnumClass , templ.parents, addedSyms.map(ref)) ,
149146 body = params ++ addedDefs ++ rest)
150147 }
151- else if (cls.isAnonymousClass && cls.owner.is(EnumCase ) && derivesFromJEnum (cls.owner.owner.linkedClass)) {
148+ else if (cls.isAnonymousClass && cls.owner.is(EnumCase ) && derivesFromJavaEnum (cls.owner.owner.linkedClass)) {
152149 def rhsOf (name : TermName ) =
153150 templ.body.collect {
154151 case mdef : DefDef if mdef.name == name => mdef.rhs
0 commit comments