本文整理汇总了Java中jdk.jshell.SnippetEvent类的典型用法代码示例。如果您正苦于以下问题:Java SnippetEvent类的具体用法?Java SnippetEvent怎么用?Java SnippetEvent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SnippetEvent类属于jdk.jshell包,在下文中一共展示了SnippetEvent类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
}
}
}
开发者ID:dmac100,项目名称:JShellScriptEngine,代码行数:20,代码来源:JShellScriptEngine.java
示例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));
}
}
}
});
}
开发者ID:dmac100,项目名称:JShellScriptEngine,代码行数:23,代码来源:JShellScriptEngine.java
示例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;
}
开发者ID:apache,项目名称:incubator-netbeans,代码行数:24,代码来源:JShellTool.java
示例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();
}
}
}
}
开发者ID:apache,项目名称:incubator-netbeans,代码行数:19,代码来源:SnippetNodes.java
示例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;
}
}
开发者ID:apache,项目名称:incubator-netbeans,代码行数:24,代码来源:SnippetCollector.java
示例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;
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:JShellTool.java
示例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();
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:ForwardReferenceTest.java
示例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));
}
}
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:KullaTesting.java
示例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();
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:VariablesTest.java
示例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();
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:RejectedFailedTest.java
示例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)));
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:ExceptionsTest.java
示例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());
}
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:ExceptionsTest.java
示例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;
}
开发者ID:campolake,项目名称:openjdk9,代码行数:24,代码来源:JShellTool.java
示例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();
}
开发者ID:campolake,项目名称:openjdk9,代码行数:24,代码来源:ReplaceTest.java
示例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();
}
开发者ID:campolake,项目名称:openjdk9,代码行数:22,代码来源:RejectedFailedTest.java
示例16: start
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
@Override
public void start(Stage primaryStage) throws Exception {
JShell shell = JShell.builder().build();
TextField textField = new TextField();
Button evalButton = new Button("eval");
ListView<String> listView = new ListView<>();
evalButton.setOnAction(e -> {
List<SnippetEvent> events = shell.eval(textField.getText());
events.stream().map(event -> convert(event)).filter(s -> s != null).forEach(s -> listView.getItems().add(s));
});
BorderPane pane = new BorderPane();
pane.setTop(new HBox(textField, evalButton));
pane.setCenter(listView);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
开发者ID:AdoptOpenJDK,项目名称:jdk9-jigsaw,代码行数:23,代码来源:ShellFX.java
示例17: jshell
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
private void jshell(@NotNull JShell shell, @NotNull String s, boolean showLog) throws JShellException {
if (showLog) {
log.info("$ {}", s);
}
List<SnippetEvent> eval = shell.eval(s);
if (showLog) {
if (eval.isEmpty()) {
log.info("> ");
}
for (SnippetEvent event : eval) {
showResult(event);
}
}
}
开发者ID:sillelien,项目名称:dollar,代码行数:19,代码来源:Java9ScriptingLanguage.java
示例18: main
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
public static void main(String[] args) {
JShell myShell = JShell.create();
System.out.println("Welcome to JShell Java API Demo");
System.out.println("Please Enter a Snippet. Enter EXIT to exit:");
try(Scanner reader = new Scanner(System.in)){
while(true){
String snippet = reader.nextLine();
if ( "EXIT".equals(snippet)){
break;
}
List<SnippetEvent> events = myShell.eval(snippet);
events.stream().forEach(se -> {
System.out.print("Evaluation status: " + se.status());
System.out.println(" Evaluation result: " + se.value());
});
}
}
System.out.println("Snippets processed: ");
myShell.snippets().forEach(s -> {
String msg = String.format("%s -> %s", s.kind(), s.source());
System.out.println(msg);
});
System.out.println("Methods: ");
myShell.methods().forEach(m -> System.out.println(m.name() + " " + m.signature()));
System.out.println("Variables: ");
myShell.variables().forEach(v -> System.out.println(v.typeName() + " " + v.name()));
myShell.close();
}
开发者ID:PacktPublishing,项目名称:Java-9-Cookbook,代码行数:32,代码来源:JshellJavaApiDemo.java
示例19: accept
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
@Override
public void accept(SnippetEvent e) {
SnippetHandle handle = snippetRegistry.installSnippet(
e.snippet(), null, 0, true);
// create an indexed file for the snippet.
snippetRegistry.snippetFile(handle, 0);
}
开发者ID:apache,项目名称:incubator-netbeans,代码行数:8,代码来源:ShellSession.java
示例20: doTest
import jdk.jshell.SnippetEvent; //导入依赖的package包/类
private void doTest(JShell jshell, String label, String code, String expected) {
List<SnippetEvent> result = jshell.eval(code);
assertEquals(result.size(), 1, "Expected only one event");
SnippetEvent evt = result.get(0);
Exception exc = evt.exception();
String out = exc.getMessage();
assertEquals(out, expected, "Exception message not as expected: " +
label + " -- " + code);
}
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:ExceptionMessageTest.java
注:本文中的jdk.jshell.SnippetEvent类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论