langtools/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/ClassFileReader.java
author mcimadamore
Fri, 16 Dec 2016 15:27:34 +0000
changeset 42827 36468b5fa7f4
parent 42407 f3702cff2933
permissions -rw-r--r--
8181370: Convert anonymous inner classes into lambdas/method references Reviewed-by: jjg, rfield, mchung
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
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41443
diff changeset
    28
import com.sun.tools.classfile.AccessFlags;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    29
import com.sun.tools.classfile.ClassFile;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    30
import com.sun.tools.classfile.ConstantPoolException;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    31
import com.sun.tools.classfile.Dependencies.ClassFileError;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    32
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
    33
import jdk.internal.util.jar.VersionedStream;
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
    34
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
    35
import java.io.Closeable;
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    36
import java.io.File;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    37
import java.io.FileNotFoundException;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    38
import java.io.IOException;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    39
import java.io.InputStream;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    40
import java.io.UncheckedIOException;
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    41
import java.nio.file.FileSystem;
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    42
import java.nio.file.FileSystems;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    43
import java.nio.file.Files;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    44
import java.nio.file.Path;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    45
import java.util.ArrayList;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    46
import java.util.Collections;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    47
import java.util.Enumeration;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    48
import java.util.Iterator;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    49
import java.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    50
import java.util.NoSuchElementException;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    51
import java.util.Set;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    52
import java.util.jar.JarEntry;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    53
import java.util.jar.JarFile;
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
    54
import java.util.stream.Collectors;
38529
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
    55
import java.util.stream.Stream;
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
    56
import java.util.zip.ZipFile;
15030
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
/**
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    59
 * 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
    60
 * a .class file, a directory, or a JAR file.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    61
 */
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
    62
