For purposes of my app I need to be able to find out a list of fields of a type (not an instance) and types of those fields in runtime. So far I was only able to get a list of methods of a case class containing getters with classOf[MyCaseClass].getMethods
and nothing useful from a simple class. Am I missing something? Are there any reflection libraries for that kinda purposes? How's that done correctly?
Asked
Active
Viewed 7,452 times
8

Nikita Volkov
- 42,792
- 11
- 94
- 169
-
I'm confused - the title says fields, and then you go looking for methods. Which do you want? – Duncan McGregor Oct 10 '11 at 06:49
-
1Related http://stackoverflow.com/q/7477248/97777 – Duncan McGregor Oct 10 '11 at 06:55
-
Thanks Duncan, I'll look into it. I went looking for methods to find properties which have names and types corresponding to their fields. – Nikita Volkov Oct 10 '11 at 07:05
2 Answers
18
Using Scala 2.10 reflection:
scala> import reflect.runtime.{universe => ru}
import reflect.runtime.{universe=>ru}
scala> trait A { val field1: Int; val field2: Char; def meth1: Int }
defined trait A
scala> val fieldSymbols = ru.typeOf[A].members.collect{ case m: ru.MethodSymbol if m.isGetter => m }
fieldSymbols: Iterable[reflect.runtime.universe.MethodSymbol] = List(value field2, value field1)
The returned symbols contain all the type information, e.g.:
scala> fieldSymbols.map(_.typeSignature)
res16: Iterable[reflect.runtime.universe.Type] = List(=> scala.Char, => scala.Int)

Nikita Volkov
- 42,792
- 11
- 94
- 169
-
1
-
-
`collect{ case m: ru.MethodSymbol if m.isGetter => m }` gives the warning "abstract type pattern ru.MethodSymbol is unchecked since it is eliminated by erasure" for the `MethodSymbol`. Not sure so far how to eliminate it. – Max Spring Apr 27 '16 at 17:26
-
I'm a Scala neophyte, but I resolved the warning by doing `collect{ case m if ru.MethodSymbolTag.runtimeClass.isInstance(m) && m.asInstanceOf[ru.MethodSymbol].isGetter => m }` – Pakman Jun 18 '18 at 20:43
6
You may want to take a look at this document on reflecting scala. getMethods
is a method from Java reflection. What can't you find there? From the Javadoc:
- String getName(): Returns the name of the method represented by this Method object, as a String.
- Class[] getParameterTypes(): Returns an array of Class objects that represent the formal parameter types, in declaration order, of the method represented by this Method object.
- Class getReturnType(): Returns a Class object that represents the formal return type of the method represented by this Method object.
You could read more about Java reflection.
Note that not all type information will be available at runtime because of erasure.

schmmd
- 18,650
- 16
- 58
- 102
-
Thanks I know about that project and have actually already downloaded their lib, but I can't get it to compile since it has too many conflicts with 2.9.1 which I am using. You don't need to explain Java reflection to me. What I was looking for were clean ways of dealing with it in Scala. The problems I have with reflection is that Scala fields are not presented there at all and `getMethods` returns a bunch of generated methods which need to be filtered out and I was hoping that there existed a consistent algorithm for that. Guess I'll have to analyze the *Mirrors* code. – Nikita Volkov Oct 10 '11 at 05:19
-
Looks like your question is a duplicate of http://stackoverflow.com/questions/2204057/reflection-api-for-scala then. – schmmd Oct 10 '11 at 05:30
-
The link is dead, an updated link would be appreciated (I don't know what to look for). – Tomer Gabel Mar 06 '12 at 11:36
-
Hi Tomer, I'm guessing the link is dead in part because there is a new Scala reflection API being released with scala 2.10. The [second milestone is already available](http://www.scala-lang.org/node/12550) and it has a preliminary reflection API. You might want to [look at the scaladoc](http://www.scala-lang.org/api/milestone/index.html#scala.reflect.package). – schmmd Mar 06 '12 at 22:15
-
Here is [an updated link](http://lampwww.epfl.ch/teaching/projects/archive/coppel_report.pdf), but you are probably better off looking into reflection in 2.10. There are a few [stackoverflow posts](http://stackoverflow.com/questions/7477248/what-reflection-capabilities-can-we-expect-from-scala-2-10) on the matter and some [blog posts](http://alots.wordpress.com/2012/03/06/getting-into-the-new-scalas-2-10-reflection-api/). – schmmd Mar 06 '12 at 22:18
-
This is the [most helpful information](http://ochsenreither.posterous.com/whats-new-in-210) I've seen about 2.10. – schmmd Mar 07 '12 at 00:43