langtools/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/ClassFileReader.java
author alanb
Thu, 17 Mar 2016 19:04:28 +0000
changeset 36526 3b41f1c69604
parent 34752 9c262a013456
child 38524 badd925c1d2f
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: jjg, jlahoda, vromero, mcimadamore, bpatel, ksrini, darcy, anazarov, dfuchs Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, jan.lahoda@oracle.com, vicente.romero@oracle.com, andreas.lundblad@oracle.com, andrey.x.nazarov@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, kumar.x.srinivasan@oracle.com, sundararajan.athijegannathan@oracle.com
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: 22449
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
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 30846
diff changeset
    25
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    26
package com.sun.tools.jdeps;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    27
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    28
import com.sun.tools.classfile.ClassFile;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    29
import com.sun.tools.classfile.ConstantPoolException;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    30
import com.sun.tools.classfile.Dependencies.ClassFileError;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    31
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    32
import java.io.File;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    33
import java.io.FileNotFoundException;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    34
import java.io.IOException;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    35
import java.io.InputStream;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    36
import java.io.UncheckedIOException;
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    37
import java.nio.file.FileSystem;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    38
import java.nio.file.FileSystems;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    39
import java.nio.file.Files;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    40
import java.nio.file.Path;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    41
import java.util.ArrayList;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    42
import java.util.Collections;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    43
import java.util.Enumeration;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    44
import java.util.Iterator;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    45
import java.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    46
import java.util.NoSuchElementException;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    47
import java.util.Set;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    48
import java.util.jar.JarEntry;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    49
import java.util.jar.JarFile;
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    50
import java.util.stream.Collectors;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    51
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    52
/**
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    53
 * ClassFileReader reads ClassFile(s) of a given path that can be
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    54
 * a .class file, a directory, or a JAR file.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    55
 */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    56
