Skip to content

Commit

Permalink
Also no longer search deeper than first body level for
Browse files Browse the repository at this point in the history
constructor method calls.
  • Loading branch information
lloydmeta committed Jan 4, 2017
1 parent 5dee740 commit fb48cc0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ object ContextUtils {
case m if m.isMethod && m.asMethod.isConstructor =>
m.asMethod.paramss.flatten.map(_.asTerm.name)
}

/**
* Returns the reserved constructor name
*/
def constructorName(c: Context): c.universe.TermName = {
c.universe.nme.CONSTRUCTOR
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ object ContextUtils {
case m if m.isConstructor =>
m.asMethod.paramLists.flatten.map(_.asTerm.name)
}

/**
* Returns the reserved constructor name
*/
def constructorName(c: Context): c.universe.TermName = {
c.universe.termNames.CONSTRUCTOR
}
}
15 changes: 12 additions & 3 deletions macros/src/main/scala/enumeratum/ValueEnumMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,25 @@ object ValueEnumMacros {
val valueTerm = ContextUtils.termName(c)("value")
// go through all the trees
memberTrees.map { declTree =>
val directMemberTrees = declTree.children.flatMap(_.children)
val directMemberTrees = declTree.children.flatMap(_.children) // Things that are body-level, no lower
val constructorTrees = {
val immediate = directMemberTrees // for 2.11+ this is enough
val constructorName = ContextUtils.constructorName(c)
val method =
directMemberTrees.collect { // for 2.10.x, we need to grab the body-level constructor method's trees
case t @ DefDef(_, `constructorName`, _, _, _, _) =>
t.collect { case t => t }
}.flatten
immediate ++ method
}.iterator

val valuesFromMembers = directMemberTrees.iterator.collect {
case ValDef(_, termName, _, Literal(Constant(i: ValueType))) if termName == valueTerm =>
Some(i)
}

// Sadly 2.10 has parent-class constructor calls nested inside a member..
val lazyTrees = declTree.collect { case t => t }.iterator
val valuesFromConstructors = lazyTrees.collect {
val valuesFromConstructors = constructorTrees.collect {
// The tree has a method call
case Apply(_, args) => {
val valueArguments: List[Option[ValueType]] =
Expand Down

0 comments on commit fb48cc0

Please sign in to comment.