changeset 40515:819fc588bd19

8164277: JShell API: Snippets are immutable and should be available for post-mortem analysis Reviewed-by: jlahoda
author rfield
date Mon, 22 Aug 2016 08:18:18 -0700
parents fa42e8040550
children 9e0e107c39dd
files langtools/src/jdk.jshell/share/classes/jdk/jshell/JShell.java langtools/test/jdk/jshell/JShellQueryTest.java langtools/test/jdk/jshell/JShellStateClosedTest.java langtools/test/jdk/jshell/KullaTesting.java
diffstat 4 files changed, 59 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/langtools/src/jdk.jshell/share/classes/jdk/jshell/JShell.java	Mon Aug 22 15:16:30 2016 +0200
+++ b/langtools/src/jdk.jshell/share/classes/jdk/jshell/JShell.java	Mon Aug 22 08:18:18 2016 -0700
@@ -503,10 +503,8 @@
     /**
      * Return all snippets.
      * @return the snippets for all current snippets in id order.
-     * @throws IllegalStateException if this JShell instance is closed.
      */
-    public Stream<Snippet> snippets() throws IllegalStateException {
-        checkIfAlive();
+    public Stream<Snippet> snippets() {
         return maps.snippetList().stream();
     }
 
@@ -517,9 +515,8 @@
      * {@code && snippet.kind() == Kind.VARIABLE}
      * and cast to {@code VarSnippet}.
      * @return the active declared variables.
-     * @throws IllegalStateException if this JShell instance is closed.
      */
-    public Stream<VarSnippet> variables() throws IllegalStateException {
+    public Stream<VarSnippet> variables() {
         return snippets()
                      .filter(sn -> status(sn).isActive() && sn.kind() == Snippet.Kind.VAR)
                      .map(sn -> (VarSnippet) sn);
@@ -532,9 +529,8 @@
      * {@code && snippet.kind() == Kind.METHOD}
      * and cast to MethodSnippet.
      * @return the active declared methods.
-     * @throws IllegalStateException if this JShell instance is closed.
      */
-    public Stream<MethodSnippet> methods() throws IllegalStateException {
+    public Stream<MethodSnippet> methods() {
         return snippets()
                      .filter(sn -> status(sn).isActive() && sn.kind() == Snippet.Kind.METHOD)
                      .map(sn -> (MethodSnippet)sn);
@@ -547,9 +543,8 @@
      * {@code && snippet.kind() == Kind.TYPE_DECL}
      * and cast to TypeDeclSnippet.
      * @return the active declared type declarations.
-     * @throws IllegalStateException if this JShell instance is closed.
      */
-    public Stream<TypeDeclSnippet> types() throws IllegalStateException {
+    public Stream<TypeDeclSnippet> types() {
         return snippets()
                 .filter(sn -> status(sn).isActive() && sn.kind() == Snippet.Kind.TYPE_DECL)
                 .map(sn -> (TypeDeclSnippet) sn);
@@ -562,9 +557,8 @@
      * {@code && snippet.kind() == Kind.IMPORT}
      * and cast to ImportSnippet.
      * @return the active declared import declarations.
-     * @throws IllegalStateException if this JShell instance is closed.
      */
-    public Stream<ImportSnippet> imports() throws IllegalStateException {
+    public Stream<ImportSnippet> imports() {
         return snippets()
                 .filter(sn -> status(sn).isActive() && sn.kind() == Snippet.Kind.IMPORT)
                 .map(sn -> (ImportSnippet) sn);
--- a/langtools/test/jdk/jshell/JShellQueryTest.java	Mon Aug 22 15:16:30 2016 +0200
+++ b/langtools/test/jdk/jshell/JShellQueryTest.java	Mon Aug 22 08:18:18 2016 -0700
@@ -28,8 +28,6 @@
  * @build KullaTesting
  * @run testng JShellQueryTest
  */
-import java.util.Set;
-import java.util.stream.Stream;
 import jdk.jshell.Snippet;
 import org.testng.annotations.Test;
 
@@ -38,46 +36,39 @@
 import jdk.jshell.TypeDeclSnippet;
 import jdk.jshell.VarSnippet;
 import static java.util.stream.Collectors.joining;
-import static java.util.stream.Collectors.toSet;
 import static org.testng.Assert.assertEquals;
 
 @Test
 public class JShellQueryTest extends KullaTesting {
 
-    private <T> void checkStreamMatch(Stream<T> result, T... expected) {
-        Set<T> sns = result.collect(toSet());
-        Set<T> exp = Stream.of(expected).collect(toSet());
-        assertEquals(sns, exp);
-    }
-
     public void testSnippets() {
-        checkStreamMatch(getState().snippets());
+        assertStreamMatch(getState().snippets());
         VarSnippet sx = varKey(assertEval("int x = 5;"));
         VarSnippet sfoo = varKey(assertEval("String foo;"));
         MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));
         MethodSnippet svv = methodKey(assertEval("void vv() { }"));
-        checkStreamMatch(getState().snippets(), sx, sfoo, smm, svv);
+        assertStreamMatch(getState().snippets(), sx, sfoo, smm, svv);
         TypeDeclSnippet sc = classKey(assertEval("class C { }"));
         TypeDeclSnippet si = classKey(assertEval("interface I { }"));
         ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));
-        checkStreamMatch(getState().snippets(), sx, sfoo, smm, svv, sc, si, simp);
+        assertStreamMatch(getState().snippets(), sx, sfoo, smm, svv, sc, si, simp);
     }
 
     public void testVars() {
-        checkStreamMatch(getState().variables());
+        assertStreamMatch(getState().variables());
         VarSnippet sx = varKey(assertEval("int x = 5;"));
         VarSnippet sfoo = varKey(assertEval("String foo;"));
         MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));
         MethodSnippet svv = methodKey(assertEval("void vv() { }"));
-        checkStreamMatch(getState().variables(), sx, sfoo);
+        assertStreamMatch(getState().variables(), sx, sfoo);
         TypeDeclSnippet sc = classKey(assertEval("class C { }"));
         TypeDeclSnippet si = classKey(assertEval("interface I { }"));
         ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));
-        checkStreamMatch(getState().variables(), sx, sfoo);
+        assertStreamMatch(getState().variables(), sx, sfoo);
     }
 
     public void testMethods() {
-        checkStreamMatch(getState().methods());
+        assertStreamMatch(getState().methods());
         VarSnippet sx = varKey(assertEval("int x = 5;"));
         VarSnippet sfoo = varKey(assertEval("String foo;"));
         MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));
@@ -85,11 +76,11 @@
         TypeDeclSnippet sc = classKey(assertEval("class C { }"));
         TypeDeclSnippet si = classKey(assertEval("interface I { }"));
         ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));
