summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/ObjectRunner.scala
blob: befdddef9b27455bbcd8de140a240a566eb98e1e (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* NSC -- new Scala compiler
 * Copyright 2005-2007 LAMP/EPFL
 * @author  Lex Spoon
 */

// $Id$

package scala.tools.nsc

import java.lang.{Class, ClassNotFoundException, NoSuchMethodException}
import java.lang.reflect.{Method, Modifier}
import java.net.{URL, URLClassLoader}

/** An object that runs another object specified by name.
 *
 *  @author  Lex Spoon
 *  @version 1.1, 2007/7/13
 */
object ObjectRunner {
  /** Create a class loader for the specified class path */
  private def makeClassLoader(classpath: List[URL]) =
    new URLClassLoader(classpath.toArray, null)

  /** Look up a class with a given class path. */
  private def findClass(loader: ClassLoader, objectName: String)
  : Option[Class[T] forSome { type T }] =
  {
    try {
      Some(Class.forName(objectName, true, loader))
    } catch {
      case e: SecurityException =>
        Console.println(e.getMessage)
        None
      case _: ClassNotFoundException =>
        None
    }
  }

  /** Check whether a class with the specified name
   *  exists on the specified class path. */
  def classExists(classpath: List[URL], objectName: String): Boolean =
    !findClass(makeClassLoader(classpath), objectName).isEmpty

  /** Set the Java context class loader while executing an action */
  def withContextClassLoader[T](loader: ClassLoader)(action: =>T): T = {
    val oldLoader = Thread.currentThread.getContextClassLoader
    try {
      Thread.currentThread.setContextClassLoader(loader)
      action
    } finally {
      Thread.currentThread.setContextClassLoader(oldLoader)
    }
  }


  /** Run a given object, specified by name, using a
   *  specified classpath and argument list.
   *
   *  @throws ClassNotFoundException
   *  @throws NoSuchMethodError
   *  @throws InvocationTargetException
   */
  def run(classpath: List[URL], objectName: String, arguments: Seq[String]) {
    val loader = makeClassLoader(classpath)
    val clsToRun = findClass(loader, objectName) match {
      case Some(cls) => cls
      case None => throw new ClassNotFoundException(objectName)
    }

    val method = clsToRun.getMethod("main", List(classOf[Array[String]]).toArray)
    if ((method.getModifiers & Modifier.STATIC) == 0)
      throw new NoSuchMethodException(objectName + ".main is not static")

    withContextClassLoader(loader) {
      method.invoke(null, List(arguments.toArray).toArray)
    }
  }
}