score:15
For classes, this can be done pretty easily using the standard java reflection classOf method. For Scala objects, it is a bit more work, but it still can be done:
trait SomeTrait { def someMethod: String}
object SomeObject extends SomeTrait { def someMethod = "something"}
class SomeClass extends SomeTrait { def someMethod = "something"}
object Main {
def main(args:Array[String]) = {
val someClassTrait:SomeTrait = Class.forName("SomeClass").newInstance().asInstanceOf[SomeTrait]
println("calling someClassTrait: " + someClassTrait.someMethod)
val objectName = "SomeObject$"
val cons = Class.forName(objectName).getDeclaredConstructors();
cons(0).setAccessible(true);
val someObjectTrait:SomeTrait = cons(0).newInstance().asInstanceOf[SomeTrait]
println("calling someObjectTrait: " + someObjectTrait.someMethod)
}
}
//prints:
calling someClassTrait: something
calling someObjectTrait: something
score:19
Since scala 2.10, we can use the reflection of Module:
import scala.reflect.runtime.universe
val runtimeMirror = universe.runtimeMirror(getClass.getClassLoader)
val module = runtimeMirror.staticModule("SomeObject")
val obj = runtimeMirror.reflectModule(module)
val someTrait:SomeTrait = obj.instance.asInstanceOf[SomeTrait]
someTrait.someMethod
score:25
def companion[T](name : String)(implicit man: Manifest[T]) : T =
Class.forName(name + "$").getField("MODULE$").get(man.erasure).asInstanceOf[T]
val result = companion[SomeTrait]("SomeObject").someMethod
Source: stackoverflow.com
Related Query
- How to call the object method (not class method) using reflection in scala
- How do I call a Scala Object method using reflection?
- How can I reflectively call a method on a Scala object from Java?
- How can I call a method defined on a Scala package object from Java? (Scala 2.10.x)
- How to instantiate a Scala object using reflection
- How to call a function on an object with a generic type parameter using scala macros?
- How to call Databricks dbutils using Scala Reflection / Mirrors
- Using Scala reflection to check for a method on an object or to find key type on a Map
- Dynamic object method invocation using reflection in scala
- Scala Reflection to generate a companion object and call the apply method
- How to override generic method in scala and call it with reflection
- Scala: How to transform a POJO like object into a SQL insert statement using Scala reflection
- How to call a method in other class/object in Scala using azure Data bricks Notebooks
- Scala: How do I dynamically instantiate an object and invoke a method using reflection?
- How do you call a Scala singleton method from Java?
- Why scala uses reflection to call method on structural type?
- How to stub a method call with an implicit matcher in Mockito and Scala
- How do I call a Java method named the same as a Scala keyword?
- How to call main method of a Scala program from the main method of a java program?
- How to mock a function within Scala object using Mockito?
- How to create a synchronized object method in scala
- How to call the correct method in Scala/Java based the types of two objects without using a switch statement?
- How to declare scala method so that it can be called from Java using varargs style
- How do I get a scala macro to replace a method call
- Using Scala 2.10 reflection how can I list the values of Enumeration?
- How to add a json object in to a json array using scala play?
- How to check which parameters of case class have default value using scala reflection 2.10
- How can I get all object vals and subobject vals using reflection in Scala?
- How to access all public members of an object recursively using Scala reflection?
- How to create a decent toString() method in scala using reflection?
More Query from same tag
- Anorm and get[Double]
- Scala non-generic collection
- Scala: How to get class of mixin composition?
- Efficient way to convert all data frame columns in Scala
- Play2 - validation of dynamic part of route(s)
- Scala: Have the type parameter of a collection survive a "collect" when the type parameter is a member type
- BufferedReader readLine return value not understood as string in scala?
- Play FrameWork / scala - return a streamed response from an Array[Byte]
- When exactly I have to use State Monad or .copy?
- In Scala's repl, programmatically find implicitly added functions
- Generic column access for scala matrix
- scala - to avoid creating empty avro file (or handling the number of files)
- Connect my code with elasticsearch
- How can I rename a field during serialization with Json4s?
- How to submit spark job Remotely
- Scala Swing Wait
- Iterator of repeated words in a file
- Spark dataframe to nested map
- akka.actor.ActorLogging does not log the stack trace of exception by logback
- Scala read-eval-print using RegexParser without so much boilerplate?
- How to use implicitly to find a conversion to a type with a method name?
- Error while finding lines starting with H or I using Scala
- Make Argument Lazy without Changing Function Signature?
- CountWord using spark on cluster azure
- Filesystem provider disappearing in Spark?
- Group By including empty rows
- Construct XML with dynamic label and attributes in Scala?
- how to set main class in SBT 0.13 project
- Is the Scala 2.8 collections library a case of "the longest suicide note in history"?
- How do I print a CSV line from repeated parameters in Scala?