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