langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/PlatformClassPath.java
author sla
Thu, 13 Nov 2014 07:58:28 +0100
changeset 27622 38913dc333ed
parent 26276 0dca2378aa34
child 27579 d1a63c99cdd5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     1
/*
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
     2
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     4
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    10
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    15
 * accompanied this code).
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    16
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    20
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    23
 * questions.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    24
 */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    25
package com.sun.tools.jdeps;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    26
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
    27
import com.sun.tools.classfile.Annotation;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
    28
import com.sun.tools.classfile.ClassFile;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
    29
import com.sun.tools.classfile.ConstantPool;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
    30
import com.sun.tools.classfile.ConstantPoolException;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
    31
import com.sun.tools.classfile.RuntimeAnnotations_attribute;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
    32
import com.sun.tools.classfile.Dependencies.ClassFileError;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    33
import java.io.IOException;
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    34
import java.io.InputStream;
26276
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
    35
import java.io.BufferedInputStream;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    36
import java.nio.file.FileVisitResult;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    37
import java.nio.file.Files;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    38
import java.nio.file.Path;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    39
import java.nio.file.Paths;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    40
import java.nio.file.SimpleFileVisitor;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    41
import java.nio.file.attribute.BasicFileAttributes;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    42
import java.util.*;
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    43
import java.util.jar.*;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    44
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
    45
import static com.sun.tools.classfile.Attribute.*;
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    46
import static com.sun.tools.jdeps.ClassFileReader.*;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
    47
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    48
/**
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    49
 * ClassPath for Java SE and JDK
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    50
 */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    51
class PlatformClassPath {
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    52
    private static List<Archive> modules;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    53
    static synchronized List<Archive> getArchives(Path mpath) throws IOException {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    54
        if (modules == null) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    55
            initPlatformArchives(mpath);
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    56
        }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    57
        return modules;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    58
    }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    59
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    60
    /**
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    61
     * Finds the module with the given name. Returns null
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    62
     * if such module doesn't exist.
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    63
     *
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    64
     * @param mn module name
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
    65
     */
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    66
    static Module findModule(String mn) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    67
        for (Archive a : modules) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    68
            if (Module.class.isInstance(a)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    69
                Module m = (Module)a;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    70
                if (mn.equals(m.name())) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    71
                    return m;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    72
                }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
    73
            }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    74
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    75
        return null;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    76
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    77
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    78
    private static List<Archive> initPlatformArchives(Path mpath) throws IOException {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    79
        Path home = Paths.get(System.getProperty("java.home"));
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    80
        if (mpath == null && !home.endsWith("jre")) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    81
            // jdk build
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    82
            Path p = home.resolve("modules");
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    83
            if (Files.isDirectory(p)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    84
                mpath = p;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    85
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
    86
        }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    87
        modules = mpath != null ? initModules(mpath) : initLegacyImage(home);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    88
        if (findModule("java.base") != null) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    89
            Profile.initProfiles();
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    90
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    91
        return modules;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    92
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    93
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
    94
    private static List<Archive> initModules(Path mpath) throws IOException {
26276
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
    95
        String fn = System.getProperty("jdeps.modules.xml");
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
    96
        if (fn!= null) {
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
    97
            Path p = Paths.get(fn);
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
    98
            try (InputStream in = new BufferedInputStream(Files.newInputStream(p))) {
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
    99
                return new ArrayList<Archive>(ModulesXmlReader.load(mpath, in));
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
   100
            }
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
   101
        } else {
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
   102
            try (InputStream in = PlatformClassPath.class
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
   103
                    .getResourceAsStream("resources/jdeps-modules.xml")) {
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
   104
                return new ArrayList<Archive>(ModulesXmlReader.load(mpath, in));
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
   105
            }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   106
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   107
    }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   108
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   109
    private static List<Archive> initLegacyImage(Path home) throws IOException {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   110
        LegacyImageHelper cfr = new LegacyImageHelper(home);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   111
        List<Archive> archives = new ArrayList<>(cfr.nonPlatformArchives);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   112
        try (InputStream in = PlatformClassPath.class
26276
0dca2378aa34 8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents: 25874
diff changeset
   113
                .getResourceAsStream("resources/jdeps-modules.xml")) {
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   114
            archives.addAll(ModulesXmlReader.loadFromImage(cfr, in));
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   115
            return archives;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   116
        }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   117
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   118
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   119
    static class LegacyImageHelper {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   120
        private static final List<String> NON_PLATFORM_JARFILES =
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   121
                Arrays.asList("alt-rt.jar", "jfxrt.jar", "ant-javafx.jar", "javafx-mx.jar");
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   122
        final List<Archive> nonPlatformArchives = new ArrayList<>();
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   123
        final List<JarFile> jarfiles = new ArrayList<>();
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   124
        final Path home;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   125
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   126
        LegacyImageHelper(Path home) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   127
            this.home = home;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   128
            try {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   129
                if (home.endsWith("jre")) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   130
                    // jar files in <javahome>/jre/lib
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   131
                    addJarFiles(home.resolve("lib"));
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   132
                    if (home.getParent() != null) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   133
                        // add tools.jar and other JDK jar files
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   134
                        Path lib = home.getParent().resolve("lib");
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   135
                        if (Files.exists(lib)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   136
                            addJarFiles(lib);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   137
                        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   138
                    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   139
                } else if (Files.exists(home.resolve("lib"))) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   140
                    // add other JAR files
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   141
                    addJarFiles(home.resolve("lib"));
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   142
                } else {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   143
                    throw new RuntimeException("\"" + home + "\" not a JDK home");
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   144
                }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   145
            } catch (IOException e) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   146
                throw new Error(e);
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   147
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   148
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   149
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   150
        /**
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   151
         * Returns a ClassFileReader that only reads classes for the given modulename.
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   152
         */
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   153
        ClassFileReader getClassReader(String modulename, Set<String> packages) throws IOException {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   154
            return new ModuleClassReader(modulename, packages);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   155
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   156
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   157
        private void addJarFiles(final Path root) throws IOException {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   158
            final Path ext = root.resolve("ext");
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   159
            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   160
                @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   161
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   162
                    throws IOException
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   163
                {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   164
                    if (dir.equals(root) || dir.equals(ext)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   165
                        return FileVisitResult.CONTINUE;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   166
                    } else {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   167
                        // skip other cobundled JAR files
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   168
                        return FileVisitResult.SKIP_SUBTREE;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   169
                    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   170
                }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   171
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   172
                @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   173
                public FileVisitResult visitFile(Path p, BasicFileAttributes attrs)
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   174
                    throws IOException
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   175
                {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   176
                    String fn = p.getFileName().toString();
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   177
                    if (fn.endsWith(".jar")) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   178
                        // JDK may cobundle with JavaFX that doesn't belong to any profile
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   179
                        // Treat jfxrt.jar as regular Archive
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   180
                        if (NON_PLATFORM_JARFILES.contains(fn)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   181
                            nonPlatformArchives.add(Archive.getInstance(p));
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   182
                        } else {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   183
                            jarfiles.add(new JarFile(p.toFile()));
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   184
                        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   185
                    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   186
                    return FileVisitResult.CONTINUE;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   187
                }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   188
            });
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   189
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   190
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   191
        /**
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   192
         * ModuleClassFile reads classes for the specified module from the legacy image.
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   193
         *
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   194
         */
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   195
        class ModuleClassReader extends JarFileReader {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   196
            private JarFile cachedJarFile = getJarFile(0);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   197
            private final Set<String> packages;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   198
            private final String module;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   199
            ModuleClassReader(String module, Set<String> packages) throws IOException {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   200
                super(home, null);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   201
                this.module = module;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   202
                this.packages = packages;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   203
            }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   204
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   205
            private boolean includes(String name) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   206
                String cn = name.replace('/', '.');
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   207
                int i = cn.lastIndexOf('.');
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   208
                String pn = i > 0 ? cn.substring(0, i) : "";
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   209
                return packages.contains(pn);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   210
            }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   211
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   212
            private JarEntry findJarEntry(JarFile jarfile, String entryName1, String entryName2) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   213
                JarEntry e = jarfile.getJarEntry(entryName1);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   214
                if (e == null) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   215
                    e = jarfile.getJarEntry(entryName2);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   216
                }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   217
                return e;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   218
            }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   219
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   220
            public String toString() {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   221
                return module + " " + packages.size() + " " + packages;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   222
            }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   223
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   224
            @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   225
            public ClassFile getClassFile(String name) throws IOException {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   226
                if (jarfiles.isEmpty() || !includes(name)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   227
                    return null;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   228
                }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   229
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   230
                if (name.indexOf('.') > 0) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   231
                    int i = name.lastIndexOf('.');
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   232
                    String entryName = name.replace('.', '/') + ".class";
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   233
                    String innerClassName = entryName.substring(0, i) + "$"
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   234
                            + entryName.substring(i + 1, entryName.length());
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   235
                    JarEntry e = findJarEntry(cachedJarFile, entryName, innerClassName);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   236
                    if (e != null) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   237
                        return readClassFile(cachedJarFile, e);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   238
                    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   239
                    for (JarFile jf : jarfiles) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   240
                        if (jf == cachedJarFile) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   241
                            continue;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   242
                        }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   243
                        System.err.format("find jar entry %s at %s%n", entryName, jf);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   244
                        e = findJarEntry(jf, entryName, innerClassName);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   245
                        if (e != null) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   246
                            cachedJarFile = jf;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   247
                            return readClassFile(jf, e);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   248
                        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   249
                    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   250
                } else {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   251
                    String entryName = name + ".class";
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   252
                    JarEntry e = cachedJarFile.getJarEntry(entryName);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   253
                    if (e != null) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   254
                        return readClassFile(cachedJarFile, e);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   255
                    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   256
                    for (JarFile jf : jarfiles) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   257
                        if (jf == cachedJarFile) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   258
                            continue;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   259
                        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   260
                        e = jf.getJarEntry(entryName);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   261
                        if (e != null) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   262
                            cachedJarFile = jf;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   263
                            return readClassFile(jf, e);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   264
                        }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   265
                    }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   266
                }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   267
                return null;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   268
            }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   269
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   270
            @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   271
            public Iterable<ClassFile> getClassFiles() throws IOException {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   272
                final Iterator<ClassFile> iter = new ModuleClassIterator(this);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   273
                return new Iterable<ClassFile>() {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   274
                    public Iterator<ClassFile> iterator() {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   275
                        return iter;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   276
                    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   277
                };
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   278
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   279
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   280
            private JarFile getJarFile(int index) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   281
                return index < jarfiles.size() ? jarfiles.get(index) : null;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   282
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   283
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   284
            class ModuleClassIterator extends JarFileIterator {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   285
                private int index;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   286
                ModuleClassIterator(ModuleClassReader reader) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   287
                    super(reader);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   288
                    this.index = 0;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   289
                    this.jf = getJarFile(0);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   290
                    this.entries = jf != null ? jf.entries() : null;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   291
                    this.nextEntry = nextEntry();
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   292
                }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   293
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   294
                @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   295
                protected JarEntry nextEntry() {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   296
                    while (jf != null) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   297
                        while (entries.hasMoreElements()) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   298
                            JarEntry e = entries.nextElement();
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   299
                            String name = e.getName();
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   300
                            if (name.endsWith(".class") && includes(name)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   301
                                return e;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   302
                            }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   303
                        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   304
                        jf = getJarFile(++index);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   305
                        entries = jf != null ? jf.entries() : null;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   306
                    }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25442
diff changeset
   307
                    return null;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   308
                }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21046
diff changeset
   309
            }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   310
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16560
diff changeset
   311
    }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   312
}