Thu, 14 Mar 2013 10:33:31 -0700
8005428: Update jdeps to read the same profile information as by javac
Reviewed-by: alanb
1 /*
2 * Copyright (c) 2012, 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;
27 import java.io.File;
28 import java.io.IOException;
29 import java.nio.file.FileVisitResult;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.SimpleFileVisitor;
33 import java.nio.file.attribute.BasicFileAttributes;
34 import java.util.*;
36 /**
37 * ClassPath for Java SE and JDK
38 */
39 class PlatformClassPath {
40 private final static List<Archive> javaHomeArchives = init();
41 static List<Archive> getArchives() {
42 return javaHomeArchives;
43 }
45 static boolean contains(Archive archive) {
46 return javaHomeArchives.contains(archive);
47 }
49 private static List<Archive> init() {
50 List<Archive> result = new ArrayList<Archive>();
51 String javaHome = System.getProperty("java.home");
52 List<File> files = new ArrayList<File>();
53 File jre = new File(javaHome, "jre");
54 File lib = new File(javaHome, "lib");
56 try {
57 if (jre.exists() && jre.isDirectory()) {
58 result.addAll(addJarFiles(new File(jre, "lib")));
59 result.addAll(addJarFiles(lib));
60 } else if (lib.exists() && lib.isDirectory()) {
61 // either a JRE or a jdk build image
62 File classes = new File(javaHome, "classes");
63 if (classes.exists() && classes.isDirectory()) {
64 // jdk build outputdir
65 result.add(new Archive(classes, ClassFileReader.newInstance(classes)));
66 }
67 // add other JAR files
68 result.addAll(addJarFiles(lib));
69 } else {
70 throw new RuntimeException("\"" + javaHome + "\" not a JDK home");
71 }
72 } catch (IOException e) {
73 throw new RuntimeException(e);
74 }
75 return result;
76 }
78 private static List<Archive> addJarFiles(File f) throws IOException {
79 final List<Archive> result = new ArrayList<Archive>();
80 final Path root = f.toPath();
81 final Path ext = root.resolve("ext");
82 Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
83 @Override
84 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
85 throws IOException
86 {
87 if (dir.equals(root) || dir.equals(ext)) {
88 return FileVisitResult.CONTINUE;
89 } else {
90 // skip other cobundled JAR files
91 return FileVisitResult.SKIP_SUBTREE;
92 }
93 }
94 @Override
95 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
96 throws IOException
97 {
98 File f = file.toFile();
99 String fn = f.getName();
100 if (fn.endsWith(".jar") && !fn.equals("alt-rt.jar")) {
101 result.add(new Archive(f, ClassFileReader.newInstance(f)));
102 }
103 return FileVisitResult.CONTINUE;
104 }
105 });
106 return result;
107 }
108 }