aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/scala/org/perl8/test/harness/package.scala
blob: 919abc296ddfc984ffeb7f7b9d51f18e37b4819d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package org.perl8.test

/** Classes to handle running test instances and providing output. */
package object harness {
  import scala.reflect.{ClassTag,classTag}

  /** Loads `className`, returning the
    * [[http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html Class]]
    * instance.
    */
  def loadClass[T: ClassTag] (className: String): Class[_] =
    classTag[T].runtimeClass.getClassLoader.loadClass(className)

  /** Loads `className` and creates a new instance of it, using the
    * no-argument constructor.
    */
  def newInstance[T: ClassTag] (className: String): T =
    loadClass[T](className).newInstance.asInstanceOf[T]

  /** Loads `className` and creates a new instance of it, using a
    * one-argument constructor. Passes `arg` as the argument to the
    * constructor.
    */
  def newInstance[T: ClassTag, U <: AnyRef: ClassTag] (
    className: String,
    arg: U
  ): T = {
    val classObj = loadClass[T](className)
    val argClassObj = classTag[U].runtimeClass
    val constructor = classObj.getConstructor(argClassObj)
    constructor.newInstance(arg).asInstanceOf[T]
  }
}