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 |
package jdk.jshell;
|
|
27 |
|
|
28 |
import jdk.jshell.Snippet.Status;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* A description of a change to a Snippet. These are generated by direct changes
|
|
32 |
* to state with {@link JShell#eval(java.lang.String) JShell.eval(String)} or
|
|
33 |
* {@link JShell#drop(jdk.jshell.PersistentSnippet) JShell.drop(PersistentSnippet)},
|
|
34 |
* or indirectly by these same methods as
|
|
35 |
* dependencies change or Snippets are overwritten. For direct changes, the
|
|
36 |
* {@link SnippetEvent#causeSnippet()} is <code>null</code>.
|
|
37 |
* <p>
|
|
38 |
* <code>SnippetEvent</code> is immutable: an access to
|
|
39 |
* any of its methods will always return the same result.
|
|
40 |
* and thus is thread-safe.
|
|
41 |
* @author Robert Field
|
|
42 |
*/
|
|
43 |
public class SnippetEvent {
|
|
44 |
|
|
45 |
SnippetEvent(Snippet snippet, Status previousStatus, Status status,
|
|
46 |
boolean isSignatureChange, Snippet causeSnippet,
|
|
47 |
String value, Exception exception) {
|
|
48 |
this.snippet = snippet;
|
|
49 |
this.previousStatus = previousStatus;
|
|
50 |
this.status = status;
|
|
51 |
this.isSignatureChange = isSignatureChange;
|
|
52 |
this.causeSnippet = causeSnippet;
|
|
53 |
this.value = value;
|
|
54 |
this.exception = exception;
|
|
55 |
}
|
|
56 |
|
|
57 |
private final Snippet snippet;
|
|
58 |
private final Status previousStatus;
|
|
59 |
private final Status status;
|
|
60 |
private final boolean isSignatureChange;
|
|
61 |
private final Snippet causeSnippet;
|
|
62 |
private final String value;
|
|
63 |
private final Exception exception;
|
|
64 |
|
|
65 |
/**
|
|
66 |
* The Snippet which has changed
|
|
67 |
* @return the return the Snippet whose <code>Status</code> has changed.
|
|
68 |
*/
|
|
69 |
public Snippet snippet() {
|
|
70 |
return snippet;
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* The status before the transition. If this event describes a Snippet
|
|
75 |
* creation return {@link Snippet.Status#NONEXISTENT NONEXISTENT}.
|
|
76 |
* @return the previousStatus
|
|
77 |
*/
|
|
78 |
public Status previousStatus() {
|
|
79 |
return previousStatus;
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* The after status. Note: this may be the same as the previous status (not
|
|
84 |
* all changes cause a <code>Status</code> change.
|
|
85 |
* @return the status
|
|
86 |
*/
|
|
87 |
public Status status() {
|
|
88 |
return status;
|
|
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Has the signature changed? Coming in or out of definition
|
|
93 |
* (status.isDefined) is always a signature change. An overwritten Snippet
|
|
94 |
* {@link jdk.jshell.Snippet.Status#OVERWRITTEN (status == OVERWRITTEN)}
|
|
95 |
* is always <code>false</code> as responsibility for the
|
|
96 |
* definition has passed to the overwriting definition.
|
|
97 |
* @return <code>true</code> if the signature changed.
|
|
98 |
*/
|
|
99 |
public boolean isSignatureChange() {
|
|
100 |
return isSignatureChange;
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Either the snippet whose change caused this update or
|
|
105 |
* <code>null</code>. This returns <code>null</code> if this change is the
|
|
106 |
* creation of a new Snippet via
|
|
107 |
* {@link jdk.jshell.JShell#eval(java.lang.String) eval} or it is the
|
|
108 |
* explicit drop of a Snippet with
|
|
109 |
* {@link jdk.jshell.JShell#drop(jdk.jshell.PersistentSnippet) drop}.
|
|
110 |
*
|
|
111 |
* @return the Snippet which caused this change or <code>null</code> if
|
|
112 |
* directly caused by an API action.
|
|
113 |
*/
|
|
114 |
public Snippet causeSnippet() {
|
|
115 |
return causeSnippet;
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* An instance of {@link jdk.jshell.UnresolvedReferenceException}, if an unresolved reference was
|
|
120 |
* encountered, or an instance of {@link jdk.jshell.EvalException} if an exception was thrown
|
|
121 |
* during execution, otherwise <code>null</code>.
|
|
122 |
* @return the exception or <code>null</code>.
|
|
123 |
*/
|
|
124 |
public Exception exception() {
|
|
125 |
return exception;
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* The result value of successful run. The value is null if not executed
|
|
130 |
* or an exception was thrown.
|
|
131 |
* @return the value or <code>null</code>.
|
|
132 |
*/
|
|
133 |
public String value() {
|
|
134 |
return value;
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Return a string representation of the event
|
|
139 |
* @return a descriptive representation of the SnippetEvent
|
|
140 |
*/
|
|
141 |
@Override
|
|
142 |
public String toString() {
|
|
143 |
return "SnippetEvent(snippet=" + snippet +
|
|
144 |
",previousStatus=" + previousStatus +
|
|
145 |
",status=" + status +
|
|
146 |
",isSignatureChange=" + isSignatureChange +
|
|
147 |
",causeSnippet" + causeSnippet +
|
|
148 |
(value == null? "" : "value=" + value) +
|
|
149 |
(exception == null? "" : "exception=" + exception) +
|
|
150 |
")";
|
|
151 |
}
|
|
152 |
}
|