package xsbt
package boot
class PlainApplication private (mainMethod: java.lang.reflect.Method) extends xsbti.AppMain {
override def run(configuration: xsbti.AppConfiguration): xsbti.MainResult = {
val IntClass = classOf[Int]
val ExitClass = classOf[xsbti.Exit]
try mainMethod.getReturnType match {
case ExitClass =>
mainMethod.invoke(null, configuration.arguments).asInstanceOf[xsbti.Exit]
case IntClass =>
PlainApplication.Exit(mainMethod.invoke(null, configuration.arguments).asInstanceOf[Int])
case _ =>
mainMethod.invoke(null, configuration.arguments)
PlainApplication.Exit(0)
} catch {
case e: java.lang.reflect.InvocationTargetException if e.getCause != null =>
throw e.getCause
}
}
}
object PlainApplication {
def isPlainApplication(clazz: Class[_]): Boolean = findMainMethod(clazz).isDefined
def apply(clazz: Class[_]): xsbti.AppMain =
findMainMethod(clazz) match {
case Some(method) => new PlainApplication(method)
case None => sys.error("Class: " + clazz + " does not have a main method!")
}
private def findMainMethod(clazz: Class[_]): Option[java.lang.reflect.Method] =
try {
val method =
clazz.getMethod("main", classOf[Array[String]])
if (java.lang.reflect.Modifier.isStatic(method.getModifiers)) Some(method)
else None
} catch {
case n: NoSuchMethodException => None
}
case class Exit(code: Int) extends xsbti.Exit
}