Skip to content

Commit 97f52af

Browse files
Dao NgocDao Ngoc
authored andcommitted
Fix #3 Support SBT: Force the REPL to use the same class loader with the target thread, so that they don't see different instances of a static variable of the same class
1 parent 2961627 commit 97f52af

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

agent/src/main/java/scalive/Server.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public static void serve(Socket client, String jarpath, String clId) throws Exce
3838
if (parentCl == null) return;
3939

4040
URLClassLoader cl = createClassLoaderWithReplJars(jarpath, parentCl);
41-
Class<?> repl = Class.forName("scalive.Repl", true, cl);
42-
Method method = repl.getMethod("run", String.class, InputStream.class, OutputStream.class);
4341
String classpath = getClasspathForRepl(cl, parentCl);
44-
method.invoke(null, classpath, in, out);
42+
Class<?> repl = Class.forName("scalive.Repl", true, cl);
43+
Method method = repl.getMethod("run", ClassLoader.class, String.class, InputStream.class, OutputStream.class);
44+
method.invoke(null, cl, classpath, in, out);
4545
} finally {
4646
System.setIn(oldIn);
4747
System.setOut(oldOut);

repl/src/main/java/scalive/Repl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
import java.io.OutputStream;
77
import java.io.PrintWriter;
88

9+
import scala.Option;
910
import scala.tools.nsc.interpreter.ILoop;
1011
import scala.tools.nsc.Settings;
1112

1213
// This must be split to a separate JAR (scalive-repl.jar), different from
1314
// the JAR file that contains the agent (scalive-agent.jar). Otherwise the agent
1415
// can't load this Repl class.
1516
public class Repl {
16-
public static void run(String classpath, InputStream in, OutputStream out) {
17+
public static void run(ClassLoader cl, String classpath, InputStream in, OutputStream out) {
1718
// Without the below settings, there will be error:
1819
// Failed to initialize compiler: object scala.runtime in compiler mirror not found.
1920
// ** Note that as of 2.8 scala does not assume use of the java classpath.
@@ -26,8 +27,15 @@ public static void run(String classpath, InputStream in, OutputStream out) {
2627
//
2728
// http://stackoverflow.com/questions/18150961/scala-runtime-in-compiler-mirror-not-found-but-working-when-started-with-xboo
2829
Settings settings = new Settings();
30+
31+
// http://www.scala-lang.org/old/node/8002
2932
settings.classpath().value_$eq(classpath);
3033

34+
// Without this class loader setting, the REPL and the target process will
35+
// see different instances of a static variable of the same class!
36+
// http://stackoverflow.com/questions/5950025/multiple-instances-of-static-variables
37+
settings.explicitParentLoader_$eq(Option.apply(cl));
38+
3139
ILoop repl = new ILoop(
3240
new BufferedReader(new InputStreamReader(in)),
3341
new PrintWriter(out)

0 commit comments

Comments
 (0)