- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Scala Reflection
Tue, 2009-02-10, 03:18
Well, I had great response to my first question, very impressive response times.
So, here is the next question:
Problem: how to scan a directory, find all the files that subclass a particular
type and then generate text files based on Scala metadata.
Application: I currently generate Actionscript files from java code, with JAM:
http://annogen.codehaus.org/JAM
JAM is able to recursively scan a directory of files, parse out the java
metadata, and identify the classes which implement a particular interface. I
then use the Velocity template engine to write the Actionscript files.
Impressively, JAM can parse both *.java and *.class
- I currently parse the *.java files.
Approaches I have tried so far:
1. Using JAM on the compiled Scala classes:
Doesn't get me any useful info other than the class name. Additionally, it seems
to fall apart when it hits traits (are they interfaces or classes in terms of
byte code metadata I wonder). At any rate, I was hoping that JAM would be able
to read the metadata in the *.class as find the public fields (which is all I'm
really interested in).
2. Using the scala reflection api:
This might work if I already knew the classes I was interested in (and perhaps
that's an interim hack for me). However, given a particular class file, known to
be compiled Scala, I can't seem to find out if it implements a particular class
or trait. Additionally, I can't seem to reflect on the Class Type, but rather I
need an instance - not a huge problem, creating instances, because it's only for
the purposes of generating files, but seems to be a hack.
Approaches I have still to try:
3. (crude) naming the class I want to extract data from, say Foo_GenerateAS3.
(But who want's to be forced to name classes like that!). At least then I'd be
able to identify them.
Ideas?
Dave
Tue, 2009-02-10, 20:37
#2
Re: Scala Reflection API
So where's this Scala reflection API that you'd like documentation for?
2009/2/10 dmoshal <dmoshal@gmail.com>
2009/2/10 dmoshal <dmoshal@gmail.com>
No answers yet? Surprisingly, I was imagining that someone would have a
pointer to literature on the scala reflection api....
--
View this message in context: http://www.nabble.com/-scala---Scala-Reflection-tp21926439p21939293.html
Sent from the Scala mailing list archive at Nabble.com.
Tue, 2009-02-10, 21:47
#3
Re: Scala Reflection API
I now realize that:
a) the 'scala reflection api' is actually the java reflection api, and
b) various scala features are probably not accessible with this api
For example: nested classes
given:
------
object Outer
{
class Inner (val s:String)
}
-----
the results of reflection are:
-----
Outer.getClass // works
Inner.getClass // fails
new Inner ("").getClass // works
-----
Seems to me that nested classes in Scala are not visible to the compiler,
even though separate .class files are created for them.
Dave
Tue, 2009-02-10, 21:57
#4
Re: Scala Reflection API
Outer.getClass works not because it's an outer class but because it's an object.
getClass only works on instances, not on classes. If you're looking for the equivalent of Java's Outer.class then you should use classOf[Outer] in Scala.
--j
On Tue, Feb 10, 2009 at 12:38 PM, dmoshal <dmoshal@gmail.com> wrote:
getClass only works on instances, not on classes. If you're looking for the equivalent of Java's Outer.class then you should use classOf[Outer] in Scala.
--j
On Tue, Feb 10, 2009 at 12:38 PM, dmoshal <dmoshal@gmail.com> wrote:
I now realize that:
a) the 'scala reflection api' is actually the java reflection api, and
b) various scala features are probably not accessible with this api
For example: nested classes
given:
------
object Outer
{
class Inner (val s:String)
}
-----
the results of reflection are:
-----
Outer.getClass // works
Inner.getClass // fails
new Inner ("").getClass // works
-----
Seems to me that nested classes in Scala are not visible to the compiler,
even though separate .class files are created for them.
Dave
--
View this message in context: http://www.nabble.com/-scala---Scala-Reflection-tp21926439p21940574.html
Sent from the Scala mailing list archive at Nabble.com.
Wed, 2009-02-11, 00:07
#5
Re: Scala Reflection API
Jorge, thanks, makes sense that 'object' is an instance!
classOf [Inner] worked perfectly.
Any idea how I'd find all the classes that implement a trait or class, given
only the location of the root of either the .class files or .scala files?
Dave
Wed, 2009-02-11, 00:17
#6
Re: Scala Reflection API
On Tue, Feb 10, 2009 at 11:00 PM, dmoshal <dmoshal@gmail.com> wrote:
I don't think there's a good scala specific way of doing this. The best way to do this is probably to write something using e.g. ASM to scan the classfiles for their inheritance properties: This should work generically for anything that compiles to class files. Then the easiest way to do it for scala is just compile the scala code and run it on the classes.
Jorge, thanks, makes sense that 'object' is an instance!
classOf [Inner] worked perfectly.
Any idea how I'd find all the classes that implement a trait or class, given
only the location of the root of either the .class files or .scala files?
I don't think there's a good scala specific way of doing this. The best way to do this is probably to write something using e.g. ASM to scan the classfiles for their inheritance properties: This should work generically for anything that compiles to class files. Then the easiest way to do it for scala is just compile the scala code and run it on the classes.
Wed, 2009-02-11, 02:47
#7
Re: Scala Reflection API
Thanks - my understanding is that JAM was doing that (see my initial post in
this thread), but I wasn't able to get it too work. Probably I'll have to
experiment on the java side first (I was using JAM to look at .java files,
not .class files, and I had that working).
Dave
No answers yet? Surprisingly, I was imagining that someone would have a
pointer to literature on the scala reflection api....