mchung@1472: /* jjg@1648: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. mchung@1472: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mchung@1472: * mchung@1472: * This code is free software; you can redistribute it and/or modify it mchung@1472: * under the terms of the GNU General Public License version 2 only, as mchung@1472: * published by the Free Software Foundation. Oracle designates this mchung@1472: * particular file as subject to the "Classpath" exception as provided mchung@1472: * by Oracle in the LICENSE file that accompanied this code. mchung@1472: * mchung@1472: * This code is distributed in the hope that it will be useful, but WITHOUT mchung@1472: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mchung@1472: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mchung@1472: * version 2 for more details (a copy is included in the LICENSE file that mchung@1472: * accompanied this code). mchung@1472: * mchung@1472: * You should have received a copy of the GNU General Public License version mchung@1472: * 2 along with this work; if not, write to the Free Software Foundation, mchung@1472: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mchung@1472: * mchung@1472: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mchung@1472: * or visit www.oracle.com if you need additional information or have any mchung@1472: * questions. mchung@1472: */ mchung@1472: package com.sun.tools.jdeps; mchung@1472: mchung@1472: import java.io.IOException; mchung@1472: import java.nio.file.FileVisitResult; mchung@1472: import java.nio.file.Files; mchung@1472: import java.nio.file.Path; mchung@2139: import java.nio.file.Paths; mchung@1472: import java.nio.file.SimpleFileVisitor; mchung@1472: import java.nio.file.attribute.BasicFileAttributes; mchung@1472: import java.util.*; mchung@1472: mchung@1472: /** mchung@1472: * ClassPath for Java SE and JDK mchung@1472: */ mchung@1472: class PlatformClassPath { mchung@1472: private final static List javaHomeArchives = init(); mchung@2139: mchung@1472: static List getArchives() { mchung@1472: return javaHomeArchives; mchung@1472: } mchung@1472: mchung@2139: private static List init() { mchung@2139: List result = new ArrayList<>(); mchung@2139: Path home = Paths.get(System.getProperty("java.home")); mchung@2139: try { mchung@2139: if (home.endsWith("jre")) { mchung@2139: // jar files in /jre/lib mchung@2139: result.addAll(addJarFiles(home.resolve("lib"))); mchung@2139: } else if (Files.exists(home.resolve("lib"))) { mchung@2139: // either a JRE or a jdk build image mchung@2139: Path classes = home.resolve("classes"); mchung@2139: if (Files.isDirectory(classes)) { mchung@2139: // jdk build outputdir mchung@2139: result.add(new JDKArchive(classes, ClassFileReader.newInstance(classes))); mchung@2139: } mchung@2139: // add other JAR files mchung@2139: result.addAll(addJarFiles(home.resolve("lib"))); mchung@2139: } else { mchung@2139: throw new RuntimeException("\"" + home + "\" not a JDK home"); mchung@2139: } mchung@2139: return result; mchung@2139: } catch (IOException e) { mchung@2139: throw new Error(e); mchung@2139: } mchung@1472: } mchung@1472: mchung@2139: private static List addJarFiles(final Path root) throws IOException { mchung@2139: final List result = new ArrayList<>(); mchung@1472: final Path ext = root.resolve("ext"); mchung@1472: Files.walkFileTree(root, new SimpleFileVisitor() { mchung@1472: @Override mchung@1472: public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) mchung@1472: throws IOException mchung@1472: { mchung@1472: if (dir.equals(root) || dir.equals(ext)) { mchung@1472: return FileVisitResult.CONTINUE; mchung@1472: } else { mchung@1472: // skip other cobundled JAR files mchung@1472: return FileVisitResult.SKIP_SUBTREE; mchung@1472: } mchung@1472: } mchung@1472: @Override mchung@2139: public FileVisitResult visitFile(Path p, BasicFileAttributes attrs) mchung@1472: throws IOException mchung@1472: { mchung@2139: String fn = p.getFileName().toString(); mchung@2139: if (fn.endsWith(".jar")) { mchung@2139: // JDK may cobundle with JavaFX that doesn't belong to any profile mchung@2139: // Treat jfxrt.jar as regular Archive mchung@2139: result.add(fn.equals("jfxrt.jar") mchung@2139: ? new Archive(p, ClassFileReader.newInstance(p)) mchung@2139: : new JDKArchive(p, ClassFileReader.newInstance(p))); mchung@1472: } mchung@1472: return FileVisitResult.CONTINUE; mchung@1472: } mchung@1472: }); mchung@1472: return result; mchung@1472: } mchung@2139: mchung@2139: /** mchung@2139: * A JDK archive is part of the JDK containing the Java SE API mchung@2139: * or implementation classes (i.e. JDK internal API) mchung@2139: */ mchung@2139: static class JDKArchive extends Archive { mchung@2139: JDKArchive(Path p, ClassFileReader reader) { mchung@2139: super(p, reader); mchung@2139: } mchung@2139: } mchung@1472: }