diff options
author | Philipp Haller <hallerp@gmail.com> | 2010-03-19 17:03:14 +0000 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2010-03-19 17:03:14 +0000 |
commit | 05c22ec2eed257c7b5e93aa4830a7666831fff48 (patch) | |
tree | 2e287c8aa7e9803ea4239bd98e991e5f84567b67 /src/library | |
parent | bf2da77cefaed778008a2ede6b129a58adc86913 (diff) | |
download | scala-05c22ec2eed257c7b5e93aa4830a7666831fff48.tar.gz scala-05c22ec2eed257c7b5e93aa4830a7666831fff48.tar.bz2 scala-05c22ec2eed257c7b5e93aa4830a7666831fff48.zip |
Closes #2827. Review by community.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Enumeration.scala | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/src/library/scala/Enumeration.scala b/src/library/scala/Enumeration.scala index 23d1c66ec9..8a708a4615 100644 --- a/src/library/scala/Enumeration.scala +++ b/src/library/scala/Enumeration.scala @@ -164,22 +164,29 @@ abstract class Enumeration(initial: Int, names: String*) { /* Obtains the name for the value with id `i`. If no name is cached * in `nmap`, it populates `nmap` using reflection. */ - private def nameOf(i: Int): String = nmap.get(i) match { - case Some(name) => name - case None => - val methods = getClass.getMethods - for (m <- methods - if classOf[Value].isAssignableFrom(m.getReturnType) && - !java.lang.reflect.Modifier.isFinal(m.getModifiers)) { - val name = m.getName - // invoke method to obtain actual `Value` instance - val value = m.invoke(this) - // invoke `id` method - val idMeth = classOf[Val].getMethod("id") - val id: Int = idMeth.invoke(value).asInstanceOf[java.lang.Integer].intValue() - nmap += (id -> name) - } - nmap(i) + private def nameOf(i: Int): String = synchronized { + def isValDef(m: java.lang.reflect.Method) = + getClass.getDeclaredFields.exists(fd => fd.getName == m.getName && + fd.getType == m.getReturnType) + nmap.get(i) match { + case Some(name) => name + case None => + val methods = getClass.getMethods + for (m <- methods + if (classOf[Value].isAssignableFrom(m.getReturnType) && + !java.lang.reflect.Modifier.isFinal(m.getModifiers) && + m.getParameterTypes.isEmpty && + isValDef(m))) { + val name = m.getName + // invoke method to obtain actual `Value` instance + val value = m.invoke(this) + // invoke `id` method + val idMeth = classOf[Val].getMethod("id") + val id: Int = idMeth.invoke(value).asInstanceOf[java.lang.Integer].intValue() + nmap += (id -> name) + } + nmap(i) + } } /** The type of the enumerated values. */ |