开发者

Is there a good reflection library available for Scala?

开发者 https://www.devze.com 2023-02-01 19:03 出处:网络
I\'m working on a library that needs reflection, and needs Scala-specific information as opposed to what is available via the standard Java reflection API.Right now I\'m using the undocumented code in

I'm working on a library that needs reflection, and needs Scala-specific information as opposed to what is available via the standard Java reflection API. Right now I'm using the undocumented code in scalap (the Scala equivalent to javap) and trying to associate the data it provides with Java reflection objects so that I can call methods and such (scalap just parses class files).

I found this but there's nothing there... https://github.com/dubochet/scala-reflection

...and I've searched the Scala SVN repo for traces of it and failed to find it there, either (but it could be I just missed it).

Does anyone know of a Scala reflection library that provides information similar to what scalap provides but also allows you to call methods, access fields, etc like the Java ref开发者_开发技巧lection library?

[Edit]

I suppose to should explain more clearly what I'm trying to do...

I want to write a library that will take a file, say a delimited text file or worksheet within an Excel file, with headers at the top, and a Manifest, and return a Seq of objects. So basically a function that looks like this:

def parseFile[T](fileName: String)(implicit manifest: Manifest[T]): Seq[T] = {
  // lots of magic ...
}

In the general case this probably isn't possible, and I'm sure there are a lot of "possible" cases that that are extremely difficult, but I think there's a useful subset that can be implemented without too much complexity once I get the hang of scalap (or scala.reflect, the two are similar yet different...) and marry it to the right objects in the Java reflection API. My basic plan is to look at the headers of the file, look at the parameter names and types of the public constructors and apply methods on the companion object, pick the one that best matches the file, and parse the file into objects. This is for something closer to scripting than application building. So I'm mostly just looking to build something that will save me time.

So the library I'm writing has neither a priori knowledge of the classes it's creating nor of anything beyond the basic structure (delimited tabular text, Excel, etc) of the file it's using. Preferably it should be minimally invasive.

I've done something similar in Python before, but it was invasive in that I either had to use descriptors that specified the needed types, or I needed to make the descriptors all accept strings and do the conversion to the appropriate type. I figure with Scala's rich type information I can be less invasive.


It is possible to create a manifest, reflectively, at runtime.

Enter the TypeWrangler here on gitub...

Just remove thge last two methods of that class (which are specific to Spring), and you're good to go!

One of these days I'll flesh out that code into a full reflection library, also using some of the signature logic in Scalap. It seems that the demand exists for something like that.

0

精彩评论

暂无评论...
验证码 换一张
取 消