2

I have some code that uses reflection to instantiate a Java or Scala class, allowing a user to specify the name: Assume that loadIt below is a hypothetical method defined using this approach.

def getInstance(name:String, jar:String) = {
   val c:Class[_] = loadIt(name, jar) // load class from the jar
   c.newInstance.asInstanceOf[AnyRef] // return new instance of the class
}

This works fine if name is a Scala class, but not if it is an object. Say I define an object:

object Foo

and call it as:

getInstance("Foo", "someJar.jar")

I get the error:

java.lang.InstantiationException: Foo
    at java.lang.Class.newInstance(Class.java:364)

Is there any way I can properly instantiate the object?

1 Answer 1

4

Found the answer:

Refer to this link. Added the following code:

import scala.reflect.runtime.universe._
def getObjectInstance(clsName: String):AnyRef = {
  val mirror = runtimeMirror(getClass.getClassLoader)
  val module = mirror.staticModule(clsName)
  mirror.reflectModule(module).instance.asInstanceOf[AnyRef]
}
val c:Class[_] = loadIt("Foo", "someJar.jar") // ok 
val o = try c.newInstance.asInstanceOf[AnyRef] catch {
    case i:java.lang.InstantiationException => getObjectInstance("Foo")
}
// works
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.