8172154: jshell tool: make all IMPORTS.jsh generated at build time
authorrfield
Tue, 14 Nov 2017 10:45:22 -0800
changeset 47837 ef7aad81c3d6
parent 47836 f17a797c910a
child 47838 f0c8554f0bce
8172154: jshell tool: make all IMPORTS.jsh generated at build time Reviewed-by: jlahoda
src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
src/jdk.jshell/share/classes/jdk/jshell/tool/resources/JAVASE.jsh
test/langtools/jdk/jshell/ToolSimpleTest.java
--- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Tue Nov 14 12:38:39 2017 +0530
+++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Tue Nov 14 10:45:22 2017 -0800
@@ -36,6 +36,9 @@
 import java.io.PrintStream;
 import java.io.Reader;
 import java.io.StringReader;
+import java.lang.module.ModuleDescriptor;
+import java.lang.module.ModuleFinder;
+import java.lang.module.ModuleReference;
 import java.nio.charset.Charset;
 import java.nio.file.FileSystems;
 import java.nio.file.Files;
@@ -55,6 +58,7 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.Scanner;
 import java.util.Set;
 import java.util.function.Consumer;
@@ -2771,8 +2775,56 @@
         return null;
     }
 
-    // Read a built-in file from resources
-    static String readResource(String name) throws IOException {
+    // Read a built-in file from resources or compute it
+    static String readResource(String name) throws Exception {
+        // Class to compute imports by following requires for a module
+        class ComputeImports {
+            final String base;
+            ModuleFinder finder = ModuleFinder.ofSystem();
+
+            ComputeImports(String base) {
+                this.base = base;
+            }
+
+            Set<ModuleDescriptor> modules() {
+                Set<ModuleDescriptor> closure = new HashSet<>();
+                moduleClosure(finder.find(base), closure);
+                return closure;
+            }
+
+            void moduleClosure(Optional<ModuleReference> omr, Set<ModuleDescriptor> closure) {
+                if (omr.isPresent()) {
+                    ModuleDescriptor mdesc = omr.get().descriptor();
+                    if (closure.add(mdesc)) {
+                        for (ModuleDescriptor.Requires req : mdesc.requires()) {
+                            if (!req.modifiers().contains(ModuleDescriptor.Requires.Modifier.STATIC)) {
+                                moduleClosure(finder.find(req.name()), closure);
+                            }
+                        }
+                    }
+                }
+            }
+
+            Set<String> packages() {
+                return modules().stream().flatMap(md -> md.exports().stream())
+                        .filter(e -> !e.isQualified()).map(Object::toString).collect(Collectors.toSet());
+            }
+
+            String imports() {
+                Set<String> si = packages();
+                String[] ai = si.toArray(new String[si.size()]);
+                Arrays.sort(ai);
+                return Arrays.stream(ai)
+                        .map(p -> String.format("import %s.*;\n", p))
+                        .collect(Collectors.joining());
+            }
+        }
+
+        if (name.equals("JAVASE")) {
+            // The built-in JAVASE is computed as the imports of all the packages in Java SE
+            return new ComputeImports("java.se").imports();
+        }
+
         // Attempt to find the file as a resource
         String spec = String.format(BUILTIN_FILE_PATH_FORMAT, name);
 
--- a/src/jdk.jshell/share/classes/jdk/jshell/tool/resources/JAVASE.jsh	Tue Nov 14 12:38:39 2017 +0530
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,173 +0,0 @@
-import java.applet.*;
-import java.awt.*;
-import java.awt.color.*;
-import java.awt.datatransfer.*;
-import java.awt.desktop.*;
-import java.awt.dnd.*;
-import java.awt.event.*;
-import java.awt.font.*;
-import java.awt.geom.*;
-import java.awt.im.*;
-import java.awt.im.spi.*;
-import java.awt.image.*;
-import java.awt.image.renderable.*;
-import java.awt.print.*;
-import java.beans.*;
-import java.beans.beancontext.*;
-import java.io.*;
-import java.lang.*;
-import java.lang.annotation.*;
-import java.lang.instrument.*;
-import java.lang.invoke.*;
-import java.lang.management.*;
-import java.lang.module.*;
-import java.lang.ref.*;
-import java.lang.reflect.*;
-import java.math.*;
-import java.net.*;
-import java.net.spi.*;
-import java.nio.*;
-import java.nio.channels.*;
-import java.nio.channels.spi.*;
-import java.nio.charset.*;
-import java.nio.charset.spi.*;
-import java.nio.file.*;
-import java.nio.file.attribute.*;
-import java.nio.file.spi.*;
-import java.rmi.*;
-import java.rmi.activation.*;
-import java.rmi.dgc.*;
-import java.rmi.registry.*;
-import java.rmi.server.*;
-import java.security.*;
-import java.security.acl.*;
-import java.security.cert.*;
-import java.security.interfaces.*;
-import java.security.spec.*;
-import java.sql.*;
-import java.text.*;
-import java.text.spi.*;
-import java.time.*;
-import java.time.chrono.*;
-import java.time.format.*;
-import java.time.temporal.*;
-import java.time.zone.*;
-import java.util.*;
-import java.util.concurrent.*;
-import java.util.concurrent.atomic.*;
-import java.util.concurrent.locks.*;
-import java.util.function.*;
-import java.util.jar.*;
-import java.util.logging.*;
-import java.util.prefs.*;
-import java.util.regex.*;
-import java.util.spi.*;
-import java.util.stream.*;
-import java.util.zip.*;
-import javax.accessibility.*;
-import javax.annotation.processing.*;
-import javax.crypto.*;
-import javax.crypto.interfaces.*;
-import javax.crypto.spec.*;
-import javax.imageio.*;
-import javax.imageio.event.*;
-import javax.imageio.metadata.*;
-import javax.imageio.plugins.bmp.*;
-import javax.imageio.plugins.jpeg.*;
-import javax.imageio.plugins.tiff.*;
-import javax.imageio.spi.*;
-import javax.imageio.stream.*;
-import javax.lang.model.*;
-import javax.lang.model.element.*;
-import javax.lang.model.type.*;
-import javax.lang.model.util.*;
-import javax.management.*;
-import javax.management.loading.*;
-import javax.management.modelmbean.*;
-import javax.management.monitor.*;
-import javax.management.openmbean.*;
-import javax.management.relation.*;
-import javax.management.remote.*;
-import javax.management.remote.rmi.*;
-import javax.management.timer.*;
-import javax.naming.*;
-import javax.naming.directory.*;
-import javax.naming.event.*;
-import javax.naming.ldap.*;
-import javax.naming.spi.*;
-import javax.net.*;
-import javax.net.ssl.*;
-import javax.print.*;
-import javax.print.attribute.*;
-import javax.print.attribute.standard.*;
-import javax.print.event.*;
-import javax.rmi.ssl.*;
-import javax.script.*;
-import javax.security.auth.*;
-import javax.security.auth.callback.*;
-import javax.security.auth.kerberos.*;
-import javax.security.auth.login.*;
-import javax.security.auth.spi.*;
-import javax.security.auth.x500.*;
-import javax.security.cert.*;
-import javax.security.sasl.*;
-import javax.sound.midi.*;
-import javax.sound.midi.spi.*;
-import javax.sound.sampled.*;
-import javax.sound.sampled.spi.*;
-import javax.sql.*;
-import javax.sql.rowset.*;
-import javax.sql.rowset.serial.*;
-import javax.sql.rowset.spi.*;
-import javax.swing.*;
-import javax.swing.border.*;
-import javax.swing.colorchooser.*;
-import javax.swing.event.*;
-import javax.swing.filechooser.*;
-import javax.swing.plaf.*;
-import javax.swing.plaf.basic.*;
-import javax.swing.plaf.metal.*;
-import javax.swing.plaf.multi.*;
-import javax.swing.plaf.nimbus.*;
-import javax.swing.plaf.synth.*;
-import javax.swing.table.*;
-import javax.swing.text.*;
-import javax.swing.text.html.*;
-import javax.swing.text.html.parser.*;
-import javax.swing.text.rtf.*;
-import javax.swing.tree.*;
-import javax.swing.undo.*;
-import javax.tools.*;
-import javax.transaction.xa.*;
-import javax.xml.*;
-import javax.xml.catalog.*;
-import javax.xml.crypto.*;
-import javax.xml.crypto.dom.*;
-import javax.xml.crypto.dsig.*;
-import javax.xml.crypto.dsig.dom.*;
-import javax.xml.crypto.dsig.keyinfo.*;
-import javax.xml.crypto.dsig.spec.*;
-import javax.xml.datatype.*;
-import javax.xml.namespace.*;
-import javax.xml.parsers.*;
-import javax.xml.stream.*;
-import javax.xml.stream.events.*;
-import javax.xml.stream.util.*;
-import javax.xml.transform.*;
-import javax.xml.transform.dom.*;
-import javax.xml.transform.sax.*;
-import javax.xml.transform.stax.*;
-import javax.xml.transform.stream.*;
-import javax.xml.validation.*;
-import javax.xml.xpath.*;
-import org.ietf.jgss.*;
-import org.w3c.dom.*;
-import org.w3c.dom.bootstrap.*;
-import org.w3c.dom.events.*;
-import org.w3c.dom.ls.*;
-import org.w3c.dom.ranges.*;
-import org.w3c.dom.traversal.*;
-import org.w3c.dom.views.*;
-import org.xml.sax.*;
-import org.xml.sax.ext.*;
-import org.xml.sax.helpers.*;
--- a/test/langtools/jdk/jshell/ToolSimpleTest.java	Tue Nov 14 12:38:39 2017 +0530
+++ b/test/langtools/jdk/jshell/ToolSimpleTest.java	Tue Nov 14 10:45:22 2017 -0800
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897 8167128 8154513 8170015 8170368 8172102 8172103  8165405 8173073 8173848 8174041 8173916 8174028 8174262 8174797 8177079 8180508 8177466
+ * @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897 8167128 8154513 8170015 8170368 8172102 8172103  8165405 8173073 8173848 8174041 8173916 8174028 8174262 8174797 8177079 8180508 8177466 8172154
  * @summary Simple jshell tool tests
  * @modules jdk.compiler/com.sun.tools.javac.api
  *          jdk.compiler/com.sun.tools.javac.main
@@ -633,6 +633,17 @@
     }
 
     @Test
+    public void testJavaSeSetStart() {
+        test(
+                (a) -> assertCommand(a, "/set sta JAVASE", ""),
+                (a) -> assertCommand(a, "/reset", "|  Resetting state."),
+                (a) -> assertCommandCheckOutput(a, "/li -a",
+                            s -> assertTrue(s.split("import ").length > 160,
+                            "not enough imports for JAVASE:\n" + s))
+        );
+    }
+
+    @Test
     public void defineClasses() {
         test(
                 (a) -> assertCommandCheckOutput(a, "/list", assertList()),