-        checkStreamMatch(getState().methods(), smm, svv);
+        assertStreamMatch(getState().methods(), smm, svv);
     }
 
     public void testTypes() {
-        checkStreamMatch(getState().types());
+        assertStreamMatch(getState().types());
         VarSnippet sx = varKey(assertEval("int x = 5;"));
         VarSnippet sfoo = varKey(assertEval("String foo;"));
         MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));
@@ -97,11 +88,11 @@
         TypeDeclSnippet sc = classKey(assertEval("class C { }"));
         TypeDeclSnippet si = classKey(assertEval("interface I { }"));
         ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));
-        checkStreamMatch(getState().types(), sc, si);
+        assertStreamMatch(getState().types(), sc, si);
     }
 
     public void testImports() {
-        checkStreamMatch(getState().imports());
+        assertStreamMatch(getState().imports());
         VarSnippet sx = varKey(assertEval("int x = 5;"));
         VarSnippet sfoo = varKey(assertEval("String foo;"));
         MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));
@@ -109,12 +100,12 @@
         TypeDeclSnippet sc = classKey(assertEval("class C { }"));
         TypeDeclSnippet si = classKey(assertEval("interface I { }"));
         ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));
-        checkStreamMatch(getState().imports(), simp);
+        assertStreamMatch(getState().imports(), simp);
     }
 
     public void testDiagnostics() {
         Snippet sx = varKey(assertEval("int x = 5;"));
-        checkStreamMatch(getState().diagnostics(sx));
+        assertStreamMatch(getState().diagnostics(sx));
         Snippet broken = methodKey(assertEvalFail("int m() { blah(); return \"hello\"; }"));
         String res = getState().diagnostics(broken)
                 .map(d -> d.getCode())
@@ -124,8 +115,8 @@
 
     public void testUnresolvedDependencies() {
         VarSnippet sx = varKey(assertEval("int x = 5;"));
-        checkStreamMatch(getState().unresolvedDependencies(sx));
+        assertStreamMatch(getState().unresolvedDependencies(sx));
         MethodSnippet unr = methodKey(getState().eval("void uu() { baz(); zips(); }"));
-        checkStreamMatch(getState().unresolvedDependencies(unr), "method zips()", "method baz()");
+        assertStreamMatch(getState().unresolvedDependencies(unr), "method zips()", "method baz()");
     }
 }
