本文整理汇总了Java中jdk.jshell.SnippetEvent类的典型用法代码示例。如果您正苦于以下问题:Java SnippetEvent类的具体用法?Java SnippetEvent怎么用?Java SnippetEvent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SnippetEvent类属于jdk.jshell包,在下文中一共展示了SnippetEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: evalAll
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
/**
* Split script into snippets and evaluate them all, returning the last result.
*/
private List<SnippetEvent> evalAll(String script) throws ScriptException {
while(true) {
CompletionInfo completionInfo = jshell.sourceCodeAnalysis().analyzeCompletion(script);
if(!completionInfo.completeness().isComplete()) {
throw new ScriptException("Incomplete script");
}
List<SnippetEvent> result = jshell.eval(completionInfo.source());
script = completionInfo.remaining();
if(script.isEmpty()) {
return result;
}
}
}
示例2: writeVariableValues
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
/**
* Writes the variables into variables in JShell.
*/
private void writeVariableValues(Map<String, Object> variables) {
JShellScriptEngine.variables.set(variables);
variables.forEach((name, value) -> {
if(name.matches("[_a-zA-Z0-9]+")) {
if(name.equals("_")) {
name = "__";
}
String type = getDeclaredType(value);
String command = String.format("%s %s = (%s) %s.getBindingValue(\"%s\");", type, name, type, JShellScriptEngine.class.getName(), name);
List<SnippetEvent> events = jshell.eval(command);
for(SnippetEvent event:events) {
if(event.status() == Status.REJECTED) {
Diag diag = jshell.diagnostics(event.snippet()).findAny().get();
throw new RuntimeException(diag.getPosition() + ": " + diag.getMessage(null));
}
}
}
});
}
示例3: processCompleteSource
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
private boolean processCompleteSource(String source) throws IllegalStateException {
debug("Compiling: %s", source);
boolean failed = false;
boolean isActive = false;
List<SnippetEvent> events = state.eval(source);
for (SnippetEvent e : events) {
// Report the event, recording failure
failed |= handleEvent(e);
// If any main snippet is active, this should be replayable
// also ignore var value queries
isActive |= e.causeSnippet() == null &&
e.status().isActive() &&
e.snippet().subKind() != VAR_VALUE_SUBKIND;
}
// If this is an active snippet and it didn't cause the backend to die,
// add it to the replayable history
if (isActive && live) {
addToReplayHistory(source);
}
return failed;
}
示例4: accept
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
@Override
public void accept(SnippetEvent t) {
Snippet snip = t.snippet();
Snippet.Status stat = t.status();
if (session == null) {
return;
}
if (stat == Snippet.Status.DROPPED ||
stat == Snippet.Status.OVERWRITTEN) {
SnippetHandle h = session.getSnippetRegistry().getHandle(snip);
if (h != null) {
N n = factory.findNode(h);
if (n != null) {
n.fireDisplayNameChange();
}
}
}
}
示例5: snippetChange
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
@Override
public void snippetChange(SnippetEvent ev) {
Snippet snip = ev.snippet();
Status stat = ev.status();
Rng snipRange = getInputRange();
switch (stat) {
case VALID:
case RECOVERABLE_DEFINED:
case RECOVERABLE_NOT_DEFINED: {
SnippetData data = new SnippetData(snip);
data.setRange(snipRange);
snippetData.put(snip, data);
break;
}
case DROPPED:
case OVERWRITTEN:
// rejected, but still may be a part of the parsing, unless replaced
case REJECTED:
break;
}
}
示例6: processCompleteSource
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
boolean processCompleteSource(String source) throws IllegalStateException {
debug("Compiling: %s", source);
boolean failed = false;
boolean isActive = false;
List<SnippetEvent> events = state.eval(source);
for (SnippetEvent e : events) {
// Report the event, recording failure
failed |= handleEvent(e);
// If any main snippet is active, this should be replayable
// also ignore var value queries
isActive |= e.causeSnippet() == null &&
e.status().isActive() &&
e.snippet().subKind() != VAR_VALUE_SUBKIND;
}
// If this is an active snippet and it didn't cause the backend to die,
// add it to the replayable history
if (isActive && live) {
addToReplayHistory(source);
}
return failed;
}
示例7: testForwardVarToClassGeneric
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
public void testForwardVarToClassGeneric() {
DeclarationSnippet a = classKey(assertEval("class A<T> { final T x; A(T v) { this.x = v; } ; T get() { return x; } int core() { return g; } }", added(RECOVERABLE_DEFINED)));
assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "variable g");
List<SnippetEvent> events = assertEval("A<String> as = new A<>(\"hi\");", null,
UnresolvedReferenceException.class, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, null);
SnippetEvent ste = events.get(0);
Snippet assn = ste.snippet();
DeclarationSnippet unsn = ((UnresolvedReferenceException) ste.exception()).getSnippet();
assertEquals(unsn.name(), "A", "Wrong with unresolved");
assertEquals(getState().unresolvedDependencies(unsn).count(), 1, "Wrong size unresolved");
assertEquals(getState().diagnostics(unsn).count(), 0L, "Expected no diagnostics");
Snippet g = varKey(assertEval("int g = 10;", "10",
added(VALID),
ste(a, RECOVERABLE_DEFINED, VALID, false, MAIN_SNIPPET)));
assertEval("A<String> as = new A<>(\"low\");",
ste(MAIN_SNIPPET, VALID, VALID, false, null),
ste(assn, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
assertEval("as.get();", "\"low\"");
assertUnresolvedDependencies(a, 0);
assertActiveKeys();
}
示例8: assertKeyMatch
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
private void assertKeyMatch(SnippetEvent ste, Snippet sn, Snippet expected, Snippet mainSnippet) {
Snippet testKey = expected;
if (testKey != null) {
if (expected == MAIN_SNIPPET) {
assertNotNull(mainSnippet, "MAIN_SNIPPET used, test must pass value to assertMatch");
testKey = mainSnippet;
}
if (ste.causeSnippet() == null && ste.status() != DROPPED && expected != MAIN_SNIPPET) {
// Source change, always new snippet -- only match id()
assertTrue(sn != testKey,
"Main-event: Expected new snippet to be != : " + testKey
+ "\n got-event: " + toString(ste));
assertEquals(sn.id(), testKey.id(), "Expected IDs to match: " + testKey + ", got: " + sn
+ "\n expected-event: " + this + "\n got-event: " + toString(ste));
} else {
assertEquals(sn, testKey, "Expected key to be: " + testKey + ", got: " + sn
+ "\n expected-event: " + this + "\n got-event: " + toString(ste));
}
}
}
示例9: multiVariables
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
public void multiVariables() {
List<SnippetEvent> abc = assertEval("int a, b, c = 10;",
DiagCheck.DIAG_OK, DiagCheck.DIAG_OK,
chain(added(VALID)),
chain(added(VALID)),
chain(added(VALID)));
Snippet a = abc.get(0).snippet();
Snippet b = abc.get(1).snippet();
Snippet c = abc.get(2).snippet();
assertVariables(variable("int", "a"), variable("int", "b"), variable("int", "c"));
assertEval("double a = 1.4, b = 8.8;", DiagCheck.DIAG_OK, DiagCheck.DIAG_OK,
chain(ste(MAIN_SNIPPET, VALID, VALID, true, null), ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET)),
chain(ste(MAIN_SNIPPET, VALID, VALID, true, null), ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));
assertVariables(variable("double", "a"), variable("double", "b"), variable("int", "c"));
assertEval("double c = a + b;",
ste(MAIN_SNIPPET, VALID, VALID, true, null),
ste(c, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
assertVariables(variable("double", "a"), variable("double", "b"), variable("double", "c"));
assertActiveKeys();
}
示例10: bad
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
private String bad(String input, Kind kind, String prevId) {
List<SnippetEvent> events = assertEvalFail(input);
assertEquals(events.size(), 1, "Expected one event, got: " + events.size());
SnippetEvent e = events.get(0);
List<Diag> diagnostics = getState().diagnostics(e.snippet()).collect(toList());
assertTrue(diagnostics.size() > 0, "Expected diagnostics, got none");
assertEquals(e.exception(), null, "Expected exception to be null.");
assertEquals(e.value(), null, "Expected value to be null.");
Snippet key = e.snippet();
assertTrue(key != null, "key must be non-null, but was null.");
assertEquals(key.kind(), kind, "Expected kind: " + kind + ", got: " + key.kind());
SubKind expectedSubKind = kind == Kind.ERRONEOUS ? SubKind.UNKNOWN_SUBKIND : SubKind.METHOD_SUBKIND;
assertEquals(key.subKind(), expectedSubKind, "SubKind: ");
assertTrue(key.id().compareTo(prevId) > 0, "Current id: " + key.id() + ", previous: " + prevId);
assertEquals(getState().diagnostics(key).collect(toList()), diagnostics, "Expected retrieved diagnostics to match, but didn't.");
assertEquals(key.source(), input, "Expected retrieved source: " +
key.source() + " to match input: " + input);
assertEquals(getState().status(key), Status.REJECTED, "Expected status of REJECTED, got: " + getState().status(key));
return key.id();
}
示例11: throwFromLocalClass
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
public void throwFromLocalClass() {
String message = "local";
Snippet s1 = methodKey(assertEval(
"void f() {\n" +
" class A {\n" +
" void f() {\n"+
" throw new RuntimeException(\"" + message + "\");\n" +
" }\n" +
" }\n" +
" new A().f();\n" +
"}"
));
SnippetEvent cr2 = assertEvalException("f();");
assertExceptionMatch(cr2,
new ExceptionInfo(RuntimeException.class, message,
newStackTraceElement("1A", "f", s1, 4),
newStackTraceElement("", "f", s1, 7),
newStackTraceElement("", "", cr2.snippet(), 1)));
}
示例12: assertExceptionMatch
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
private void assertExceptionMatch(SnippetEvent cr, ExceptionInfo exceptionInfo) {
assertNotNull(cr.exception(), "Expected exception was not thrown: " + exceptionInfo.exception);
if (cr.exception() instanceof EvalException) {
EvalException ex = (EvalException) cr.exception();
String actualException = ex.getExceptionClassName();
String expectedException = exceptionInfo.exception.getCanonicalName();
String stackTrace = getStackTrace(ex);
String source = cr.snippet().source();
assertEquals(actualException, expectedException,
String.format("Given \"%s\" expected exception: %s, got: %s%nStack trace:%n%s",
source, expectedException, actualException, stackTrace));
if (exceptionInfo.message != null) {
assertEquals(ex.getMessage(), exceptionInfo.message,
String.format("Given \"%s\" expected message: %s, got: %s",
source, exceptionInfo.message, ex.getMessage()));
}
if (exceptionInfo.stackTraceElements != null) {
assertStackTrace(ex.getStackTrace(), exceptionInfo.stackTraceElements,
String.format("Given \"%s\"%nStack trace:%n%s%n",
source, stackTrace));
}
} else {
fail("Unexpected execution exceptionInfo: " + cr.exception());
}
}
示例13: processCompleteSource
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
private boolean processCompleteSource(String source) throws IllegalStateException {
debug("Compiling: %s", source);
boolean failed = false;
boolean isActive = false;
List<SnippetEvent> events = state.eval(source);
for (SnippetEvent e : events) {
// Report the event, recording failure
failed |= handleEvent(e);
// If any main snippet is active, this should be replayable
// also ignore var value queries
isActive |= e.causeSnippet() == null &&
e.status().isActive &&
e.snippet().subKind() != VAR_VALUE_SUBKIND;
}
// If this is an active snippet and it didn't cause the backend to die,
// add it to the replayable history
if (isActive && live) {
addToReplayHistory(source);
}
return failed;
}
示例14: testForwardVarToClassGeneric
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
public void testForwardVarToClassGeneric() {
DeclarationSnippet a = classKey(assertEval("class A<T> { final T x; A(T v) { this.x = v; } ; T get() { return x; } int core() { return g; } }", added(RECOVERABLE_DEFINED)));
assertUnresolvedDependencies1(a, RECOVERABLE_DEFINED, "variable g");
List<SnippetEvent> events = assertEval("A<String> as = new A<>(\"hi\");", null,
UnresolvedReferenceException.class, DiagCheck.DIAG_OK, DiagCheck.DIAG_OK, null);
SnippetEvent ste = events.get(0);
Snippet assn = ste.snippet();
DeclarationSnippet unsn = ((UnresolvedReferenceException) ste.exception()).getSnippet();
assertEquals(unsn.name(), "A", "Wrong with unresolved");
assertEquals(getState().unresolvedDependencies(unsn).size(), 1, "Wrong size unresolved");
assertEquals(getState().diagnostics(unsn).size(), 0, "Expected no diagnostics");
Snippet g = varKey(assertEval("int g = 10;", "10",
added(VALID),
ste(a, RECOVERABLE_DEFINED, VALID, false, MAIN_SNIPPET)));
assertEval("A<String> as = new A<>(\"low\");",
ste(MAIN_SNIPPET, VALID, VALID, false, null),
ste(assn, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
assertEval("as.get();", "\"low\"");
assertUnresolvedDependencies(a, 0);
assertActiveKeys();
}
示例15: bad
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
private String bad(String input, Kind kind, String prevId) {
List<SnippetEvent> events = assertEvalFail(input);
assertEquals(events.size(), 1, "Expected one event, got: " + events.size());
SnippetEvent e = events.get(0);
List<Diag> diagnostics = getState().diagnostics(e.snippet());
assertTrue(diagnostics.size() > 0, "Expected diagnostics, got none");
assertEquals(e.exception(), null, "Expected exception to be null.");
assertEquals(e.value(), null, "Expected value to be null.");
Snippet key = e.snippet();
assertTrue(key != null, "key must be non-null, but was null.");
assertEquals(key.kind(), kind, "Expected kind: " + kind + ", got: " + key.kind());
SubKind expectedSubKind = kind == Kind.ERRONEOUS ? SubKind.UNKNOWN_SUBKIND : SubKind.METHOD_SUBKIND;
assertEquals(key.subKind(), expectedSubKind, "SubKind: ");
assertTrue(key.id().compareTo(prevId) > 0, "Current id: " + key.id() + ", previous: " + prevId);
assertEquals(getState().diagnostics(key), diagnostics, "Expected retrieved diagnostics to match, but didn't.");
assertEquals(key.source(), input, "Expected retrieved source: " +
key.source() + " to match input: " + input);
assertEquals(getState().status(key), Status.REJECTED, "Expected status of REJECTED, got: " + getState().status(key));
return key.id();
}