Skip to content

Commit 6c1d461

Browse files
committed
Initial commit: Java runtime code compilation prototype
Add Bazel-based Java project demonstrating runtime code compilation and execution using the Java Compiler API. Core features: - CodeRunner API for compiling and executing Java code from strings - RuntimeCompiler using javax.tools.JavaCompiler for in-memory compilation - CLI interface with built-in examples and file-based code execution - Support for BiFunction<U, D, T> interface with key/keyData parameters - Access to Java standard library and Guava Security considerations: - Prominent warnings about lack of sandboxing - Documentation clearly states prototype/educational use only - Not suitable for production or untrusted input Includes: - 4 core implementation files - 6 working examples (simple, math, list, guava, filter, map) - 4 example code files users can run - Comprehensive README and quick start guide --- Prompt: ``` Create a bazel java project demonstrating the ability to take in code as a string and compile and run it. The idea I have is that our code provides an API * runCode(key: U, code: String) : T We have a that takes in code and returns the output of running that code on the data for that key. The code contains a function in the interface `BiFunction<key: U, keyData: D, output: T>`, and the system compiles the code and runs it. The function is allowed to access the key value and key data, and it can use a known allowlisted set of libraries like Guava or collections. Make a prototype bazel project for this in a form that can shared publicly on github with runnable examples (e.g., typing in code on the CLI or having code in a local file). For the purposes of the prototype, we'll ignore security. Add a clear warning in any readmes we produce. ```
0 parents  commit 6c1d461

15 files changed

Lines changed: 1233 additions & 0 deletions

.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Enable WORKSPACE for Bazel 8
2+
common --enable_workspace

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Bazel
2+
/bazel-*
3+
/.bazel-cache/
4+
5+
# IDE
6+
/.idea/
7+
/.vscode/
8+
/.bsp/
9+
/.bazelbsp/
10+
*.iml
11+
*.swp
12+
*.swo
13+
*~
14+
15+
# OS
16+
.DS_Store
17+
Thumbs.db
18+
19+
# Java
20+
*.class
21+
*.jar
22+
*.war
23+
*.ear
24+
hs_err_pid*
25+
26+
# Logs
27+
*.log

BUILD.bazel

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load("@rules_java//java:defs.bzl", "java_binary", "java_library")
2+
3+
java_library(
4+
name = "code_runner",
5+
srcs = glob(["src/main/java/com/example/coderunner/*.java"]),
6+
visibility = ["//visibility:public"],
7+
deps = [
8+
"@maven//:com_google_guava_guava",
9+
"@maven//:com_google_code_findbugs_jsr305",
10+
],
11+
)
12+
13+
java_binary(
14+
name = "cli",
15+
srcs = ["src/main/java/com/example/coderunner/CLI.java"],
16+
main_class = "com.example.coderunner.CLI",
17+
deps = [
18+
":code_runner",
19+
"@maven//:com_google_guava_guava",
20+
],
21+
)

QUICKSTART.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Quick Start Guide
2+
3+
## ⚠️ Security Warning
4+
This tool executes arbitrary code without sandboxing. **For prototyping only!**
5+
6+
## Run Your First Example
7+
8+
1. **List available examples:**
9+
```bash
10+
bazel run //:cli -- list
11+
```
12+
13+
2. **Run a simple example:**
14+
```bash
15+
bazel run //:cli -- example simple
16+
```
17+
18+
3. **Try the Guava example:**
19+
```bash
20+
bazel run //:cli -- example guava
21+
```
22+
23+
## Run Code from a File
24+
25+
1. **Create a simple Java file** (`MyCode.java`):
26+
```java
27+
import java.util.function.BiFunction;
28+
29+
public class MyCode implements BiFunction<String, String, String> {
30+
@Override
31+
public String apply(String key, String data) {
32+
return key + " processed: " + data;
33+
}
34+
}
35+
```
36+
37+
2. **Run it:**
38+
```bash
39+
bazel run //:cli -- file /path/to/MyCode.java mykey "test data"
40+
```
41+
42+
## Use the API in Your Code
43+
44+
```java
45+
import com.example.coderunner.CodeRunner;
46+
47+
public class Example {
48+
public static void main(String[] args) throws Exception {
49+
CodeRunner<String, String, String> runner = new CodeRunner<>();
50+
51+
String code = """
52+
import java.util.function.BiFunction;
53+
54+
public class MyFunction implements BiFunction<String, String, String> {
55+
public String apply(String key, String data) {
56+
return key + ": " + data.toUpperCase();
57+
}
58+
}
59+
""";
60+
61+
String result = runner.runCode("test", "hello world", code);
62+
System.out.println(result); // Prints: test: HELLO WORLD
63+
}
64+
}
65+
```
66+
67+
## Try the Examples
68+
69+
All examples are in the `examples/` directory. Run them using absolute paths:
70+
71+
```bash
72+
# Word counter
73+
bazel run //:cli -- file $PWD/examples/WordCounter.java doc "hello world test"
74+
75+
# Calculator
76+
bazel run //:cli -- file $PWD/examples/Calculator.java add "10,20"
77+
78+
# List processor (uses Guava)
79+
bazel run //:cli -- file $PWD/examples/ListProcessor.java items "a,b,c,d"
80+
```
81+
82+
## What Can Your Code Do?
83+
84+
Your code can use:
85+
- ✅ Java standard library
86+
- ✅ Guava library
87+
- ✅ Collections and Streams
88+
- ✅ Any Java language features
89+
- ⚠️ **BUT: No sandboxing! Full system access!**
90+
91+
## Next Steps
92+
93+
- Read the [full README](README.md) for details
94+
- Check the [examples directory](examples/) for more samples
95+
- Review [CodeRunner.java](src/main/java/com/example/coderunner/CodeRunner.java) for API details

0 commit comments

Comments
 (0)