diff options
author | Lex Spoon <lex@lexspoon.org> | 2006-06-15 15:15:47 +0000 |
---|---|---|
committer | Lex Spoon <lex@lexspoon.org> | 2006-06-15 15:15:47 +0000 |
commit | 5bae313f42a697ff2c4a55b861254764df071971 (patch) | |
tree | 331c59659a6bf9bd34f2aca069403f65cedde89b /src | |
parent | e8f45e929693736278ac7deed2a909d3fe2d0d5b (diff) | |
download | scala-5bae313f42a697ff2c4a55b861254764df071971.tar.gz scala-5bae313f42a697ff2c4a55b861254764df071971.tar.bz2 scala-5bae313f42a697ff2c4a55b861254764df071971.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/ObjectRunner.scala | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/ObjectRunner.scala b/src/compiler/scala/tools/nsc/ObjectRunner.scala new file mode 100644 index 0000000000..5649fcbc05 --- /dev/null +++ b/src/compiler/scala/tools/nsc/ObjectRunner.scala @@ -0,0 +1,36 @@ +package scala.tools.nsc +import java.net.URLClassLoader +import java.io.File +import java.lang.reflect.{Method,Modifier} + +/** An object that runs another object specified by name. */ +object ObjectRunner { + def isMainMethod(meth: Method): Boolean = { + def paramsOK(params: Array[Class]): Boolean = { + (params.length == 1) && + (params(0) == classOf[Array[String]]) + } + meth.getName == "main" && + Modifier.isStatic(meth.getModifiers) && + paramsOK(meth.getParameterTypes) + } + + def run( + classpath: List[String], + objectName: String, + arguments: Seq[String]): Unit = + { + val classpathURLs = classpath.map(s => new File(s).toURL).toArray + val mainLoader = new URLClassLoader(classpathURLs, null) + val clsToRun = Class.forName(objectName, true, mainLoader) + + val method = clsToRun.getMethods.find(isMainMethod) match { + case Some(meth) => meth + case None => { + throw new Error("no main method in object " + objectName) + } + } + val res = method.invoke(null, List(arguments.toArray).toArray) + () + } +} |