Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>scala&gt; case class Foo(id:Long, name:String) defined class Foo scala&gt; val constructor = classOf[Foo].getConstructors()(0) constructor: java.lang.reflect.Constructor[_] = public Foo(long,java.lang.String) scala&gt; val args = Array[AnyRef](new java.lang.Integer(1), "Foobar") args: Array[AnyRef] = Array(1, Foobar) scala&gt; val instance = constructor.newInstance(args:_*).asInstanceOf[Foo] instance: Foo = Foo(1,Foobar) scala&gt; instance.id res12: Long = 1 scala&gt; instance.name res13: String = Foobar scala&gt; instance.getClass res14: java.lang.Class[_] = class Foo </code></pre> <p>Currently there is not much reflection support in Scala. But you can fall back to th Java Reflection API. But there are some obstacles:</p> <ul> <li><p>You have to create a <code>Array[AnyRef]</code> and box your "primitive types" in the wrapper classes (<code>java.lang.Integer, java.lang.Character, java.lang.Double, ...</code>)</p></li> <li><p><code>newInstance(Object ... args)</code> gets an varargs array of <code>Object</code>, so you should give the type inferer a hint with <code>:_*</code></p></li> <li><p><code>newInstance(...)</code> returns an <code>Object</code> so you have to cast it back with <code>asInstanceOf[T]</code></p></li> </ul> <p>The closest I could get to your <code>instantiate</code> function is this:</p> <pre><code>def instantiate(clazz: java.lang.Class[_])(args:AnyRef*): AnyRef = { val constructor = clazz.getConstructors()(0) return constructor.newInstance(args:_*).asInstanceOf[AnyRef] } val instance = instantiate(classOf[MyClass])(new java.lang.Integer(42), "foo") println(instance) // prints: MyClass(42,foo) println(instance.getClass) // prints: class MyClass </code></pre> <p>You cannot get the get class from a generic type. Java erases it (type erasure).</p> <p><strong>Edit: 20 September 2012</strong></p> <p>Three years on, the <code>instantiate</code> method can be improved to return a properly typed object.</p> <pre><code>def instantiate[T](clazz: java.lang.Class[T])(args:AnyRef*): T = { val constructor = clazz.getConstructors()(0) return constructor.newInstance(args:_*).asInstanceOf[T] } </code></pre> <p>See <a href="http://www.nabble.com/How-do-I-get-the-class-of-a-Generic--td20873455.html" rel="noreferrer">http://www.nabble.com/How-do-I-get-the-class-of-a-Generic--td20873455.html</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload