Please view the edits below
I'm trying to create a JShell instance that gives me access to, and lets me interact with objects in the JVM it was created in. This works fine with classes that have been available at compile time but fails for classes that are loaded dynamically.
public class Main {
public static final int A = 1;
public static Main M;
public static void main(String[] args) throws Exception {
M = new Main();
ClassLoader cl = new URLClassLoader(new URL[]{new File("Example.jar").toURL()}, Main.class.getClassLoader());
Class<?> bc = cl.loadClass("com.example.test.Dynamic");//Works
JShell shell = JShell.builder()
.executionEngine(new ExecutionControlProvider() {
@Override
public String name() {
return "direct";
}
@Override
public ExecutionControl generate(ExecutionEnv ee, Map<String, String> map) throws Throwable {
return new DirectExecutionControl();
}
}, null)
.build();
shell.eval("System.out.println(com.example.test.Main.A);");//Always works
shell.eval("System.out.println(com.example.test.Main.M);");//Fails (is null) if executionEngine is not set
shell.eval("System.out.println(com.example.test.Dynamic.class);");//Always fails
}
}
Additionally, exchanging DirectExecutionControl
with LocalExecutionControl
gives the same results, but I do not understand the difference between the two classes.
How would I make classes that are loaded at runtime available to this JShell instance?
Edit: The first part of this question has been solved, below is the updated source code to demonstrate the second part of the issue
public class Main {
public static void main(String[] args) throws Exception {
ClassLoader cl = new URLClassLoader(new URL[]{new File("Example.jar").toURL()}, Main.class.getClassLoader());
Class<?> c = cl.loadClass("com.example.test.C");
c.getDeclaredField("C").set(null, "initial");
JShell shell = JShell.builder()
.executionEngine(new ExecutionControlProvider() {
@Override
public String name() {
return "direct";
}
@Override
public ExecutionControl generate(ExecutionEnv ee, Map<String, String> map) throws Throwable {
return new DirectExecutionControl();
}
}, null)
.build();
shell.addToClasspath("Example.jar");
shell.eval("import com.example.test.C;");
shell.eval("System.out.println(C.C)"); //null
shell.eval("C.C = \"modified\";");
shell.eval("System.out.println(C.C)"); //"modified"
System.out.println(c.getDeclaredField("C").get(null)); //"initial"
}
}
This is the expected output, if the JVM and the JShell instance do not share any memory, however adding com.example.test.C
directly to the project instead of loading it dynamically changes the results as follows:
shell.eval("import com.example.test.C;");
shell.eval("System.out.println(C.C)"); //"initial"
shell.eval("C.C = \"modified\";");
shell.eval("System.out.println(C.C)"); //"modified"
System.out.println(c.getDeclaredField("C").get(null)); //"modified"
Why is the memory between the JVM and the JShell instance not shared for classes loaded at runtime?
EDIT 2: The issue seems to be caused by different class loaders
Executing the following code in the context of the above example:
System.out.println(c.getClassLoader()); //java.net.URLClassLoader
shell.eval("System.out.println(C.class.getClassLoader())"); //jdk.jshell.execution.DefaultLoaderDelegate$RemoteClassLoader
shell.eval("System.out.println(com.example.test.Main.class.getClassLoader())"); //jdk.internal.loader.ClassLoaders$AppClassLoader
This shows, that the same class, com.example.test.C
is loaded by two different classloaders. Is it possible to add the class to the JShell instance without loading it again? If no, why is the statically loaded class already loaded?
only speaking to a small part of this rather substantial question:
LocalExecutionControl extends DirectExecutionControl
and it overrides onlyinvoke(Method method)
, the bodies of which are ...local:
direct:
so the difference between the two classes is that direct invokes the method in the current thread, and local invokes it in a new thread. the same classloader is used in both cases, so you'd expect the same results in terms of sharing memory and loaded classes
The solution is to create a custom
LoaderDelegate
implementation, that supplies instances of already loaded classes instead of loading them again. A simple example is to use the default implementation,DefaultLoaderDelegate
(source) and override thefindClass
method of its internalRemoteClassLoader
To create a working JShell instance, use the following code
Now, there is better and easier solution:
Default execution engine is JDI, but u can switch it to local or own.