Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit a9ad8f6

Browse files
committed
Initial commit
0 parents  commit a9ad8f6

15 files changed

Lines changed: 737 additions & 0 deletions

‎.gitignore‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
.gradle
3+
/local.properties
4+
.DS_Store
5+
/build
6+
7+
# Java class files
8+
*.class
9+
10+
# Java class files
11+
*.class
12+
13+
*.iml
14+
15+
/.idea/
16+
17+
/out/
18+
/binary/.cache/

‎README.md‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
```

‎binary/ScriptJava.jar‎

14 KB
Binary file not shown.

‎binary/scriptjava.bat‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java -jar ScriptJava.jar

‎binary/scriptjava.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java -jar ScriptJava.jar

‎src/META-INF/MANIFEST.MF‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: com.company.Main
3+

‎src/com/company/Clear.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.company;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by Dimitry Ivanov (mail@dimitryivanov.ru) on 09.07.2015.
8+
*/
9+
public enum Clear {
10+
11+
VOID ("void"),
12+
VARIABLES ("var"),
13+
METHODS ("met"),
14+
IMPORTS ("imp");
15+
16+
final String value;
17+
18+
Clear(String value) {
19+
this.value = value;
20+
}
21+
22+
static Clear[] forValue(String value) {
23+
24+
if (TextUtils.isEmpty(value)) {
25+
return values();
26+
}
27+
28+
final List<Clear> list = new ArrayList<Clear>();
29+
for (Clear clear: values()) {
30+
if (value.contains(clear.value)) {
31+
list.add(clear);
32+
}
33+
}
34+
35+
final Clear[] clears = new Clear[list.size()];
36+
list.toArray(clears);
37+
return clears;
38+
}
39+
}

‎src/com/company/ConsoleReader.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.company;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
7+
/**
8+
* Created by Dimitry Ivanov (mail@dimitryivanov.ru) on 09.07.2015.
9+
*/
10+
public class ConsoleReader {
11+
12+
public interface Callback {
13+
void onNewLine(String line);
14+
}
15+
16+
private volatile boolean isRunning = true;
17+
18+
public void read(final Callback callback) {
19+
final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
20+
while (isRunning) {
21+
try {
22+
final String line = reader.readLine();
23+
if (line != null) {
24+
callback.onNewLine(line);
25+
}
26+
} catch (IOException e) {
27+
28+
}
29+
}
30+
try {
31+
reader.close();
32+
} catch (IOException e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
37+
public void setRunning(boolean isRunning) {
38+
this.isRunning = isRunning;
39+
}
40+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.company;
2+
3+
/**
4+
* Created by Dimitry Ivanov (mail@dimitryivanov.ru) on 09.07.2015.
5+
*/
6+
public enum ExecutionState {
7+
8+
TRUE (Boolean.TRUE),
9+
FALSE (Boolean.FALSE),
10+
RETURN (null);
11+
12+
final Boolean value;
13+
14+
ExecutionState(Boolean value) {
15+
this.value = value;
16+
}
17+
}

‎src/com/company/Indent.java‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.company;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by Dimitry Ivanov (mail@dimitryivanov.ru) on 09.07.2015.
7+
*/
8+
public class Indent {
9+
10+
private final int length;
11+
12+
private int count;
13+
private String cache;
14+
15+
public Indent(int length) {
16+
this.length = length;
17+
}
18+
19+
public int getCount() {
20+
return count;
21+
}
22+
23+
public void setCount(int count) {
24+
this.count = count;
25+
this.cache = null;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
if (cache == null) {
31+
cache = initCache();
32+
}
33+
return cache;
34+
}
35+
36+
private String initCache() {
37+
38+
final char[] chars = new char[length * count];
39+
Arrays.fill(chars, ' ');
40+
41+
return new String(chars);
42+
}
43+
}

0 commit comments

Comments
 (0)