Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Scala reflection with Java reflection

I have a lot of code that has been using Java reflection to invoke arbitrary user-specified methods on Java objects, as part of a DSL.

However, a lot of those Java objects, in practice, are Scala objects, and some of the methods on them are marked with the Scala @deprecated annotation. Unfortunately, Java's annotations mechanism cannot see Scala annotations, and until Scala 2.10 there was no convenient way (that I know of?) to get access to those annotations.

I have finally upgraded to 2.10, and would like to be able to emit deprecation warnings from my DSL. However, I don't want to rewrite the entire evaluator (for now, at least) to use Scala reflection, so is there a convenient way to go from a java.lang.reflect.Method to something I can use Scala reflection on to figure out if the Scala method in question is deprecated? Or will I have to duplicate a lot of the existing method resolution logic using Scala reflection before I can get at that information?

It looks like a JavaToScala class used to exist in Scala reflection, which had a methodToScala method in it that did what I want. The only trace I can find is here, in a google code project. Why was the functionality moved out of core reflection? Or is it still there under a different name?

Edit: It looks like the code in question lives on in scala.reflect.runtime.JavaMirrors, but unfortunately it's private (there's another JavaMirrors that is part of the public API, but doesn't provide any of what I need.) I can't figure out for the life of me if there's a "path" of invocations through a public API that takes me all the way down to the private methodAsScala buried deep within the private JavaMirrors. It's frustrating, to say the least :(

like image 532
Mysterious Dan Avatar asked Mar 29 '13 22:03

Mysterious Dan


People also ask

Is it good to use reflection in Java?

Java Reflection is quite powerful and can be very useful. Java Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time.

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.

What is reflection API in Java & why it is being used?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.

What is TypeTag in Scala?

Like scala. reflect. Manifest , TypeTags can be thought of as objects which carry along all type information available at compile time, to runtime. For example, TypeTag[T] encapsulates the runtime type representation of some compile-time type T .


1 Answers

Here is a quick fix (credits to @EugeneBurmako)

https://gist.github.com/xeno-by/5277805

An issue has been filed for this (credits to @MysteriousDan):

https://issues.scala-lang.org/browse/SI-7317

like image 180
gzm0 Avatar answered Nov 15 '22 20:11

gzm0