src/jdk.jdeps/share/classes/com/sun/tools/jdeps/Analyzer.java
author mchung
Wed, 21 Nov 2018 22:34:01 -0800
changeset 52650 c16b6cc93272
parent 47216 71c04702a3d5
permissions -rw-r--r--
8213909: jdeps --print-module-deps should report missing dependences 8168869: jdeps: localized messages don't use proper line breaks Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
     1
/*
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
     4
 *
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    10
 *
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    15
 * accompanied this code).
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    16
 *
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    20
 *
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    23
 * questions.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    24
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 30846
diff changeset
    25
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    26
package com.sun.tools.jdeps;
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    27
39671
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
    28
import com.sun.tools.classfile.Dependency.Location;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    29
39671
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
    30
import java.io.BufferedReader;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    31
import java.io.IOException;
39671
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
    32
import java.io.InputStream;
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
    33
import java.io.InputStreamReader;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    34
import java.io.UncheckedIOException;
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
    35
import java.lang.module.ModuleDescriptor;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    36
import java.util.Collections;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    37
import java.util.Comparator;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    38
import java.util.HashMap;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    39
import java.util.HashSet;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    40
import java.util.Map;
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    41
import java.util.Objects;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    42
import java.util.Set;
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
    43
import java.util.function.Predicate;
25692
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
    44
import java.util.stream.Collectors;
30843
6e378856c5a2 8080608: Missing archive name from jdeps -v -e output if no dependency on other JAR
dfuchs
parents: 28706
diff changeset
    45
import java.util.stream.Stream;
25692
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
    46
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    47
/**
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    48
 * Dependency Analyzer.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    49
 */
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    50
public class Analyzer {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    51
    /**
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    52
     * Type of the dependency analysis.  Appropriate level of data
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    53
     * will be stored.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    54
     */
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    55
    public enum Type {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    56
        SUMMARY,
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    57
        MODULE,  // equivalent to summary in addition, print module descriptor
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    58
        PACKAGE,
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    59
        CLASS,
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    60
        VERBOSE
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21503
diff changeset
    61
    }
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    62
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    63
    /**
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    64
     * Filter to be applied when analyzing the dependencies from the given archives.
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    65
     * Only the accepted dependencies are recorded.
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    66
     */
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    67
    interface Filter {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    68
        boolean accepts(Location origin, Archive originArchive,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    69
                        Location target, Archive targetArchive);
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    70
    }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    71
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    72
    protected final JdepsConfiguration configuration;
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
    73
    protected final Type type;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
    74
    protected final Filter filter;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    75
    protected final Map<Archive, Dependences> results = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    76
    protected final Map<Location, Archive> locationToArchive = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
    77
    static final Archive NOT_FOUND
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    78
        = new Archive(JdepsTask.getMessage("artifact.not.found"));
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
    79
    static final Predicate<Archive> ANY = a -> true;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    80
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    81
    /**
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    82
     * Constructs an Analyzer instance.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    83
     *
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    84
     * @param type Type of the dependency analysis
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    85
     * @param filter
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    86
     */
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    87
    Analyzer(JdepsConfiguration config, Type type, Filter filter) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    88
        this.configuration = config;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    89
        this.type = type;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    90
        this.filter = filter;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    91
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    92
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    93
    /**
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    94
     * Performs the dependency analysis on the given archives.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    95
     */
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    96
    boolean run(Iterable<? extends Archive> archives,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    97
                Map<Location, Archive> locationMap)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    98
    {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    99
        this.locationToArchive.putAll(locationMap);
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   100
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   101
        // traverse and analyze all dependencies
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   102
        for (Archive archive : archives) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   103
            Dependences deps = new Dependences(archive, type);
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   104
            archive.visitDependences(deps);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   105
            results.put(archive, deps);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   106
        }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   107
        return true;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   108
    }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   109
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   110
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   111
     * Returns the analyzed archives
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   112
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   113
    Set<Archive> archives() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   114
        return results.keySet();
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   115
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   116
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   117
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   118
     * Returns true if the given archive has dependences.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   119
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   120
    boolean hasDependences(Archive archive) {
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   121
        if (results.containsKey(archive)) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   122
            return results.get(archive).dependencies().size() > 0;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   123
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   124
        return false;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   125
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   126
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   127
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   128
     * Returns the dependences, either class name or package name
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   129
     * as specified in the given verbose level, from the given source.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   130
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   131
    Set<String> dependences(Archive source) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   132
        if (!results.containsKey(source)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   133
            return Collections.emptySet();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   134
        }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   135
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   136
        return results.get(source).dependencies()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   137
                      .stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   138
                      .map(Dep::target)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   139
                      .collect(Collectors.toSet());
25692
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   140
    }
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   141
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   142
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   143
     * Returns the direct dependences of the given source
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   144
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   145
    Stream<Archive> requires(Archive source) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   146
        if (!results.containsKey(source)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   147
            return Stream.empty();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   148
        }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   149
        return results.get(source).requires()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   150
                      .stream();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   151
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   152
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   153
    interface Visitor {
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   154
        /**
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   155
         * Visits a recorded dependency from origin to target which can be
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   156
         * a fully-qualified classname, a package name, a module or
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   157
         * archive name depending on the Analyzer's type.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   158
         */
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   159
        public void visitDependence(String origin, Archive originArchive,
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   160
                                    String target, Archive targetArchive);
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   161
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   162
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   163
    /**
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   164
     * Visit the dependencies of the given source.
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   165
     * If the requested level is SUMMARY, it will visit the required archives list.
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   166
     */
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   167
    void visitDependences(Archive source, Visitor v, Type level, Predicate<Archive> targetFilter) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   168
        if (level == Type.SUMMARY) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   169
            final Dependences result = results.get(source);
30843
6e378856c5a2 8080608: Missing archive name from jdeps -v -e output if no dependency on other JAR
dfuchs
parents: 28706
diff changeset
   170
            final Set<Archive> reqs = result.requires();
6e378856c5a2 8080608: Missing archive name from jdeps -v -e output if no dependency on other JAR
dfuchs
parents: 28706
diff changeset
   171
            Stream<Archive> stream = reqs.stream();
6e378856c5a2 8080608: Missing archive name from jdeps -v -e output if no dependency on other JAR
dfuchs
parents: 28706
diff changeset
   172
            if (reqs.isEmpty()) {
6e378856c5a2 8080608: Missing archive name from jdeps -v -e output if no dependency on other JAR
dfuchs
parents: 28706
diff changeset
   173
                if (hasDependences(source)) {
6e378856c5a2 8080608: Missing archive name from jdeps -v -e output if no dependency on other JAR
dfuchs
parents: 28706
diff changeset
   174
                    // If reqs.isEmpty() and we have dependences, then it means
6e378856c5a2 8080608: Missing archive name from jdeps -v -e output if no dependency on other JAR
dfuchs
parents: 28706
diff changeset
   175
                    // that the dependences are from 'source' onto itself.
6e378856c5a2 8080608: Missing archive name from jdeps -v -e output if no dependency on other JAR
dfuchs
parents: 28706
diff changeset
   176
                    stream = Stream.of(source);
6e378856c5a2 8080608: Missing archive name from jdeps -v -e output if no dependency on other JAR
dfuchs
parents: 28706
diff changeset
   177
                }
6e378856c5a2 8080608: Missing archive name from jdeps -v -e output if no dependency on other JAR
dfuchs
parents: 28706
diff changeset
   178
            }
6e378856c5a2 8080608: Missing archive name from jdeps -v -e output if no dependency on other JAR
dfuchs
parents: 28706
diff changeset
   179
            stream.sorted(Comparator.comparing(Archive::getName))
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   180
                  .forEach(archive -> {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   181
                      Profile profile = result.getTargetProfile(archive);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   182
                      v.visitDependence(source.getName(), source,
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   183
                                        profile != null ? profile.profileName()
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   184
                                                        : archive.getName(), archive);
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   185
                  });
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   186
        } else {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   187
            Dependences result = results.get(source);
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   188
            if (level != type) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   189
                // requesting different level of analysis
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   190
                result = new Dependences(source, level, targetFilter);
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   191
                source.visitDependences(result);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   192
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   193
            result.dependencies().stream()
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   194
                  .sorted(Comparator.comparing(Dep::origin)
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   195
                                    .thenComparing(Dep::target))
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   196
                  .forEach(d -> v.visitDependence(d.origin(), d.originArchive(),
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   197
                                                  d.target(), d.targetArchive()));
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   198
        }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   199
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   200
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   201
    void visitDependences(Archive source, Visitor v) {
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   202
        visitDependences(source, v, type, ANY);
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   203
    }
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   204
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   205
    void visitDependences(Archive source, Visitor v, Type level) {
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   206
        visitDependences(source, v, level, ANY);
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   207
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   208
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   209
    /**
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   210
     * Dependences contains the dependencies for an Archive that can have one or
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   211
     * more classes.
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   212
     */
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   213
    class Dependences implements Archive.Visitor {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   214
        protected final Archive archive;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   215
        protected final Set<Archive> requires;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   216
        protected final Set<Dep> deps;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   217
        protected final Type level;
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   218
        protected final Predicate<Archive> targetFilter;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   219
        private Profile profile;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   220
        Dependences(Archive archive, Type level) {
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   221
            this(archive, level, ANY);
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   222
        }
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   223
        Dependences(Archive archive, Type level, Predicate<Archive> targetFilter) {
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   224
            this.archive = archive;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   225
            this.deps = new HashSet<>();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   226
            this.requires = new HashSet<>();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   227
            this.level = level;
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   228
            this.targetFilter = targetFilter;
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   229
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   230
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   231
        Set<Dep> dependencies() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   232
            return deps;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   233
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   234
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   235
        Set<Archive> requires() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   236
            return requires;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   237
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   238
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   239
        Profile getTargetProfile(Archive target) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   240
            if (target.getModule().isJDK()) {
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   241
                return Profile.getProfile((Module) target);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   242
            } else {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   243
                return null;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   244
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   245
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   246
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   247
        /*
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   248
         * Returns the archive that contains the given location.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   249
         */
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   250
        Archive findArchive(Location t) {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   251
            // local in this archive
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   252
            if (archive.getClasses().contains(t))
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   253
                return archive;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   254
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   255
            Archive target;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   256
            if (locationToArchive.containsKey(t)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   257
                target = locationToArchive.get(t);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   258
            } else {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   259
                // special case JDK removed API
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   260
                target = configuration.findClass(t)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   261
                    .orElseGet(() -> REMOVED_JDK_INTERNALS.contains(t)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   262
                                        ? REMOVED_JDK_INTERNALS
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   263
                                        : NOT_FOUND);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   264
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   265
            return locationToArchive.computeIfAbsent(t, _k -> target);
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   266
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   267
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   268
        // return classname or package name depending on the level
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   269
        private String getLocationName(Location o) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   270
            if (level == Type.CLASS || level == Type.VERBOSE) {
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 39671
diff changeset
   271
                return VersionHelper.get(o.getClassName());
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   272
            } else {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   273
                String pkg = o.getPackageName();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   274
                return pkg.isEmpty() ? "<unnamed>" : pkg;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   275
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   276
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   277
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   278
        @Override
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   279
        public void visit(Location o, Location t) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   280
            Archive targetArchive = findArchive(t);
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   281
            if (filter.accepts(o, archive, t, targetArchive) && targetFilter.test(targetArchive)) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   282
                addDep(o, t);
28706
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 25874
diff changeset
   283
                if (archive != targetArchive && !requires.contains(targetArchive)) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   284
                    requires.add(targetArchive);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   285
                }
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   286
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   287
            if (targetArchive.getModule().isNamed()) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   288
                Profile p = Profile.getProfile(t.getPackageName());
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   289
                if (profile == null || (p != null && p.compareTo(profile) > 0)) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   290
                    profile = p;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   291
                }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   292
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   293
        }
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   294
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   295
        private Dep curDep;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   296
        protected Dep addDep(Location o, Location t) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   297
            String origin = getLocationName(o);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   298
            String target = getLocationName(t);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   299
            Archive targetArchive = findArchive(t);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   300
            if (curDep != null &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   301
                    curDep.origin().equals(origin) &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   302
                    curDep.originArchive() == archive &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   303
                    curDep.target().equals(target) &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   304
                    curDep.targetArchive() == targetArchive) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   305
                return curDep;
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   306
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   307
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   308
            Dep e = new Dep(origin, archive, target, targetArchive);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   309
            if (deps.contains(e)) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   310
                for (Dep e1 : deps) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   311
                    if (e.equals(e1)) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   312
                        curDep = e1;
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   313
                    }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   314
                }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   315
            } else {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   316
                deps.add(e);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   317
                curDep = e;
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   318
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   319
            return curDep;
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   320
        }
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   321
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   322
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   323
    /*
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   324
     * Class-level or package-level dependency
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   325
     */
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   326
    class Dep {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   327
        final String origin;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   328
        final Archive originArchive;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   329
        final String target;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   330
        final Archive targetArchive;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   331
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   332
        Dep(String origin, Archive originArchive, String target, Archive targetArchive) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   333
            this.origin = origin;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   334
            this.originArchive = originArchive;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   335
            this.target = target;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   336
            this.targetArchive = targetArchive;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   337
        }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   338
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   339
        String origin() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   340
            return origin;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   341
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   342
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   343
        Archive originArchive() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   344
            return originArchive;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   345
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   346
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   347
        String target() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   348
            return target;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   349
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   350
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   351
        Archive targetArchive() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   352
            return targetArchive;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   353
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   354
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   355
        @Override
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   356
        @SuppressWarnings("unchecked")
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   357
        public boolean equals(Object o) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   358
            if (o instanceof Dep) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   359
                Dep d = (Dep) o;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   360
                return this.origin.equals(d.origin) &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   361
                        this.originArchive == d.originArchive &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   362
                        this.target.equals(d.target) &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   363
                        this.targetArchive == d.targetArchive;
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   364
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   365
            return false;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   366
        }
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   367
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   368
        @Override
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   369
        public int hashCode() {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   370
            return Objects.hash(this.origin,
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   371
                                this.originArchive,
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   372
                                this.target,
3b41f1c69604 8142968: Module System implementation
alanb
parents: 34752
diff changeset
   373
                                this.targetArchive);
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   374
        }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   375
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   376
        public String toString() {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   377
            return String.format("%s (%s) -> %s (%s)%n",
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   378
                    origin, originArchive.getName(),
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   379
                    target, targetArchive.getName());
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   380
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   381
    }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   382
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   383
    /*
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   384
     * Returns true if the given archive represents not found.
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   385
     */
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   386
    static boolean notFound(Archive archive) {
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   387
        return archive == NOT_FOUND || archive == REMOVED_JDK_INTERNALS;
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   388
    }
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   389
39671
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   390
    static final Jdk8Internals REMOVED_JDK_INTERNALS = new Jdk8Internals();
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   391
39671
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   392
    static class Jdk8Internals extends Module {
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   393
        private static final String NAME = "JDK removed internal API";
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   394
        private static final String JDK8_INTERNALS = "/com/sun/tools/jdeps/resources/jdk8_internals.txt";
39671
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   395
        private final Set<String> jdk8Internals;
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   396
        private Jdk8Internals() {
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   397
            super(NAME, ModuleDescriptor.newModule("jdk8internals").build(), true);
39671
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   398
            try (InputStream in = JdepsTask.class.getResourceAsStream(JDK8_INTERNALS);
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   399
                 BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   400
                this.jdk8Internals = reader.lines()
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   401
                                          .filter(ln -> !ln.startsWith("#"))
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   402
                                          .collect(Collectors.toSet());
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   403
            } catch (IOException e) {
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   404
                throw new UncheckedIOException(e);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   405
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   406
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   407
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   408
        public boolean contains(Location location) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   409
            String cn = location.getClassName();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   410
            int i = cn.lastIndexOf('.');
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   411
            String pn = i > 0 ? cn.substring(0, i) : "";
39671
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   412
5a4b6a035514 8160398: (jdeps) Replace a list of JDK 8 internal API for detecting if it's removed in JDK 9 or later
mchung
parents: 38532
diff changeset
   413
            return jdk8Internals.contains(pn);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   414
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   415
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   416
        @Override
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38524
diff changeset
   417
        public boolean isJDK() {
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38524
diff changeset
   418
            return true;
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38524
diff changeset
   419
        }
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38524
diff changeset
   420
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38524
diff changeset
   421
        @Override
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   422
        public boolean isExported(String pn) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   423
            return false;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   424
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   425
    }
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   426
}