< prev index next >

src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/PackagesHelper.java

Print this page

        

@@ -25,10 +25,16 @@
 
 package jdk.nashorn.tools.jjs;
 
 import java.io.IOException;
 import java.io.File;
+import java.net.URI;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.LinkedHashMap;

@@ -56,38 +62,46 @@
         // Use javac only if security manager is not around!
         compiler = System.getSecurityManager() == null? ToolProvider.getSystemJavaCompiler() : null;
     }
 
     /**
-     * Is Java package properties helper available?
+     * Is javac available?
      *
-     * @return true if package properties support is available
+     * @return true if javac is available
      */
-    static boolean isAvailable() {
+    private static boolean isJavacAvailable() {
         return compiler != null;
     }
 
     private final StandardJavaFileManager fm;
     private final Set<JavaFileObject.Kind> fileKinds;
+    private final FileSystem jrtfs;
 
     /**
      * Construct a new PackagesHelper.
      *
      * @param classPath Class path to compute properties of java package objects
      */
     PackagesHelper(final String classPath) throws IOException {
-        assert isAvailable() : "no java compiler found!";
-
+        if (isJavacAvailable()) {
         fm = compiler.getStandardFileManager(null, null, null);
         fileKinds = EnumSet.of(JavaFileObject.Kind.CLASS);
 
         if (classPath != null && !classPath.isEmpty()) {
             fm.setLocation(StandardLocation.CLASS_PATH, getFiles(classPath));
         } else {
             // no classpath set. Make sure that it is empty and not any default like "."
             fm.setLocation(StandardLocation.CLASS_PATH, Collections.<File>emptyList());
         }
+            jrtfs = null;
+        } else {
+            // javac is not available - directly use jrt fs
+            // to support atleast platform classes.
+            fm = null;
+            fileKinds = null;
+            jrtfs = FileSystems.getFileSystem(URI.create("jrt:/"));
+        }
     }
 
     // LRU cache for java package properties lists
     private final LinkedHashMap<String, List<String>> propsCache =
         new LinkedHashMap<String, List<String>>(32, 0.75f, true) {

@@ -125,17 +139,45 @@
             return Collections.<String>emptyList();
         }
     }
 
     public void close() throws IOException {
+        if (fm != null) {
         fm.close();
     }
+    }
 
     private Set<String> listPackage(final String pkg) throws IOException {
         final Set<String> props = new HashSet<>();
+        if (fm != null) {
         listPackage(StandardLocation.PLATFORM_CLASS_PATH, pkg, props);
         listPackage(StandardLocation.CLASS_PATH, pkg, props);
+        } else if (jrtfs != null) {
+            // look for the /packages/<package_name> directory
+            Path pkgDir = jrtfs.getPath("/packages/" + pkg);
+            if (Files.exists(pkgDir)) {
+                String pkgSlashName = pkg.replace('.', '/');
+                try (DirectoryStream<Path> ds = Files.newDirectoryStream(pkgDir)) {
+                    // it has module links under which this package occurs
+                    for (Path mod : ds) {
+                        // get the package directory under /modules
+                        Path pkgUnderMod = jrtfs.getPath(mod.toString() + "/" + pkgSlashName);
+                        try (DirectoryStream<Path> ds2 = Files.newDirectoryStream(pkgUnderMod)) {
+                            for (Path p : ds2) {
+                                String str = p.getFileName().toString();
+                                // get rid of ".class", if any
+                                if (str.endsWith(".class")) {
+                                    props.add(str.substring(0, str.length() - ".class".length()));
+                                } else {
+                                    props.add(str);
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
         return props;
     }
 
     private void listPackage(final Location loc, final String pkg, final Set<String> props)
             throws IOException {
< prev index next >