This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Scala Reflection

7 replies
dmoshal
Joined: 2008-11-06,
User offline. Last seen 2 years 24 weeks ago.

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

dmoshal
Joined: 2008-11-06,
User offline. Last seen 2 years 24 weeks ago.
Re: Scala Reflection API

No answers yet? Surprisingly, I was imagining that someone would have a
pointer to literature on the scala reflection api....

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Scala Reflection API
So where's this Scala reflection API that you'd like documentation for?

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.


dmoshal
Joined: 2008-11-06,
User offline. Last seen 2 years 24 weeks ago.
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

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
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:

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.


dmoshal
Joined: 2008-11-06,
User offline. Last seen 2 years 24 weeks ago.
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

DRMacIver
Joined: 2008-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala Reflection API
On Tue, Feb 10, 2009 at 11:00 PM, dmoshal <dmoshal@gmail.com> wrote:

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.
dmoshal
Joined: 2008-11-06,
User offline. Last seen 2 years 24 weeks ago.
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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland