mchung@1472: /* mchung@2538: * Copyright (c) 2012, 2014, 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@2538: import com.sun.tools.classfile.Annotation; mchung@2538: import com.sun.tools.classfile.ClassFile; mchung@2538: import com.sun.tools.classfile.ConstantPool; mchung@2538: import com.sun.tools.classfile.ConstantPoolException; mchung@2538: import com.sun.tools.classfile.RuntimeAnnotations_attribute; mchung@2538: import com.sun.tools.classfile.Dependencies.ClassFileError; 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@2538: import static com.sun.tools.classfile.Attribute.*; mchung@2538: mchung@1472: /** mchung@1472: * ClassPath for Java SE and JDK mchung@1472: */ mchung@1472: class PlatformClassPath { mchung@2538: private static final List NON_PLATFORM_JARFILES = mchung@2538: Arrays.asList("alt-rt.jar", "jfxrt.jar", "ant-javafx.jar", "javafx-mx.jar"); mchung@2538: private static final 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@2538: if (home.getParent() != null) { mchung@2538: // add tools.jar and other JDK jar files mchung@2538: Path lib = home.getParent().resolve("lib"); mchung@2538: if (Files.exists(lib)) { mchung@2538: result.addAll(addJarFiles(lib)); mchung@2538: } mchung@2538: } 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@2538: result.add(new JDKArchive(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@2538: result.add(NON_PLATFORM_JARFILES.contains(fn) mchung@2538: ? Archive.getInstance(p) mchung@2538: : new JDKArchive(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@2538: private static List PROFILE_JARS = Arrays.asList("rt.jar", "jce.jar"); mchung@2538: public static boolean isProfileArchive(Archive archive) { mchung@2538: if (archive instanceof JDKArchive) { mchung@2538: return PROFILE_JARS.contains(archive.getName()); mchung@2538: } mchung@2538: return false; mchung@2538: } mchung@2538: mchung@2538: private final Map exportedPackages = new HashMap<>(); mchung@2538: private final Map exportedTypes = new HashMap<>(); mchung@2538: JDKArchive(Path p) throws IOException { mchung@2538: super(p, ClassFileReader.newInstance(p)); mchung@2538: } mchung@2538: mchung@2538: /** mchung@2538: * Tests if a given fully-qualified name is an exported type. mchung@2538: */ mchung@2538: public boolean isExported(String cn) { mchung@2538: int i = cn.lastIndexOf('.'); mchung@2538: String pn = i > 0 ? cn.substring(0, i) : ""; mchung@2538: mchung@2538: boolean isJdkExported = isExportedPackage(pn); mchung@2538: if (exportedTypes.containsKey(cn)) { mchung@2538: return exportedTypes.get(cn); mchung@2538: } mchung@2538: return isJdkExported; mchung@2538: } mchung@2538: mchung@2538: /** mchung@2538: * Tests if a given package name is exported. mchung@2538: */ mchung@2538: public boolean isExportedPackage(String pn) { mchung@2538: if (Profile.getProfile(pn) != null) { mchung@2538: return true; mchung@2538: } mchung@2538: return exportedPackages.containsKey(pn) ? exportedPackages.get(pn) : false; mchung@2538: } mchung@2538: mchung@2538: private static final String JDK_EXPORTED_ANNOTATION = "Ljdk/Exported;"; mchung@2538: private Boolean isJdkExported(ClassFile cf) throws ConstantPoolException { mchung@2538: RuntimeAnnotations_attribute attr = (RuntimeAnnotations_attribute) mchung@2538: cf.attributes.get(RuntimeVisibleAnnotations); mchung@2538: if (attr != null) { mchung@2538: for (int i = 0; i < attr.annotations.length; i++) { mchung@2538: Annotation ann = attr.annotations[i]; mchung@2538: String annType = cf.constant_pool.getUTF8Value(ann.type_index); mchung@2538: if (JDK_EXPORTED_ANNOTATION.equals(annType)) { mchung@2538: boolean isJdkExported = true; mchung@2538: for (int j = 0; j < ann.num_element_value_pairs; j++) { mchung@2538: Annotation.element_value_pair pair = ann.element_value_pairs[j]; mchung@2538: Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) pair.value; mchung@2538: ConstantPool.CONSTANT_Integer_info info = (ConstantPool.CONSTANT_Integer_info) mchung@2538: cf.constant_pool.get(ev.const_value_index); mchung@2538: isJdkExported = info.value != 0; mchung@2538: } mchung@2538: return Boolean.valueOf(isJdkExported); mchung@2538: } mchung@2538: } mchung@2538: } mchung@2538: return null; mchung@2538: } mchung@2538: mchung@2538: void processJdkExported(ClassFile cf) throws IOException { mchung@2538: try { mchung@2538: String cn = cf.getName(); mchung@2538: String pn = cn.substring(0, cn.lastIndexOf('/')).replace('/', '.'); mchung@2538: mchung@2538: Boolean b = isJdkExported(cf); mchung@2538: if (b != null) { mchung@2538: exportedTypes.put(cn.replace('/', '.'), b); mchung@2538: } mchung@2538: if (!exportedPackages.containsKey(pn)) { mchung@2538: // check if package-info.class has @jdk.Exported mchung@2538: Boolean isJdkExported = null; mchung@2538: ClassFile pcf = reader().getClassFile(cn.substring(0, cn.lastIndexOf('/')+1) + "package-info"); mchung@2538: if (pcf != null) { mchung@2538: isJdkExported = isJdkExported(pcf); mchung@2538: } mchung@2538: if (isJdkExported != null) { mchung@2538: exportedPackages.put(pn, isJdkExported); mchung@2538: } mchung@2538: } mchung@2538: } catch (ConstantPoolException e) { mchung@2538: throw new ClassFileError(e); mchung@2538: } mchung@2139: } mchung@2139: } mchung@1472: }