score:6
Accepted answer
currently that's impossible to do with the public api, but there's a jira issue for that: https://issues.scala-lang.org/browse/si-7317. here's a workaround that uses internal apis:
scala> class c { def foo = 2 }defined class c
scala> val foo = typeof[c].declarations.tolist.apply(1).asmethod
foo: reflect.runtime.universe.methodsymbol = method foo
scala> val cm = runtimemirror(getclass.getclassloader)
cm: reflect.runtime.universe.mirror = javamirror...
scala> val cmx = cm.asinstanceof[{
def methodtojava(sym: scala.reflect.internal.symbols#methodsymbol): java.lang.reflect.method
}]
cmx: anyref{def methodtojava(sym: scala.reflect.internal.symbols#methodsymbol): java.lang.reflect.method} = javamirror ...
scala> cmx.methodtojava(
foo.asinstanceof[scala.reflect.internal.symbols#methodsymbol]
)
res1: java.lang.reflect.method = public int c.foo()
Source: stackoverflow.com
Related Query
- How to get reflected runtime Method from AnyRef object?
- Get Scala annotation from Java Method
- How to get Scala List from Java List?
- What does :_* do when calling a Java vararg method from Scala?
- How to call T eq(Object) method of Java interface from Scala?
- Get source code of any class from within a Java program
- How to call main method of a Scala program from the main method of a java program?
- How to access a Java static method from Scala given a type alias for that class it resides in
- Calling Java vararg method from Scala with primitives
- How to declare scala method so that it can be called from Java using varargs style
- Calling a protected static Java method from Scala
- Calling an overloaded java generic method from scala
- How do you get java method annotations to work in scala
- can I get a function from an overloaded method in scala?
- Get ClassTag from reflected Java Class instance
- no access to a java static method from scala
- Calling Java Generic Typed Method from Scala gives a Type mismatch error : Scala
- Play 2 reverse routing, get route from controller method
- using Java generic method from Scala
- Accessing public static java method from scala
- Scala class cant override compare method from Java Interface which extends java.util.comparator
- Scala 2.12.4: Cannot access protected static Java method from another package anymore
- How to call java method taking parameter as List<Class<?>> from Scala
- scala case class copy method is unseen from java code
- Get precise return type from a TypeTag and a method
- Why do I get a "will always yield true" warning when translating the following from Java to Scala?
- Getting value from Hashmap with get or contains with apply method
- Calling Scala Method from Java - Set<Object> or Set<Long>?
- Calling Java method that receive variable amount of parameters from Scala
- Returning typed collection from Scala 2.13 method to Java 8 caller
More Query from same tag
- Sbt: How to define task for all projects?
- How to constrain argument by type member of type parameter in scala?
- How do I "left join" in Scala Array[Array[String]]?
- filter DataFrame with Regex with Spark in Scala
- Skewed Window Function & Hive Source Partitions?
- Retrive ids of ResultSet and return as java.sql.Array
- How to use transactions in a controller in Scala
- How to concatenate two streams in Apache Flink
- Is it possible to automatically pretty print json on REPL with play?
- Unable to authenticate OAuth2 with Akka-Http
- Profiling scala for loops using hprof
- How to use placeholder syntax properly with map?
- How to pass Arrays as arguments to a function in Scala?
- Flattening rows in Spark along with existing columns
- Caused by: java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: scala/runtime/LambdaDeserialize
- Serialize to JSON a list of case classes having common trait
- Spark - aggregated column disappears from a DataFrame after join
- Scala/Python vs. Java: SparkContext.map vs. .filter in PI example?
- Compute Number of Attributes efficiently in flink
- What is the cost of access to a configuration property?
- Importing in Scala - How to add a jar to the classpath permanently
- Can Scala reflection get you the list of type aliases defined in a class?
- how can i get arround reassignement to val , binary search tree scala
- What is the meaning of the chaining of => (rightward double arrow) operator in Scala's function literal?
- Calling Spark Scala Function from Python airflow dag code
- How to parse a Map[String,String] with Argonaut
- Type parameter in play-json read/write macro
- Custom Protocol Handler not passing argument on OSX
- Akka actor avoiding mutating state
- How to convert JsValue to a Model Class with Scala in Play framework?