langtools/src/share/classes/com/sun/tools/jdeps/PlatformClassPath.java
changeset 21046 ebf16a1a6328
parent 16560 c6c7f0c26568
child 25442 755ff386d1ac
equal deleted inserted replaced
21045:a7a1562c97be 21046:ebf16a1a6328
    22  * or visit www.oracle.com if you need additional information or have any
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 package com.sun.tools.jdeps;
    25 package com.sun.tools.jdeps;
    26 
    26 
    27 import java.io.File;
       
    28 import java.io.IOException;
    27 import java.io.IOException;
    29 import java.nio.file.FileVisitResult;
    28 import java.nio.file.FileVisitResult;
    30 import java.nio.file.Files;
    29 import java.nio.file.Files;
    31 import java.nio.file.Path;
    30 import java.nio.file.Path;
       
    31 import java.nio.file.Paths;
    32 import java.nio.file.SimpleFileVisitor;
    32 import java.nio.file.SimpleFileVisitor;
    33 import java.nio.file.attribute.BasicFileAttributes;
    33 import java.nio.file.attribute.BasicFileAttributes;
    34 import java.util.*;
    34 import java.util.*;
    35 
    35 
    36 /**
    36 /**
    37  * ClassPath for Java SE and JDK
    37  * ClassPath for Java SE and JDK
    38  */
    38  */
    39 class PlatformClassPath {
    39 class PlatformClassPath {
    40     private final static List<Archive> javaHomeArchives = init();
    40     private final static List<Archive> javaHomeArchives = init();
       
    41 
    41     static List<Archive> getArchives() {
    42     static List<Archive> getArchives() {
    42         return javaHomeArchives;
    43         return javaHomeArchives;
    43     }
    44     }
    44 
    45 
    45     static boolean contains(Archive archive) {
    46     private static List<Archive> init() {
    46         return javaHomeArchives.contains(archive);
    47         List<Archive> result = new ArrayList<>();
       
    48         Path home = Paths.get(System.getProperty("java.home"));
       
    49         try {
       
    50             if (home.endsWith("jre")) {
       
    51                 // jar files in <javahome>/jre/lib
       
    52                 result.addAll(addJarFiles(home.resolve("lib")));
       
    53             } else if (Files.exists(home.resolve("lib"))) {
       
    54                 // either a JRE or a jdk build image
       
    55                 Path classes = home.resolve("classes");
       
    56                 if (Files.isDirectory(classes)) {
       
    57                     // jdk build outputdir
       
    58                     result.add(new JDKArchive(classes, ClassFileReader.newInstance(classes)));
       
    59                 }
       
    60                 // add other JAR files
       
    61                 result.addAll(addJarFiles(home.resolve("lib")));
       
    62             } else {
       
    63                 throw new RuntimeException("\"" + home + "\" not a JDK home");
       
    64             }
       
    65             return result;
       
    66         } catch (IOException e) {
       
    67             throw new Error(e);
       
    68         }
    47     }
    69     }
    48 
    70 
    49     private static List<Archive> init() {
    71     private static List<Archive> addJarFiles(final Path root) throws IOException {
    50         List<Archive> result = new ArrayList<Archive>();
    72         final List<Archive> result = new ArrayList<>();
    51         String javaHome = System.getProperty("java.home");
       
    52         File jre = new File(javaHome, "jre");
       
    53         File lib = new File(javaHome, "lib");
       
    54 
       
    55         try {
       
    56             if (jre.exists() && jre.isDirectory()) {
       
    57                 result.addAll(addJarFiles(new File(jre, "lib")));
       
    58                 result.addAll(addJarFiles(lib));
       
    59             } else if (lib.exists() && lib.isDirectory()) {
       
    60                 // either a JRE or a jdk build image
       
    61                 File classes = new File(javaHome, "classes");
       
    62                 if (classes.exists() && classes.isDirectory()) {
       
    63                     // jdk build outputdir
       
    64                     result.add(new Archive(classes, ClassFileReader.newInstance(classes)));
       
    65                 }
       
    66                 // add other JAR files
       
    67                 result.addAll(addJarFiles(lib));
       
    68             } else {
       
    69                 throw new RuntimeException("\"" + javaHome + "\" not a JDK home");
       
    70             }
       
    71         } catch (IOException e) {
       
    72             throw new RuntimeException(e);
       
    73         }
       
    74         return result;
       
    75     }
       
    76 
       
    77     private static List<Archive> addJarFiles(File f) throws IOException {
       
    78         final List<Archive> result = new ArrayList<Archive>();
       
    79         final Path root = f.toPath();
       
    80         final Path ext = root.resolve("ext");
    73         final Path ext = root.resolve("ext");
    81         Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
    74         Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
    82             @Override
    75             @Override
    83             public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
    76             public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
    84                 throws IOException
    77                 throws IOException
    89                     // skip other cobundled JAR files
    82                     // skip other cobundled JAR files
    90                     return FileVisitResult.SKIP_SUBTREE;
    83                     return FileVisitResult.SKIP_SUBTREE;
    91                 }
    84                 }
    92             }
    85             }
    93             @Override
    86             @Override
    94             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
    87             public FileVisitResult visitFile(Path p, BasicFileAttributes attrs)
    95                 throws IOException
    88                 throws IOException
    96             {
    89             {
    97                 File f = file.toFile();
    90                 String fn = p.getFileName().toString();
    98                 String fn = f.getName();
    91                 if (fn.endsWith(".jar")) {
    99                 if (fn.endsWith(".jar") && !fn.equals("alt-rt.jar")) {
    92                     // JDK may cobundle with JavaFX that doesn't belong to any profile
   100                     result.add(new Archive(f, ClassFileReader.newInstance(f)));
    93                     // Treat jfxrt.jar as regular Archive
       
    94                     result.add(fn.equals("jfxrt.jar")
       
    95                         ? new Archive(p, ClassFileReader.newInstance(p))
       
    96                         : new JDKArchive(p, ClassFileReader.newInstance(p)));
   101                 }
    97                 }
   102                 return FileVisitResult.CONTINUE;
    98                 return FileVisitResult.CONTINUE;
   103             }
    99             }
   104         });
   100         });
   105         return result;
   101         return result;
   106     }
   102     }
       
   103 
       
   104     /**
       
   105      * A JDK archive is part of the JDK containing the Java SE API
       
   106      * or implementation classes (i.e. JDK internal API)
       
   107      */
       
   108     static class JDKArchive extends Archive {
       
   109         JDKArchive(Path p, ClassFileReader reader) {
       
   110             super(p, reader);
       
   111         }
       
   112     }
   107 }
   113 }