--- a/langtools/test/jdk/jshell/JShellStateClosedTest.java	Mon Aug 22 15:16:30 2016 +0200
+++ b/langtools/test/jdk/jshell/JShellStateClosedTest.java	Mon Aug 22 08:18:18 2016 -0700
@@ -22,7 +22,7 @@
  */
 
 /*
- * @test
+ * @test 8164277
  * @summary Testing IllegalStateException.
  * @build KullaTesting TestingInputStream JShellStateClosedTest
  * @run testng JShellStateClosedTest
@@ -31,8 +31,11 @@
 import java.util.function.Consumer;
 
 import jdk.jshell.DeclarationSnippet;
+import jdk.jshell.ImportSnippet;
+import jdk.jshell.MethodSnippet;
 import jdk.jshell.PersistentSnippet;
 import jdk.jshell.Snippet;
+import jdk.jshell.TypeDeclSnippet;
 import jdk.jshell.VarSnippet;
 import org.testng.annotations.Test;
 
@@ -52,19 +55,42 @@
     }
 
     public void testClasses() {
-        testStateClosedException(() -> getState().types());
+        TypeDeclSnippet sc = classKey(assertEval("class C { }"));
+        TypeDeclSnippet si = classKey(assertEval("interface I { }"));
+        getState().close();
+        assertStreamMatch(getState().types(), sc, si);
     }
 
     public void testVariables() {
-        testStateClosedException(() -> getState().variables());
+        VarSnippet sx = varKey(assertEval("int x = 5;"));
+        VarSnippet sfoo = varKey(assertEval("String foo;"));
+        getState().close();
+        assertStreamMatch(getState().variables(), sx, sfoo);
     }
 
     public void testMethods() {
-        testStateClosedException(() -> getState().methods());
+        MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));
+        MethodSnippet svv = methodKey(assertEval("void vv() { }"));
+        getState().close();
+        assertStreamMatch(getState().methods(), smm, svv);
     }
 
-    public void testKeys() {
-        testStateClosedException(() -> getState().snippets());
+    public void testImports() {
+        ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));
+        getState().close();
+        assertStreamMatch(getState().imports(), simp);
+    }
+
+    public void testSnippets() {
+        VarSnippet sx = varKey(assertEval("int x = 5;"));
+        VarSnippet sfoo = varKey(assertEval("String foo;"));
+        MethodSnippet smm = methodKey(assertEval("int mm() { return 6; }"));
+        MethodSnippet svv = methodKey(assertEval("void vv() { }"));
+        TypeDeclSnippet sc = classKey(assertEval("class C { }"));
+        TypeDeclSnippet si = classKey(assertEval("interface I { }"));
+        ImportSnippet simp = importKey(assertEval("import java.lang.reflect.*;"));
+        getState().close();
+        assertStreamMatch(getState().snippets(), sx, sfoo, smm, svv, sc, si, simp);
     }
 
     public void testEval() {
--- a/langtools/test/jdk/jshell/KullaTesting.java	Mon Aug 22 15:16:30 2016 +0200
+++ b/langtools/test/jdk/jshell/KullaTesting.java	Mon Aug 22 08:18:18 2016 -0700
@@ -71,6 +71,7 @@
 
 import jdk.jshell.Diag;
 import static java.util.stream.Collectors.toList;
+import static java.util.stream.Collectors.toSet;
 import static jdk.jshell.Snippet.Status.*;
 import static org.testng.Assert.*;
 import static jdk.jshell.Snippet.SubKind.METHOD_SUBKIND;
@@ -358,6 +359,12 @@
         return checkEvents(() -> getState().eval(input), "eval(" + input + ")", diagMain, diagUpdates, eventChains);
     }
 
+    <T> void assertStreamMatch(Stream<T> result, T... expected) {
+        Set<T> sns = result.collect(toSet());
+        Set<T> exp = Stream.of(expected).collect(toSet());
+        assertEquals(sns, exp);
+    }
+
     private Map<Snippet, Snippet> closure(List<SnippetEvent> events) {
         Map<Snippet, Snippet> transitions = new HashMap<>();
         for (SnippetEvent event : events) {