|
1 /* |
|
2 * Copyright (c) 2012, 2013, 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 package com.sun.tools.jdeps; |
|
26 |
|
27 import java.io.IOException; |
|
28 import java.nio.file.FileVisitResult; |
|
29 import java.nio.file.Files; |
|
30 import java.nio.file.Path; |
|
31 import java.nio.file.Paths; |
|
32 import java.nio.file.SimpleFileVisitor; |
|
33 import java.nio.file.attribute.BasicFileAttributes; |
|
34 import java.util.*; |
|
35 |
|
36 /** |
|
37 * ClassPath for Java SE and JDK |
|
38 */ |
|
39 class PlatformClassPath { |
|
40 private final static List<Archive> javaHomeArchives = init(); |
|
41 |
|
42 static List<Archive> getArchives() { |
|
43 return javaHomeArchives; |
|
44 } |
|
45 |
|
46 private static List<Archive> init() { |
|
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 } |
|
69 } |
|
70 |
|
71 private static List<Archive> addJarFiles(final Path root) throws IOException { |
|
72 final List<Archive> result = new ArrayList<>(); |
|
73 final Path ext = root.resolve("ext"); |
|
74 Files.walkFileTree(root, new SimpleFileVisitor<Path>() { |
|
75 @Override |
|
76 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) |
|
77 throws IOException |
|
78 { |
|
79 if (dir.equals(root) || dir.equals(ext)) { |
|
80 return FileVisitResult.CONTINUE; |
|
81 } else { |
|
82 // skip other cobundled JAR files |
|
83 return FileVisitResult.SKIP_SUBTREE; |
|
84 } |
|
85 } |
|
86 @Override |
|
87 public FileVisitResult visitFile(Path p, BasicFileAttributes attrs) |
|
88 throws IOException |
|
89 { |
|
90 String fn = p.getFileName().toString(); |
|
91 if (fn.endsWith(".jar")) { |
|
92 // JDK may cobundle with JavaFX that doesn't belong to any profile |
|
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))); |
|
97 } |
|
98 return FileVisitResult.CONTINUE; |
|
99 } |
|
100 }); |
|
101 return result; |
|
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 } |
|
113 } |