public class ClassFileReader implements Closeable {
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
     * Returns a ClassFileReader instance of a given path.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    65
     */
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    66
    public static ClassFileReader newInstance(Path path) throws IOException {
41442
14db641d4a9f 8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents: 41164
diff changeset
    67
        return newInstance(path, null);
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
    68
    }
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
    69
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
    70
    /**
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
    71
     * Returns a ClassFileReader instance of a given path.
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
    72
     */
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
    73
    public static ClassFileReader newInstance(Path path, Runtime.Version version) throws IOException {
38529
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
    74
        if (Files.notExists(path)) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    75
            throw new FileNotFoundException(path.toString());
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    76
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    77
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    78
        if (Files.isDirectory(path)) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    79
            return new DirectoryReader(path);
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    80
        } else if (path.getFileName().toString().endsWith(".jar")) {
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
    81
            return new JarFileReader(path, version);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    82
        } else {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    83
            return new ClassFileReader(path);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    84
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    85
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    86
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
    87
    /**
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    88
     * Returns a ClassFileReader instance of a given FileSystem and path.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    89
     *
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    90
     * This method is used for reading classes from jrtfs.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    91
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    92
    public static ClassFileReader newInstance(FileSystem fs, Path path) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    93
        return new DirectoryReader(fs, path);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    94
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    95
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    96
    protected final Path path;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    97
    protected final String baseFileName;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    98
    protected Set<String> entries; // binary names
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    99
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   100
    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
   101
    protected ClassFileReader(Path path) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   102
        this.path = path;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   103
        this.baseFileName = path.getFileName() != null
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   104
                                ? path.getFileName().toString()
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   105
                                : path.toString();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   106
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   107
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   108
    public String getFileName() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   109
        return baseFileName;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   110
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   111
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   112
    public List<String> skippedEntries() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   113
        return skippedEntries;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   114
    }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   115
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   116
    /**
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   117
     * Returns all entries in this archive.
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   118
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   119
    public Set<String> entries() {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   120
        Set<String> es = this.entries;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   121
        if (es == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   122
            // lazily scan the entries
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   123
            this.entries = scan();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   124
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   125
        return this.entries;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   126
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   127
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   128
    /**
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   129
     * Returns the ClassFile matching the given binary name
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   130
     * or a fully-qualified class name.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   131
     */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   132
    public ClassFile getClassFile(String name) throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   133
        if (name.indexOf('.') > 0) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   134
            int i = name.lastIndexOf('.');
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   135
            String pathname = name.replace('.', File.separatorChar) + ".class";
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   136
            if (baseFileName.equals(pathname) ||
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   137
                    baseFileName.equals(pathname.substring(0, i) + "$" +
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   138
                                        pathname.substring(i+1, pathname.length()))) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   139
                return readClassFile(path);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   140
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   141
        } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   142
            if (baseFileName.equals(name.replace('/', File.separatorChar) + ".class")) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   143
                return readClassFile(path);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   144
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   145
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   146
        return null;
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
    public Iterable<ClassFile> getClassFiles() throws IOException {
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42407
diff changeset
   150
        return FileIterator::new;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   151
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   152
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   153
    protected ClassFile readClassFile(Path p) throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   154
        InputStream is = null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   155
        try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   156
            is = Files.newInputStream(p);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   157
            return ClassFile.read(is);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   158
        } catch (ConstantPoolException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   159
            throw new ClassFileError(e);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   160
        } finally {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   161
            if (is != null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   162
                is.close();
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
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   165
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   166
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   167
    protected Set<String> scan() {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   168
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   169
            ClassFile cf = ClassFile.read(path);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41443
diff changeset
   170
            String name = cf.access_flags.is(AccessFlags.ACC_MODULE)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41443
diff changeset
   171
                ? "module-info" : cf.getName();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41443
diff changeset
   172
            return Collections.singleton(name);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   173
        } catch (ConstantPoolException|IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   174
            throw new ClassFileError(e);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   175
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   176
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   177
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   178
    static boolean isClass(Path file) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   179
        String fn = file.getFileName().toString();
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   180
        return fn.endsWith(".class");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   181
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   182
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   183
    @Override
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   184
    public void close() throws IOException {
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   185
    }
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   186
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   187
    class FileIterator implements Iterator<ClassFile> {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   188
        int count;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   189
        FileIterator() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   190
            this.count = 0;
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
        public boolean hasNext() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   193
            return count == 0 && baseFileName.endsWith(".class");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   194
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   195
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   196
        public ClassFile next() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   197
            if (!hasNext()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   198
                throw new NoSuchElementException();
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
            try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   201
                ClassFile cf = readClassFile(path);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   202
                count++;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   203
                return cf;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   204
            } catch (IOException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   205
                throw new ClassFileError(e);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   206
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   207
        }
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
        public void remove() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   210
            throw new UnsupportedOperationException("Not supported yet.");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   211
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   212
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   213
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   214
    public String toString() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   215
        return path.toString();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   216
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   217
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   218
    private static class DirectoryReader extends ClassFileReader {
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   219
        protected final String fsSep;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   220
        DirectoryReader(Path path) throws IOException {
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   221
            this(FileSystems.getDefault(), path);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   222
        }
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   223
        DirectoryReader(FileSystem fs, Path path) throws IOException {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   224
            super(path);
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   225
            this.fsSep = fs.getSeparator();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   226
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   227
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   228
        protected Set<String> scan() {
38529
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   229
            try (Stream<Path> stream = Files.walk(path, Integer.MAX_VALUE)) {
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   230
                return stream.filter(ClassFileReader::isClass)
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42407
diff changeset
   231
                             .map(path::relativize)
38529
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   232
                             .map(Path::toString)
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   233
                             .map(p -> p.replace(File.separatorChar, '/'))
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   234
                             .collect(Collectors.toSet());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   235
            } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   236
                throw new UncheckedIOException(e);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   237
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   238
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   239
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   240
        public ClassFile getClassFile(String name) throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   241
            if (name.indexOf('.') > 0) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   242
                int i = name.lastIndexOf('.');
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   243
                String pathname = name.replace(".", fsSep) + ".class";
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   244
                Path p = path.resolve(pathname);
38529
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   245
                if (Files.notExists(p)) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   246
                    p = path.resolve(pathname.substring(0, i) + "$" +
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   247
                            pathname.substring(i+1, pathname.length()));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   248
                }
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   249
                if (Files.exists(p)) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   250
                    return readClassFile(p);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   251
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   252
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   253
                Path p = path.resolve(name + ".class");
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   254
                if (Files.exists(p)) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   255
                    return readClassFile(p);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   256
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   257
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   258
            return null;
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
        public Iterable<ClassFile> getClassFiles() throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   262
            final Iterator<ClassFile> iter = new DirectoryIterator();
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42407
diff changeset
   263
            return () -> iter;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   264
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   265
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   266
        class DirectoryIterator implements Iterator<ClassFile> {
38529
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   267
            private final List<Path> entries;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   268
            private int index = 0;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   269
            DirectoryIterator() throws IOException {
38529
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   270
                List<Path> paths = null;
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   271
                try (Stream<Path> stream = Files.walk(path, Integer.MAX_VALUE)) {
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   272
                    paths = stream.filter(ClassFileReader::isClass)
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   273
                                  .collect(Collectors.toList());
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   274
                }
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   275
                this.entries = paths;
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38524
diff changeset
   276
                this.index = 0;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   277
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   278
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   279
            public boolean hasNext() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   280
                return index != entries.size();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   281
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   282
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   283
            public ClassFile next() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   284
                if (!hasNext()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   285
                    throw new NoSuchElementException();
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
                Path path = entries.get(index++);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   288
                try {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   289
                    return readClassFile(path);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   290
                } catch (IOException e) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   291
                    throw new ClassFileError(e);
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
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   295
            public void remove() {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   296
                throw new UnsupportedOperationException("Not supported yet.");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   297
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   298
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   299
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   300
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   301
    static class JarFileReader extends ClassFileReader {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   302
        private final JarFile jarfile;
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   303
        private final Runtime.Version version;
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   304
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   305
        JarFileReader(Path path, Runtime.Version version) throws IOException {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   306
            this(path, openJarFile(path.toFile(), version), version);
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
   307
        }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   308
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   309
        JarFileReader(Path path, JarFile jf, Runtime.Version version) throws IOException {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   310
            super(path);
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 15030
diff changeset
   311
            this.jarfile = jf;
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   312
            this.version = version;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   313
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   314
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   315
        @Override
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   316
        public void close() throws IOException {
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   317
            jarfile.close();
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   318
        }
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   319
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   320
        private static JarFile openJarFile(File f, Runtime.Version version)
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   321
                throws IOException {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   322
            JarFile jf;
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   323
            if (version == null) {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   324
                jf = new JarFile(f, false);
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   325
                if (jf.isMultiRelease()) {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   326
                    throw new MultiReleaseException("err.multirelease.option.notfound", f.getName());
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   327
                }
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   328
            } else {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   329
                jf = new JarFile(f, false, ZipFile.OPEN_READ, version);
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   330
                if (!jf.isMultiRelease()) {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   331
                    throw new MultiReleaseException("err.multirelease.option.exists", f.getName());
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   332
                }
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   333
            }
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   334
            return jf;
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   335
        }
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   336
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   337
        protected Set<String> scan() {
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   338
            try (JarFile jf = openJarFile(path.toFile(), version)) {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   339
                return VersionedStream.stream(jf).map(JarEntry::getName)
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   340
                         .filter(n -> n.endsWith(".class"))
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   341
                         .collect(Collectors.toSet());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   342
            } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   343
                throw new UncheckedIOException(e);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   344
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   345
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   346
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   347
        public ClassFile getClassFile(String name) throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   348
            if (name.indexOf('.') > 0) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   349
                int i = name.lastIndexOf('.');
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   350
                String entryName = name.replace('.', '/') + ".class";
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   351
                JarEntry e = jarfile.getJarEntry(entryName);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   352
                if (e == null) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   353
                    e = jarfile.getJarEntry(entryName.substring(0, i) + "$"
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   354
                            + entryName.substring(i + 1, entryName.length()));
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
                if (e != null) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   357
                    return readClassFile(jarfile, e);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   358
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   359
            } else {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   360
                JarEntry e = jarfile.getJarEntry(name + ".class");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   361
                if (e != null) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   362
                    return readClassFile(jarfile, e);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   363
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   364
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   365
            return null;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   366
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   367
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   368
        protected ClassFile readClassFile(JarFile jarfile, JarEntry e) throws IOException {
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   369
            try (InputStream is = jarfile.getInputStream(e)) {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   370
                ClassFile cf = ClassFile.read(is);
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   371
                if (jarfile.isMultiRelease()) {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   372
                    VersionHelper.add(jarfile, e, cf);
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   373
                }
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   374
                return cf;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   375
            } catch (ConstantPoolException ex) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   376
                throw new ClassFileError(ex);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   377
            }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   378
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   379
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   380
        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
   381
            final Iterator<ClassFile> iter = new JarFileIterator(this, jarfile);
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42407
diff changeset
   382
            return () -> iter;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   383
        }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   384
    }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   385
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   386
    Enumeration<JarEntry> versionedEntries(JarFile jf) {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   387
        Iterator<JarEntry> it = VersionedStream.stream(jf).iterator();
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   388
        return new Enumeration<>() {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   389
            @Override
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   390
            public boolean hasMoreElements() {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   391
                return it.hasNext();
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   392
            }
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   393
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   394
            @Override
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   395
            public JarEntry nextElement() {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   396
                return it.next();
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   397
            }
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   398
        };
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   399
    }
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   400
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   401
    class JarFileIterator implements Iterator<ClassFile> {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   402
        protected final JarFileReader reader;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   403
        protected Enumeration<JarEntry> entries;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   404
        protected JarFile jf;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   405
        protected JarEntry nextEntry;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   406
        protected ClassFile cf;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   407
        JarFileIterator(JarFileReader reader) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   408
            this(reader, null);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   409
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   410
        JarFileIterator(JarFileReader reader, JarFile jarfile) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   411
            this.reader = reader;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   412
            setJarFile(jarfile);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   413
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   414
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   415
        void setJarFile(JarFile jarfile) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   416
            if (jarfile == null) return;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   417
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   418
            this.jf = jarfile;
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 38530
diff changeset
   419
            this.entries = versionedEntries(jf);
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   420
            this.nextEntry = nextEntry();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   421
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   422
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   423
        public boolean hasNext() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   424
            if (nextEntry != null && cf != null) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   425
                return true;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   426
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   427
            while (nextEntry != null) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   428
                try {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   429
                    cf = reader.readClassFile(jf, nextEntry);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   430
                    return true;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   431
                } catch (ClassFileError | IOException ex) {
41443
6398801363f2 8167014: jdeps: Missing message: warn.skipped.entry
mchung
parents: 41442
diff changeset
   432
                    skippedEntries.add(String.format("%s: %s (%s)",
6398801363f2 8167014: jdeps: Missing message: warn.skipped.entry
mchung
parents: 41442
diff changeset
   433
                                                     ex.getMessage(),
6398801363f2 8167014: jdeps: Missing message: warn.skipped.entry
mchung
parents: 41442
diff changeset
   434
                                                     nextEntry.getName(),
6398801363f2 8167014: jdeps: Missing message: warn.skipped.entry
mchung
parents: 41442
diff changeset
   435
                                                     jf.getName()));
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   436
                }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   437
                nextEntry = nextEntry();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   438
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   439
            return false;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   440
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   441
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   442
        public ClassFile next() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   443
            if (!hasNext()) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   444
                throw new NoSuchElementException();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   445
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   446
            ClassFile classFile = cf;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   447
            cf = null;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   448
            nextEntry = nextEntry();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   449
            return classFile;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   450
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   451
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   452
        protected JarEntry nextEntry() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   453
            while (entries.hasMoreElements()) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   454
                JarEntry e = entries.nextElement();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   455
                String name = e.getName();
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   456
                if (name.endsWith(".class")) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   457
                    return e;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   458
                }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   459
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   460
            return null;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   461
        }
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   462
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   463
        public void remove() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22449
diff changeset
   464
            throw new UnsupportedOperationException("Not supported yet.");
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   465
        }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   466
    }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   467
    private static final String MODULE_INFO = "module-info.class";
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   468
}