Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good reflection library available for Scala?

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 reflection 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.

like image 241
Erik Engbrecht Avatar asked Dec 31 '10 23:12

Erik Engbrecht


People also ask

Does Scala have reflection?

Scala reflection enables a form of metaprogramming which makes it possible for programs to modify themselves at compile time. This compile-time reflection is realized in the form of macros, which provide the ability to execute methods that manipulate abstract syntax trees at compile-time.

Where is reflection API used?

The Reflection API is mainly used in: IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc. Debugger and Test Tools etc.

What is spring boot reflection?

Reflection is an API that is used to examine or modify the behavior of methods, classes, and interfaces at runtime.

What is reflection API how are they implemented?

Reflection programming in java helps in retrieving and modifying information about Classes and Class members such variable, methods, constructors. Reflection API in Java can be implemented using classes in java. lang. reflect package and methods of java.


1 Answers

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.

like image 194
Kevin Wright Avatar answered Nov 15 '22 22:11

Kevin Wright