public class ClassFileReader {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    57
    /**
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    58
     * Returns a ClassFileReader instance of a given path.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    59
     */
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    60
    public static ClassFileReader newInstance(Path path) throws IOException {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    61
        if (!Files.exists(path)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    62
            throw new FileNotFoundException(path.toString());
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    63
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    64
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    65
        if (Files.isDirectory(path)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    66
            return new DirectoryReader(path);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    67
        } else if (path.getFileName().toString().endsWith(".jar")) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    68
            return new JarFileReader(path);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    69
        } else {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    70
            return new ClassFileReader(path);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    71
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    72
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    73
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
    74
    /**
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
    75
     * Returns a ClassFileReader instance of a given JarFile.
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
    76
     */
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
    77
    public static ClassFileReader newInstance(Path path, JarFile jf) throws IOException {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
    78
        return new JarFileReader(path, jf);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
    79
    }
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
    80
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    81
    /**
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    82
     * Returns a ClassFileReader instance of a given FileSystem and path.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    83
     *
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    84
     * This method is used for reading classes from jrtfs.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    85
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    86
    public static ClassFileReader newInstance(FileSystem fs, Path path) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    87
        return new DirectoryReader(fs, path);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    88
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    89
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    90
    protected final Path path;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    91
    protected final String baseFileName;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    92
    protected Set<String> entries; // binary names
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    93
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
    94
    protected final List<String> skippedEntries = new ArrayList<>();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
    95
    protected ClassFileReader(Path path) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    96
        this.path = path;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    97
        this.baseFileName = path.getFileName() != null
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    98
                                ? path.getFileName().toString()
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    99
                                : path.toString();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   100
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   101
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   102
    public String getFileName() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   103
        return baseFileName;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   104
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   105
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   106
    public List<String> skippedEntries() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   107
        return skippedEntries;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   108
    }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   109
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   110
    /**
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   111
     * Returns all entries in this archive.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   112
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   113
    public Set<String> entries() {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   114
        Set<String> es = this.entries;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   115
        if (es == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   116
            // lazily scan the entries
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   117
            this.entries = scan();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   118
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   119
        return this.entries;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   120
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   121
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   122
    /**
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   123
     * Returns the ClassFile matching the given binary name
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   124
     * or a fully-qualified class name.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   125
     */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   126
    public ClassFile getClassFile(String name) throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   127
        if (name.indexOf('.') > 0) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   128
            int i = name.lastIndexOf('.');
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   129
            String pathname = name.replace('.', File.separatorChar) + ".class";
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   130
            if (baseFileName.equals(pathname) ||
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   131
                    baseFileName.equals(pathname.substring(0, i) + "$" +
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   132
                                        pathname.substring(i+1, pathname.length()))) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   133
                return readClassFile(path);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   134
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   135
        } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   136
            if (baseFileName.equals(name.replace('/', File.separatorChar) + ".class")) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   137
                return readClassFile(path);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   138
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   139
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   140
        return null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   141
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   142
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   143
    public Iterable<ClassFile> getClassFiles() throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   144
        return new Iterable<ClassFile>() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   145
            public Iterator<ClassFile> iterator() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   146
                return new FileIterator();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   147
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   148
        };
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   149
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   150
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   151
    protected ClassFile readClassFile(Path p) throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   152
        InputStream is = null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   153
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   154
            is = Files.newInputStream(p);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   155
            return ClassFile.read(is);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   156
        } catch (ConstantPoolException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   157
            throw new ClassFileError(e);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   158
        } finally {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   159
            if (is != null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   160
                is.close();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   161
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   162
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   163
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   164
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   165
    protected Set<String> scan() {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   166
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   167
            ClassFile cf = ClassFile.read(path);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   168
            return Collections.singleton(cf.getName());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   169
        } catch (ConstantPoolException|IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   170
            throw new ClassFileError(e);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   171
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   172
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   173
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   174
    static boolean isClass(Path file) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   175
        String fn = file.getFileName().toString();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   176
        return fn.endsWith(".class") && !fn.equals(MODULE_INFO);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   177
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   178
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   179
    class FileIterator implements Iterator<ClassFile> {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   180
        int count;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   181
        FileIterator() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   182
            this.count = 0;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   183
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   184
        public boolean hasNext() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   185
            return count == 0 && baseFileName.endsWith(".class");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   186
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   187
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   188
        public ClassFile next() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   189
            if (!hasNext()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   190
                throw new NoSuchElementException();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   191
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   192
            try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   193
                ClassFile cf = readClassFile(path);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   194
                count++;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   195
                return cf;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   196
            } catch (IOException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   197
                throw new ClassFileError(e);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   198
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   199
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   200
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   201
        public void remove() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   202
            throw new UnsupportedOperationException("Not supported yet.");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   203
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   204
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   205
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   206
    public String toString() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   207
        return path.toString();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   208
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   209
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   210
    private static class DirectoryReader extends ClassFileReader {
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   211
        protected final String fsSep;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   212
        DirectoryReader(Path path) throws IOException {
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   213
            this(FileSystems.getDefault(), path);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   214
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   215
        DirectoryReader(FileSystem fs, Path path) throws IOException {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   216
            super(path);
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   217
            this.fsSep = fs.getSeparator();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   218
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   219
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   220
        protected Set<String> scan() {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   221
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   222
                return Files.walk(path, Integer.MAX_VALUE)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   223
                        .filter(ClassFileReader::isClass)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   224
                        .map(f -> path.relativize(f))
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   225
                        .map(Path::toString)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   226
                        .map(p -> p.replace(File.separatorChar, '/'))
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   227
                        .collect(Collectors.toSet());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   228
            } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   229
                throw new UncheckedIOException(e);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   230
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   231
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   232
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   233
        public ClassFile getClassFile(String name) throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   234
            if (name.indexOf('.') > 0) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   235
                int i = name.lastIndexOf('.');
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   236
                String pathname = name.replace(".", fsSep) + ".class";
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   237
                Path p = path.resolve(pathname);
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   238
                if (!Files.exists(p)) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   239
                    p = path.resolve(pathname.substring(0, i) + "$" +
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   240
                            pathname.substring(i+1, pathname.length()));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   241
                }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   242
                if (Files.exists(p)) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   243
                    return readClassFile(p);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   244
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   245
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   246
                Path p = path.resolve(name + ".class");
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   247
                if (Files.exists(p)) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   248
                    return readClassFile(p);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   249
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   250
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   251
            return null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   252
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   253
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   254
        public Iterable<ClassFile> getClassFiles() throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   255
            final Iterator<ClassFile> iter = new DirectoryIterator();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   256
            return new Iterable<ClassFile>() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   257
                public Iterator<ClassFile> iterator() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   258
                    return iter;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   259
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   260
            };
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   261
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   262
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   263
        class DirectoryIterator implements Iterator<ClassFile> {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   264
            private List<Path> entries;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   265
            private int index = 0;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   266
            DirectoryIterator() throws IOException {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   267
                entries = Files.walk(path, Integer.MAX_VALUE)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   268
                               .filter(ClassFileReader::isClass)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   269
                               .collect(Collectors.toList());
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   270
                index = 0;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   271
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   272
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   273
            public boolean hasNext() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   274
                return index != entries.size();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   275
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   276
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   277
            public ClassFile next() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   278
                if (!hasNext()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   279
                    throw new NoSuchElementException();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   280
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   281
                Path path = entries.get(index++);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   282
                try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   283
                    return readClassFile(path);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   284
                } catch (IOException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   285
                    throw new ClassFileError(e);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   286
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   287
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   288
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   289
            public void remove() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   290
                throw new UnsupportedOperationException("Not supported yet.");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   291
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   292
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   293
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   294
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   295
    static class JarFileReader extends ClassFileReader {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   296
        private final JarFile jarfile;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   297
        JarFileReader(Path path) throws IOException {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   298
            this(path, new JarFile(path.toFile(), false));
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
   299
        }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   300
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
   301
        JarFileReader(Path path, JarFile jf) throws IOException {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   302
            super(path);
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
   303
            this.jarfile = jf;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   304
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   305
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   306
        protected Set<String> scan() {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   307
            try (JarFile jf = new JarFile(path.toFile())) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   308
                return jf.stream().map(JarEntry::getName)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   309
                         .filter(n -> n.endsWith(".class") && !n.endsWith(MODULE_INFO))
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   310
                         .collect(Collectors.toSet());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   311
            } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   312
                throw new UncheckedIOException(e);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   313
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   314
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   315
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   316
        public ClassFile getClassFile(String name) throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   317
            if (name.indexOf('.') > 0) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   318
                int i = name.lastIndexOf('.');
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   319
                String entryName = name.replace('.', '/') + ".class";
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   320
                JarEntry e = jarfile.getJarEntry(entryName);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   321
                if (e == null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   322
                    e = jarfile.getJarEntry(entryName.substring(0, i) + "$"
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   323
                            + entryName.substring(i + 1, entryName.length()));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   324
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   325
                if (e != null) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   326
                    return readClassFile(jarfile, e);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   327
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   328
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   329
                JarEntry e = jarfile.getJarEntry(name + ".class");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   330
                if (e != null) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   331
                    return readClassFile(jarfile, e);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   332
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   333
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   334
            return null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   335
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   336
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   337
        protected ClassFile readClassFile(JarFile jarfile, JarEntry e) throws IOException {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   338
            InputStream is = null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   339
            try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   340
                is = jarfile.getInputStream(e);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   341
                return ClassFile.read(is);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   342
            } catch (ConstantPoolException ex) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   343
                throw new ClassFileError(ex);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   344
            } finally {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   345
                if (is != null)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   346
                    is.close();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   347
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   348
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   349
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   350
        public Iterable<ClassFile> getClassFiles() throws IOException {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   351
            final Iterator<ClassFile> iter = new JarFileIterator(this, jarfile);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   352
            return new Iterable<ClassFile>() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   353
                public Iterator<ClassFile> iterator() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   354
                    return iter;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   355
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   356
            };
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   357
        }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   358
    }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   359
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   360
    class JarFileIterator implements Iterator<ClassFile> {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   361
        protected final JarFileReader reader;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   362
        protected Enumeration<JarEntry> entries;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   363
        protected JarFile jf;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   364
        protected JarEntry nextEntry;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   365
        protected ClassFile cf;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   366
        JarFileIterator(JarFileReader reader) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   367
            this(reader, null);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   368
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   369
        JarFileIterator(JarFileReader reader, JarFile jarfile) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   370
            this.reader = reader;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   371
            setJarFile(jarfile);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   372
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   373
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   374
        void setJarFile(JarFile jarfile) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   375
            if (jarfile == null) return;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   376
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   377
            this.jf = jarfile;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   378
            this.entries = jf.entries();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   379
            this.nextEntry = nextEntry();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   380
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   381
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   382
        public boolean hasNext() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   383
            if (nextEntry != null && cf != null) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   384
                return true;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   385
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   386
            while (nextEntry != null) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   387
                try {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   388
                    cf = reader.readClassFile(jf, nextEntry);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   389
                    return true;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   390
                } catch (ClassFileError | IOException ex) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   391
                    skippedEntries.add(nextEntry.getName());
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   392
                }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   393
                nextEntry = nextEntry();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   394
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   395
            return false;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   396
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   397
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   398
        public ClassFile next() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   399
            if (!hasNext()) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   400
                throw new NoSuchElementException();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   401
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   402
            ClassFile classFile = cf;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   403
            cf = null;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   404
            nextEntry = nextEntry();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   405
            return classFile;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   406
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   407
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   408
        protected JarEntry nextEntry() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   409
            while (entries.hasMoreElements()) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   410
                JarEntry e = entries.nextElement();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   411
                String name = e.getName();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   412
                if (name.endsWith(".class") && !name.equals(MODULE_INFO)) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   413
                    return e;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   414
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   415
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   416
            return null;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   417
        }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   418
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   419
        public void remove() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   420
            throw new UnsupportedOperationException("Not supported yet.");
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   421
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   422
    }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   423
    private static final String MODULE_INFO = "module-info.class";
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   424
}