33362
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/**
|
|
27 |
* Provides interfaces for creating tools, such as a Read-Eval-Print Loop (REPL),
|
|
28 |
* which interactively evaluate "snippets" of Java programming language code.
|
|
29 |
* Where a "snippet" is a single expression, statement, or declaration.
|
|
30 |
* This functionality can be used to enhance tools such as IDEs or can be
|
|
31 |
* stand-alone.
|
|
32 |
* <p>
|
|
33 |
* {@link jdk.jshell.JShell} is the central class. An instance of
|
|
34 |
* <code>JShell</code> holds the evaluation state, which is both the current
|
|
35 |
* set of source snippets and the execution state they have produced.
|
|
36 |
* <p>
|
|
37 |
* Each source snippet is represented by an instance of a subclass of
|
|
38 |
* {@link jdk.jshell.Snippet}. For example, a statement is represented by an
|
|
39 |
* instance of {@link jdk.jshell.StatementSnippet}, and a method declaration is
|
|
40 |
* represented by an instance of {@link jdk.jshell.MethodSnippet}.
|
|
41 |
* Snippets are created when {@link jdk.jshell.JShell#eval(java.lang.String)}
|
|
42 |
* is invoked with an input which includes one or more snippets of code.
|
|
43 |
* <p>
|
|
44 |
* Any change to the compilation status of a snippet is reported with a
|
|
45 |
* {@link jdk.jshell.SnippetEvent}. There are three major kinds of
|
|
46 |
* changes to the status of a snippet: it can created with <code>eval</code>,
|
|
47 |
* it can be dropped from the active source state with
|
|
48 |
* {@link jdk.jshell.JShell#drop(jdk.jshell.PersistentSnippet)}, and it can have
|
|
49 |
* its status updated as a result of a status change in another snippet.
|
|
50 |
* For
|
|
51 |
* example: given <code>js</code>, an instance of <code>JShell</code>, executing
|
|
52 |
* <code>js.eval("int x = 5;")</code> will add the variable <code>x</code> to
|
|
53 |
* the source state and will generate an event describing the creation of a
|
|
54 |
* {@link jdk.jshell.VarSnippet} for <code>x</code>. Then executing
|
|
55 |
* <code>js.eval("int timesx(int val) { return val * x; }")</code> will add
|
|
56 |
* a method to the source state and will generate an event
|
|
57 |
* describing the creation of a {@link jdk.jshell.MethodSnippet} for
|
|
58 |
* <code>timesx</code>.
|
|
59 |
* Assume that <code>varx</code> holds the snippet created by the first
|
|
60 |
* call to <code>eval</code>, executing <code>js.drop(varx)</code> will
|
|
61 |
* generate two events: one for changing the status of the
|
|
62 |
* variable snippet to <code>DROPPED</code> and one for
|
|
63 |
* updating the method snippet (which now has an unresolved reference to
|
|
64 |
* <code>x</code>).
|
|
65 |
* <p>
|
|
66 |
* Of course, for any general application of the API, the input would not be
|
|
67 |
* fixed strings, but would come from the user. Below is a very simplified
|
|
68 |
* example of how the API might be used to implement a REPL.
|
|
69 |
* <pre>
|
|
70 |
* {@code
|
|
71 |
* import java.io.ByteArrayInputStream;
|
|
72 |
* import java.io.Console;
|
|
73 |
* import java.util.List;
|
|
74 |
* import jdk.jshell.*;
|
|
75 |
* import jdk.jshell.Snippet.Status;
|
|
76 |
*
|
|
77 |
* class ExampleJShell {
|
|
78 |
* public static void main(String[] args) {
|
|
79 |
* Console console = System.console();
|
|
80 |
* try (JShell js = JShell.create()) {
|
|
81 |
* do {
|
|
82 |
* System.out.print("Enter some Java code: ");
|
|
83 |
* String input = console.readLine();
|
|
84 |
* if (input == null) {
|
|
85 |
* break;
|
|
86 |
* }
|
|
87 |
* List<SnippetEvent> events = js.eval(input);
|
|
88 |
* for (SnippetEvent e : events) {
|
|
89 |
* StringBuilder sb = new StringBuilder();
|
|
90 |
* if (e.causeSnippet == null) {
|
|
91 |
* // We have a snippet creation event
|
|
92 |
* switch (e.status) {
|
|
93 |
* case VALID:
|
|
94 |
* sb.append("Successful ");
|
|
95 |
* break;
|
|
96 |
* case RECOVERABLE_DEFINED:
|
|
97 |
* sb.append("With unresolved references ");
|
|
98 |
* break;
|
|
99 |
* case RECOVERABLE_NOT_DEFINED:
|
|
100 |
* sb.append("Possibly reparable, failed ");
|
|
101 |
* break;
|
|
102 |
* case REJECTED:
|
|
103 |
* sb.append("Failed ");
|
|
104 |
* break;
|
|
105 |
* }
|
|
106 |
* if (e.previousStatus == Status.NONEXISTENT) {
|
|
107 |
* sb.append("addition");
|
|
108 |
* } else {
|
|
109 |
* sb.append("modification");
|
|
110 |
* }
|
|
111 |
* sb.append(" of ");
|
|
112 |
* sb.append(e.snippet.source());
|
|
113 |
* System.out.println(sb);
|
|
114 |
* if (e.value != null) {
|
|
115 |
* System.out.printf("Value is: %s\n", e.value);
|
|
116 |
* }
|
|
117 |
* System.out.flush();
|
|
118 |
* }
|
|
119 |
* }
|
|
120 |
* } while (true);
|
|
121 |
* }
|
|
122 |
* System.out.println("\nGoodbye");
|
|
123 |
* }
|
|
124 |
* }
|
|
125 |
* }
|
|
126 |
* </pre>
|
|
127 |
* <p>
|
|
128 |
* To register for status change events use
|
|
129 |
* {@link jdk.jshell.JShell#onSnippetEvent(java.util.function.Consumer)}.
|
|
130 |
* These events are only generated by <code>eval</code> and <code>drop</code>,
|
|
131 |
* the return values of these methods are the list of events generated by that
|
|
132 |
* call. So, as in the example above, events can be used without registering
|
|
133 |
* to receive events.
|
|
134 |
* <p>
|
|
135 |
* If you experiment with this example, you will see that failing to terminate
|
|
136 |
* a statement or variable declaration with a semi-colon will simply fail.
|
|
137 |
* An unfinished entry (for example a desired multi-line method) will also just
|
|
138 |
* fail after one line. The utilities in {@link jdk.jshell.SourceCodeAnalysis}
|
|
139 |
* provide source boundary and completeness analysis to address cases like
|
|
140 |
* those. <code>SourceCodeAnalysis</code> also provides suggested completions
|
|
141 |
* of input, as might be used in tab-completion.
|
|
142 |
*/
|
|
143 |
|
|
144 |
|
|
145 |
package jdk.jshell;
|
|
146 |
|