Skip to content

Commit 8972821

Browse files
committed
Rendering stale var #21
1 parent c471e6a commit 8972821

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- #8 Java package change
66
- #11 Enable GitHub Actions
77
- #16 Upgrade gson transitive dependency
8+
- #21 Rendering stale var
89

910
# 1.0-M1
1011

src/main/java/org/dflib/jjava/execution/CodeEvaluator.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.dflib.jjava.JavaKernel;
2727
import jdk.jshell.*;
2828

29+
import java.lang.reflect.InvocationTargetException;
2930
import java.lang.reflect.Method;
3031
import java.util.List;
3132
import java.util.regex.Matcher;
@@ -46,6 +47,7 @@ public class CodeEvaluator {
4647
throw new RuntimeException("Unable to access jdk.jshell.Snippet.classFullName() method.", e);
4748
}
4849
}
50+
private static final String INDENTATION = " ";
4951

5052
private final JShell shell;
5153
private final JJavaExecutionControlProvider executionControlProvider;
@@ -55,7 +57,6 @@ public class CodeEvaluator {
5557
private boolean isInitialized = false;
5658
private final List<String> startupScripts;
5759

58-
private final String indentation = " ";
5960

6061
public CodeEvaluator(JShell shell, JJavaExecutionControlProvider executionControlProvider, String executionControlID, List<String> startupScripts) {
6162
this.shell = shell;
@@ -172,14 +173,26 @@ public Object eval(String code) throws Exception {
172173
/**
173174
* Try to clean up information linked to a code snippet and the snippet itself
174175
*/
175-
private void dropSnippet(Snippet snippet)
176-
throws Exception {
176+
private void dropSnippet(Snippet snippet) throws Exception {
177177
JJavaExecutionControl executionControl =
178178
this.executionControlProvider.getRegisteredControlByID(this.executionControlID);
179179
this.shell.drop(snippet);
180180
// snippet.classFullName() returns name of a wrapper class created for a snippet
181-
String className = SNIPPET_CLASS_NAME_METHOD.invoke(snippet).toString();
182-
executionControl.unloadClass(className);
181+
String className = snippetClassName(snippet);
182+
// check that this class is not used by other snippets
183+
if(this.shell.snippets()
184+
.map(this::snippetClassName)
185+
.noneMatch(className::equals)) {
186+
executionControl.unloadClass(className);
187+
}
188+
}
189+
190+
private String snippetClassName(Snippet snippet) {
191+
try {
192+
return SNIPPET_CLASS_NAME_METHOD.invoke(snippet).toString();
193+
} catch (IllegalAccessException | InvocationTargetException e) {
194+
throw new RuntimeException(e);
195+
}
183196
}
184197

185198
private String computeIndentation(String partialStatement) {
@@ -222,7 +235,7 @@ private String computeIndentation(String partialStatement) {
222235
}
223236

224237
return newlyOpenedBraces > 0 || newlyOpenedParens > 0
225-
? currentIndentation + this.indentation
238+
? currentIndentation + INDENTATION
226239
: currentIndentation;
227240
}
228241

0 commit comments

Comments
 (0)