|
| 1 | +/* __ *\ |
| 2 | +** ________ ___ / / ___ Scala API ** |
| 3 | +** / __/ __// _ | / / / _ | (c) 2010-2013, LAMP/EPFL ** |
| 4 | +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** |
| 5 | +** /____/\___/_/ |_/____/_/ | | ** |
| 6 | +** |/ ** |
| 7 | +\* */ |
| 8 | + |
| 9 | +package scala |
| 10 | + |
| 11 | +import scala.compat.Platform.currentTime |
| 12 | +import scala.collection.mutable.ListBuffer |
| 13 | + |
| 14 | +/** The `App` trait can be used to quickly turn objects |
| 15 | + * into executable programs. Here is an example: |
| 16 | + * {{{ |
| 17 | + * object Main extends App { |
| 18 | + * Console.println("Hello World: " + (args mkString ", ")) |
| 19 | + * } |
| 20 | + * }}} |
| 21 | + * Here, object `Main` inherits the `main` method of `App`. |
| 22 | + * |
| 23 | + * `args` returns the current command line arguments as an array. |
| 24 | + * |
| 25 | + * ==Caveats== |
| 26 | + * |
| 27 | + * '''''It should be noted that this trait is implemented using the [[DelayedInit]] |
| 28 | + * functionality, which means that fields of the object will not have been initialized |
| 29 | + * before the main method has been executed.''''' |
| 30 | + * |
| 31 | + * It should also be noted that the `main` method should not be overridden: |
| 32 | + * the whole class body becomes the “main method”. |
| 33 | + * |
| 34 | + * Future versions of this trait will no longer extend `DelayedInit`. |
| 35 | + * |
| 36 | + * @author Martin Odersky |
| 37 | + * @version 2.1, 15/02/2011 |
| 38 | + */ |
| 39 | +trait App extends DelayedInit { |
| 40 | + |
| 41 | + /** The time when the execution of this program started, in milliseconds since 1 |
| 42 | + * January 1970 UTC. */ |
| 43 | + @deprecatedOverriding("executionStart should not be overridden", "2.11.0") |
| 44 | + val executionStart: Long = currentTime |
| 45 | + |
| 46 | + /** The command line arguments passed to the application's `main` method. |
| 47 | + */ |
| 48 | + @deprecatedOverriding("args should not be overridden", "2.11.0") |
| 49 | + protected def args: Array[String] = _args |
| 50 | + |
| 51 | + private var _args: Array[String] = _ |
| 52 | + |
| 53 | + private val initCode = new ListBuffer[() => Unit] |
| 54 | + |
| 55 | + /** The init hook. This saves all initialization code for execution within `main`. |
| 56 | + * This method is normally never called directly from user code. |
| 57 | + * Instead it is called as compiler-generated code for those classes and objects |
| 58 | + * (but not traits) that inherit from the `DelayedInit` trait and that do not |
| 59 | + * themselves define a `delayedInit` method. |
| 60 | + * @param body the initialization code to be stored for later execution |
| 61 | + */ |
| 62 | + @deprecated("the delayedInit mechanism will disappear", "2.11.0") |
| 63 | + override def delayedInit(body: => Unit) { |
| 64 | + initCode += (() => body) |
| 65 | + } |
| 66 | + |
| 67 | + /** The main method. |
| 68 | + * This stores all arguments so that they can be retrieved with `args` |
| 69 | + * and then executes all initialization code segments in the order in which |
| 70 | + * they were passed to `delayedInit`. |
| 71 | + * @param args the arguments passed to the main method |
| 72 | + */ |
| 73 | + @deprecatedOverriding("main should not be overridden", "2.11.0") |
| 74 | + def main(args: Array[String]) = { |
| 75 | + this._args = args |
| 76 | + for (proc <- initCode) proc() |
| 77 | + if (util.Properties.propIsSet("scala.time")) { |
| 78 | + val total = currentTime - executionStart |
| 79 | + Console.println("[total " + total + "ms]") |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments