Class RunningJVM
java.lang.Object
uno.anahata.ai.tools.spi.RunningJVM
Provides tools for the AI model to compile and execute Java source code
within the application's running JVM.
This enables a powerful "hot-reload" workflow where the model can write, test, and run code dynamically. It uses an in-memory file manager and a custom classloader to ensure that newly compiled classes take precedence.
The Hot-Reload Mechanism:
When code is compiled via compileJava(String, String, String, String[]), the resulting
bytecode is stored in memory. A custom URLClassLoader is then created with a
Child-First strategy:
- It first checks if the class is the one just compiled in memory.
- If not, it attempts to load the class from the
extraClassPath(e.g., a project'starget/classes). - Only if the class is not found in either location does it delegate to the parent classloader (e.g. the IDE's classpath or the host application's classpath).
This ensures that any changes you make to the code are reflected immediately, even if a version of that class already exists in the IDE's main classpath.
- Author:
- anahata
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectcompileAndExecuteJava(String sourceCode, String extraClassPath, String[] compilerOptions) Compiles and executes a Java class namedAnahatathat implementsCallable.static ClasscompileJava(String sourceCode, String className, String extraClassPath, String[] compilerOptions) Compiles Java source code in memory and returns the resulting Class object.static StringGets the current default compiler classpath.static StringReturns a pretty-printed, tree-like representation of the default classpath.static StringInitializes the default compiler classpath from the system property.static voidsetDefaultCompilerClasspath(String defaultCompilerClasspath) Sets a new default compiler classpath.
-
Field Details
-
chatTemp
A thread-safe map that can be used by the model to persist state across multiple dynamic code executions. -
defaultCompilerClasspath
The default classpath used by the compiler.
-
-
Constructor Details
-
RunningJVM
public RunningJVM()
-
-
Method Details
-
initDefaultCompilerClasspath
Initializes the default compiler classpath from the system property.- Returns:
- The system classpath.
-
getDefaultCompilerClasspath
Gets the current default compiler classpath.- Returns:
- The classpath string.
-
setDefaultCompilerClasspath
Sets a new default compiler classpath.- Parameters:
defaultCompilerClasspath- The new classpath string.
-
getPrettyPrintedDefaultCompilerClasspath
Returns a pretty-printed, tree-like representation of the default classpath.- Returns:
- The formatted classpath string.
-
compileJava
public static Class compileJava(String sourceCode, String className, String extraClassPath, String[] compilerOptions) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException Compiles Java source code in memory and returns the resulting Class object.- Parameters:
sourceCode- The Java source code to compile.className- The fully qualified name of the class.extraClassPath- Additional classpath entries to include.compilerOptions- Additional options for the Java compiler.- Returns:
- The compiled Class object.
- Throws:
ClassNotFoundException- if the class cannot be found after compilation.NoSuchMethodException- if a required constructor is missing.IllegalAccessException- if the class or constructor is not accessible.InvocationTargetException- if the constructor throws an exception.
-
compileAndExecuteJava
public static Object compileAndExecuteJava(String sourceCode, String extraClassPath, String[] compilerOptions) throws Exception Compiles and executes a Java class namedAnahatathat implementsCallable.- Parameters:
sourceCode- The Java source code.extraClassPath- Additional classpath entries.compilerOptions- Additional compiler options.- Returns:
- The result of the
call()method, ensured to be JSON-serializable. - Throws:
Exception- if compilation or execution fails.
-
