|
| 1 | +package jsheets.runtime.evaluation.shell; |
| 2 | + |
| 3 | +import java.lang.reflect.Field; |
| 4 | +import java.lang.reflect.InaccessibleObjectException; |
| 5 | +import java.lang.reflect.Method; |
| 6 | + |
| 7 | +public final class ExhaustiveEvaluation { |
| 8 | + private record WrapType(Method firstIndex, Method lastIndex) {} |
| 9 | + |
| 10 | + private static final String internalWrapTypeName = "jdk.jshell.GeneralWrap"; |
| 11 | + |
| 12 | + private static WrapType resolveWrapType() { |
| 13 | + try { |
| 14 | + var wrapType = Class.forName(internalWrapTypeName); |
| 15 | + var firstIndex = wrapType.getDeclaredMethod("firstSnippetIndex"); |
| 16 | + firstIndex.setAccessible(true); |
| 17 | + var secondIndex = wrapType.getDeclaredMethod("lastSnippetIndex"); |
| 18 | + secondIndex.setAccessible(true); |
| 19 | + return new WrapType(firstIndex, secondIndex); |
| 20 | + } catch (InaccessibleObjectException missingAccess) { |
| 21 | + throw new RuntimeException("could not open " + internalWrapTypeName); |
| 22 | + } catch (ClassNotFoundException | NoSuchMethodException incompatible) { |
| 23 | + throw new RuntimeException("incompatible jshell version", incompatible); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + private final int statementLimit; |
| 28 | + private final WrapType wrapType; |
| 29 | + private final Field wrapField; |
| 30 | + |
| 31 | + private ExhaustiveEvaluation() {} |
| 32 | + |
| 33 | + public static void main(String[] options) { |
| 34 | + var shell = jdk.jshell.JShell.builder() |
| 35 | + .err(System.err) |
| 36 | + .out(System.out) |
| 37 | + .build(); |
| 38 | + var originalSource = """ |
| 39 | + int x = 10; |
| 40 | + System.out.println(x + "a"); |
| 41 | + x = 20; |
| 42 | + System.out.println(x + "b"); |
| 43 | + """.trim(); |
| 44 | + var source = originalSource; |
| 45 | + var snippets = new java.util.ArrayList<jdk.jshell.Snippet>(); |
| 46 | + |
| 47 | + for (int index = 0; index < 100 && !source.isEmpty(); index++) { |
| 48 | + var snippet = shell.eval(source).get(0).snippet(); |
| 49 | + var range = rangeInText(snippet); |
| 50 | + snippets.add(snippet); |
| 51 | + if (range.end() >= source.length()) { |
| 52 | + break; |
| 53 | + } |
| 54 | + source = originalSource.substring(range.end()); |
| 55 | + } |
| 56 | + System.out.println(snippets); |
| 57 | + } |
| 58 | + |
| 59 | + record Range(int start, int end) {} |
| 60 | + |
| 61 | + private static Range rangeInText(jdk.jshell.Snippet snippet) { |
| 62 | + try { |
| 63 | + var accessor = jdk.jshell.Snippet.class.getDeclaredMethod("guts"); |
| 64 | + accessor.setAccessible(true); |
| 65 | + var wrap = accessor.invoke(snippet); |
| 66 | + return rangeInWrap(wrap); |
| 67 | + } catch (Exception failedAccess) { |
| 68 | + throw new RuntimeException(failedAccess); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + static Class<?> wrapType; |
| 73 | + |
| 74 | + |
| 75 | + static Range rangeInWrap(Object wrap) { |
| 76 | + try { |
| 77 | + (int) firstIndex.invoke(wrap), |
| 78 | + (int) secondIndex.invoke(wrap) |
| 79 | + ); |
| 80 | + } catch (Exception failedAccess) { |
| 81 | + throw new RuntimeException(failedAccess); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
0 commit comments