|
| 1 | +# ScriptJava |
| 2 | +ScriptJava is a simple command line utility that helps to evaluate simple Java statements at runtime (aka scripting) |
| 3 | + |
| 4 | +## Howto |
| 5 | +Get a hold of a `ScriptJava.jar` from a `binary` folder. Make sure that `java` & `javac` are in the system's PATH. Run in the terminal: |
| 6 | +```bash |
| 7 | +java -jar ScriptJava.jar |
| 8 | +``` |
| 9 | + |
| 10 | +## What can be done |
| 11 | +```java |
| 12 | +"Hello world!" |
| 13 | +1 + 2 |
| 14 | +1 << 2 |
| 15 | +Integer.MAX_VALUE |
| 16 | +``` |
| 17 | + |
| 18 | +```java |
| 19 | +int max = Integer.MIN_VALUE |
| 20 | +int min = Integer.MIN_VALUE |
| 21 | +max - min |
| 22 | +``` |
| 23 | +Note that each statement **must** return something (it cannot be void). Also note that for a single line statements semicolons are optional |
| 24 | + |
| 25 | +```java |
| 26 | +int getInt() { |
| 27 | + return 1 |
| 28 | +} |
| 29 | +getInt() |
| 30 | +``` |
| 31 | + |
| 32 | +```java |
| 33 | +int getOtherInt(int what) { |
| 34 | + for (int i = 0; i < 10; i++) { |
| 35 | + if ((i % what) == 0) return i |
| 36 | + } |
| 37 | + return -1 |
| 38 | +} |
| 39 | +getOtherInt(3) |
| 40 | +``` |
| 41 | + |
| 42 | +### Utility methods |
| 43 | + |
| 44 | +```java |
| 45 | +print(Object o); |
| 46 | +printf(String s, Object... args); |
| 47 | +typeof(Object o); |
| 48 | +``` |
| 49 | + |
| 50 | +For a singleline statements that want to print an object `print` & `printf` commands are the best |
| 51 | +```java |
| 52 | +print(obj) |
| 53 | +``` |
| 54 | + |
| 55 | +If you wish to print somthing to console during some operation use `out.*` (which is `System.out.*`) |
| 56 | +```java |
| 57 | +Runnable makeRunnable() { |
| 58 | + return new Runnable() { |
| 59 | + public void run() { out.println(Thread.currentThread()); } |
| 60 | + }; |
| 61 | +} |
| 62 | +``` |
| 63 | +Note that in this case semicolons are required |
| 64 | + |
| 65 | +### Methods |
| 66 | +All generated methods are static, so you must **not** define a `static` modifier for your methods. |
| 67 | + |
| 68 | +### Imports |
| 69 | +By default each script has these in import section: |
| 70 | +```java |
| 71 | +import java.util.*; |
| 72 | +import static java.lang.System.out; |
| 73 | +``` |
| 74 | +To add a custom import start your command with `import`, like this: |
| 75 | +```java |
| 76 | +import java.util.concurrent.* |
| 77 | +``` |
| 78 | +You **must** import before accessing classes |
| 79 | + |
| 80 | + |
| 81 | +### Void |
| 82 | +if you want to execute a method that returns nothing (and thus cannot be printed) start your command with `void`, for example: |
| 83 | +```java |
| 84 | +void start() |
| 85 | +``` |
| 86 | + |
| 87 | +### Clear |
| 88 | +You might clear a certain scope of current script by evaluating a `clear()` command: |
| 89 | +```java |
| 90 | +clear() // no arguments - clears all |
| 91 | +clear(var) // clears variables |
| 92 | +clear(void) // clears voids |
| 93 | +clear(met) // clears methods |
| 94 | +clear(imp) // clears imports |
| 95 | +``` |
| 96 | +You may composite clear scopes: |
| 97 | +```java |
| 98 | +clear(var void) // clears variables & voids |
| 99 | +clear(imp met) // clears imports & methods |
| 100 | +``` |
| 101 | + |
| 102 | +### Quit |
| 103 | +To exit execution evaluate a `quit()` command |
| 104 | + |
| 105 | +## License |
| 106 | + |
| 107 | +``` |
| 108 | + Copyright 2015 Dimitry Ivanov (mail@dimitryivanov.ru) |
| 109 | +
|
| 110 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 111 | + you may not use this file except in compliance with the License. |
| 112 | + You may obtain a copy of the License at |
| 113 | +
|
| 114 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 115 | +
|
| 116 | + Unless required by applicable law or agreed to in writing, software |
| 117 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 118 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 119 | + See the License for the specific language governing permissions and |
| 120 | + limitations under the License. |
| 121 | +``` |
0 commit comments