summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Haller <hallerp@gmail.com>2010-03-19 17:03:14 +0000
committerPhilipp Haller <hallerp@gmail.com>2010-03-19 17:03:14 +0000
commit05c22ec2eed257c7b5e93aa4830a7666831fff48 (patch)
tree2e287c8aa7e9803ea4239bd98e991e5f84567b67
parentbf2da77cefaed778008a2ede6b129a58adc86913 (diff)
downloadscala-05c22ec2eed257c7b5e93aa4830a7666831fff48.tar.gz
scala-05c22ec2eed257c7b5e93aa4830a7666831fff48.tar.bz2
scala-05c22ec2eed257c7b5e93aa4830a7666831fff48.zip
Closes #2827. Review by community.
-rw-r--r--src/library/scala/Enumeration.scala39
-rw-r--r--test/files/jvm/t2827.check3
-rw-r--r--test/files/jvm/t2827.scala14
3 files changed, 40 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. */
diff --git a/test/files/jvm/t2827.check b/test/files/jvm/t2827.check
new file mode 100644
index 0000000000..c318e01ae5
--- /dev/null
+++ b/test/files/jvm/t2827.check
@@ -0,0 +1,3 @@
+Larry
+Curly
+Moe
diff --git a/test/files/jvm/t2827.scala b/test/files/jvm/t2827.scala
new file mode 100644
index 0000000000..d89e68516b
--- /dev/null
+++ b/test/files/jvm/t2827.scala
@@ -0,0 +1,14 @@
+object Stooges extends Enumeration {
+ type Stooge = Value
+ val Larry, Curly, Moe = Value
+ def nextStooge(v:Stooges.Stooge):Stooges.Stooge =
+ Stooges((v.id+1) % Stooges.maxId)
+}
+
+object Test {
+ def main(args: Array[String]) {
+ println(Stooges.Larry)
+ println(Stooges.Curly)
+ println(Stooges.Moe)
+ }
+}