langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/Analyzer.java
author mchung
Tue, 27 Jan 2015 19:50:41 -0800
changeset 28706 a724585645ce
parent 25874 83c19f00452c
child 30843 6e378856c5a2
permissions -rw-r--r--
8068937: jdeps shows "not found" if target class has no reference other than its own package Reviewed-by: alanb
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
 */
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    25
package com.sun.tools.jdeps;
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    26
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
    27
import java.io.PrintStream;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    28
import java.util.Comparator;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    29
import java.util.HashMap;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    30
import java.util.HashSet;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    31
import java.util.List;
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    32
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
    33
import java.util.Objects;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    34
import java.util.Set;
25692
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
    35
import java.util.stream.Collectors;
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
    36
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
    37
import com.sun.tools.classfile.Dependency.Location;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    38
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    39
/**
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    40
 * Dependency Analyzer.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    41
 */
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    42
public class Analyzer {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    43
    /**
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    44
     * Type of the dependency analysis.  Appropriate level of data
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    45
     * will be stored.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    46
     */
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    47
    public enum Type {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    48
        SUMMARY,
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    49
        PACKAGE,
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    50
        CLASS,
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    51
        VERBOSE
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21503
diff changeset
    52
    }
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    53
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    54
    /**
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    55
     * 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
    56
     * Only the accepted dependencies are recorded.
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    57
     */
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    58
    interface Filter {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    59
        boolean accepts(Location origin, Archive originArchive, Location target, Archive targetArchive);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    60
    }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    61
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
    62
    protected final Type type;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
    63
    protected final Filter filter;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
    64
    protected final Map<Archive, ArchiveDeps> results = new HashMap<>();
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
    65
    protected final Map<Location, Archive> map = new HashMap<>();
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
    66
    private static final Archive NOT_FOUND
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    67
        = new Archive(JdepsTask.getMessage("artifact.not.found"));
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    68
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    69
    /**
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    70
     * Constructs an Analyzer instance.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    71
     *
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    72
     * @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
    73
     * @param filter
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    74
     */
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    75
    public Analyzer(Type type, Filter filter) {
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    76
        this.type = type;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    77
        this.filter = filter;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    78
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    79
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
     * Performs the dependency analysis on the given archives.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
    82
     */
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
    83
    public boolean run(List<Archive> archives) {
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    84
        // build a map from Location to Archive
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    85
        buildLocationArchiveMap(archives);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    86
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    87
        // traverse and analyze all dependencies
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    88
        for (Archive archive : archives) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    89
            ArchiveDeps deps = new ArchiveDeps(archive, type);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    90
            archive.visitDependences(deps);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    91
            results.put(archive, deps);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    92
        }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
    93
        return true;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    94
    }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    95
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
    96
    protected void buildLocationArchiveMap(List<Archive> archives) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
    97
        // build a map from Location to Archive
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    98
        for (Archive archive: archives) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    99
            for (Location l: archive.getClasses()) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   100
                if (!map.containsKey(l)) {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   101
                    map.put(l, archive);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   102
                } else {
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   103
                    // duplicated class warning?
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   104
                }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   105
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   106
        }
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   107
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   108
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   109
    public boolean hasDependences(Archive archive) {
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   110
        if (results.containsKey(archive)) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   111
            return results.get(archive).dependencies().size() > 0;
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   112
        }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   113
        return false;
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   114
    }
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   115
25692
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   116
    public Set<String> dependences(Archive source) {
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   117
        ArchiveDeps result = results.get(source);
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   118
        return result.dependencies().stream()
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   119
                     .map(Dep::target)
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   120
                     .collect(Collectors.toSet());
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   121
    }
39537fdca12c 8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents: 25442
diff changeset
   122
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   123
    public interface Visitor {
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   124
        /**
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   125
         * 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
   126
         * a fully-qualified classname, a package name, a module or
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   127
         * archive name depending on the Analyzer's type.
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   128
         */
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   129
        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
   130
                                    String target, Archive targetArchive);
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   131
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   132
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   133
    /**
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   134
     * 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
   135
     * 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
   136
     */
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   137
    public void visitDependences(Archive source, Visitor v, Type level) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   138
        if (level == Type.SUMMARY) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   139
            final ArchiveDeps result = results.get(source);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   140
            result.requires().stream()
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   141
                  .sorted(Comparator.comparing(Archive::getName))
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   142
                  .forEach(archive -> {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   143
                      Profile profile = result.getTargetProfile(archive);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   144
                      v.visitDependence(source.getName(), source,
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   145
                                        profile != null ? profile.profileName() : archive.getName(), archive);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   146
                  });
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   147
        } else {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   148
            ArchiveDeps result = results.get(source);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   149
            if (level != type) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   150
                // requesting different level of analysis
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   151
                result = new ArchiveDeps(source, level);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   152
                source.visitDependences(result);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   153
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   154
            result.dependencies().stream()
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   155
                  .sorted(Comparator.comparing(Dep::origin)
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   156
                                    .thenComparing(Dep::target))
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   157
                  .forEach(d -> v.visitDependence(d.origin(), d.originArchive(), d.target(), d.targetArchive()));
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   158
        }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   159
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   160
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   161
    public void visitDependences(Archive source, Visitor v) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   162
        visitDependences(source, v, type);
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   163
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   164
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   165
    /**
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   166
     * ArchiveDeps contains the dependencies for an Archive that can have one or
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   167
     * 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
   168
     */
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   169
    class ArchiveDeps implements Archive.Visitor {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   170
        protected final Archive archive;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   171
        protected final Set<Archive> requires;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   172
        protected final Set<Dep> deps;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   173
        protected final Type level;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   174
        private Profile profile;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   175
        ArchiveDeps(Archive archive, Type level) {
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   176
            this.archive = archive;
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   177
            this.deps = new HashSet<>();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   178
            this.requires = new HashSet<>();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   179
            this.level = level;
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   180
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   181
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   182
        Set<Dep> dependencies() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   183
            return deps;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   184
        }
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
        Set<Archive> requires() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   187
            return requires;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   188
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   189
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   190
        Profile getTargetProfile(Archive target) {
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   191
            if (target instanceof Module) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   192
                return Profile.getProfile((Module) target);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   193
            } else {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   194
                return null;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   195
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   196
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   197
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   198
        Archive findArchive(Location t) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   199
            Archive target = archive.getClasses().contains(t) ? archive : map.get(t);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   200
            if (target == null) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   201
                map.put(t, target = NOT_FOUND);
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   202
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   203
            return target;
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   204
        }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   205
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   206
        // return classname or package name depedning on the level
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   207
        private String getLocationName(Location o) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   208
            if (level == Type.CLASS || level == Type.VERBOSE) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   209
                return o.getClassName();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   210
            } else {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   211
                String pkg = o.getPackageName();
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   212
                return pkg.isEmpty() ? "<unnamed>" : pkg;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   213
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   214
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   215
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   216
        @Override
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   217
        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
   218
            Archive targetArchive = findArchive(t);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   219
            if (filter.accepts(o, archive, t, targetArchive)) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   220
                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
   221
                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
   222
                    requires.add(targetArchive);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   223
                }
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   224
            }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   225
            if (targetArchive instanceof Module) {
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   226
                Profile p = Profile.getProfile(t.getPackageName());
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   227
                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
   228
                    profile = p;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   229
                }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   230
            }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   231
        }
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   232
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   233
        private Dep curDep;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   234
        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
   235
            String origin = getLocationName(o);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   236
            String target = getLocationName(t);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   237
            Archive targetArchive = findArchive(t);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   238
            if (curDep != null &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   239
                    curDep.origin().equals(origin) &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   240
                    curDep.originArchive() == archive &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   241
                    curDep.target().equals(target) &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   242
                    curDep.targetArchive() == targetArchive) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   243
                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
   244
            }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   245
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   246
            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
   247
            if (deps.contains(e)) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   248
                for (Dep e1 : deps) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   249
                    if (e.equals(e1)) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   250
                        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
   251
                    }
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   252
                }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   253
            } else {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   254
                deps.add(e);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   255
                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
   256
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   257
            return curDep;
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   258
        }
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   259
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   260
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   261
    /*
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   262
     * Class-level or package-level dependency
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   263
     */
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   264
    class Dep {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   265
        final String origin;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   266
        final Archive originArchive;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   267
        final String target;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   268
        final Archive targetArchive;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   269
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   270
        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
   271
            this.origin = origin;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   272
            this.originArchive = originArchive;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   273
            this.target = target;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   274
            this.targetArchive = targetArchive;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   275
        }
25442
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
        String origin() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   278
            return origin;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   279
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   280
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   281
        Archive originArchive() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   282
            return originArchive;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   283
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   284
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   285
        String target() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   286
            return target;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   287
        }
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   288
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   289
        Archive targetArchive() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   290
            return targetArchive;
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
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   293
        @Override
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   294
        @SuppressWarnings("unchecked")
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   295
        public boolean equals(Object o) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   296
            if (o instanceof Dep) {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   297
                Dep d = (Dep) o;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   298
                return this.origin.equals(d.origin) &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   299
                        this.originArchive == d.originArchive &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   300
                        this.target.equals(d.target) &&
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   301
                        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
   302
            }
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   303
            return false;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   304
        }
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   305
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   306
        @Override
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   307
        public int hashCode() {
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   308
            int hash = 7;
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   309
            hash = 67*hash + Objects.hashCode(this.origin)
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   310
                           + Objects.hashCode(this.originArchive)
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   311
                           + Objects.hashCode(this.target)
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   312
                           + Objects.hashCode(this.targetArchive);
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 22163
diff changeset
   313
            return hash;
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   314
        }
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   315
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   316
        public String toString() {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   317
            return String.format("%s (%s) -> %s (%s)%n",
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   318
                    origin, originArchive.getName(),
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   319
                    target, targetArchive.getName());
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   320
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   321
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   322
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   323
    static Analyzer getExportedAPIsAnalyzer() {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   324
        return new ModuleAccessAnalyzer(ModuleAccessAnalyzer.reexportsFilter, true);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   325
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   326
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   327
    static Analyzer getModuleAccessAnalyzer() {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   328
        return new ModuleAccessAnalyzer(ModuleAccessAnalyzer.accessCheckFilter, false);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   329
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   330
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   331
    private static class ModuleAccessAnalyzer extends Analyzer {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   332
        private final boolean apionly;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   333
        ModuleAccessAnalyzer(Filter filter, boolean apionly) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   334
            super(Type.VERBOSE, filter);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   335
            this.apionly = apionly;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   336
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   337
        /**
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   338
         * Verify module access
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   339
         */
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   340
        public boolean run(List<Archive> archives) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   341
            // build a map from Location to Archive
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   342
            buildLocationArchiveMap(archives);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   343
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   344
            // traverse and analyze all dependencies
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   345
            int count = 0;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   346
            for (Archive archive : archives) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   347
                ArchiveDeps checker = new ArchiveDeps(archive, type);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   348
                archive.visitDependences(checker);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   349
                count += checker.dependencies().size();
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   350
                // output if any error
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   351
                Module m = (Module)archive;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   352
                printDependences(System.err, m, checker.dependencies());
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   353
                results.put(archive, checker);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   354
            }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   355
            return count == 0;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   356
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   357
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   358
        private void printDependences(PrintStream out, Module m, Set<Dep> deps) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   359
            if (deps.isEmpty())
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   360
                return;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   361
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   362
            String msg = apionly ? "API reference:" : "inaccessible reference:";
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   363
            deps.stream().sorted(Comparator.comparing(Dep::origin)
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   364
                                           .thenComparing(Dep::target))
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   365
                .forEach(d -> out.format("%s %s (%s) -> %s (%s)%n", msg,
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   366
                                         d.origin(), d.originArchive().getName(),
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   367
                                         d.target(), d.targetArchive().getName()));
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   368
            if (apionly) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   369
                out.format("Dependences missing re-exports=\"true\" attribute:%n");
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   370
                deps.stream()
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   371
                        .map(Dep::targetArchive)
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   372
                        .map(Archive::getName)
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   373
                        .distinct()
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   374
                        .sorted()
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   375
                        .forEach(d -> out.format("  %s -> %s%n", m.name(), d));
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   376
            }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   377
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   378
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   379
        private static Module findModule(Archive archive) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   380
            if (Module.class.isInstance(archive)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   381
                return (Module) archive;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   382
            } else {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   383
                return null;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   384
            }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   385
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   386
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   387
        // returns true if target is accessible by origin
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   388
        private static boolean canAccess(Location o, Archive originArchive, Location t, Archive targetArchive) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   389
            Module origin = findModule(originArchive);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   390
            Module target = findModule(targetArchive);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   391
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   392
            if (targetArchive == Analyzer.NOT_FOUND) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   393
                return false;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   394
            }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   395
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   396
            // unnamed module
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   397
            // ## should check public type?
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   398
            if (target == null)
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   399
                return true;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   400
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   401
            // module-private
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   402
            if (origin == target)
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   403
                return true;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   404
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   405
            return target.isAccessibleTo(t.getClassName(), origin);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   406
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   407
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   408
        static final Filter accessCheckFilter = new Filter() {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   409
            @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   410
            public boolean accepts(Location o, Archive originArchive, Location t, Archive targetArchive) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   411
                return !canAccess(o, originArchive, t, targetArchive);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   412
            }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   413
        };
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   414
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   415
        static final Filter reexportsFilter = new Filter() {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   416
            @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   417
            public boolean accepts(Location o, Archive originArchive, Location t, Archive targetArchive) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   418
                Module origin = findModule(originArchive);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   419
                Module target = findModule(targetArchive);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   420
                if (!origin.isExportedPackage(o.getPackageName())) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   421
                    // filter non-exported classes
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   422
                    return false;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   423
                }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   424
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   425
                boolean accessible = canAccess(o, originArchive, t, targetArchive);
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   426
                if (!accessible)
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   427
                    return true;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   428
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   429
                String mn = target.name();
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   430
                // skip checking re-exports for java.base
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   431
                if (origin == target || "java.base".equals(mn))
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   432
                    return false;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   433
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   434
                assert origin.requires().containsKey(mn);  // otherwise, should not be accessible
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   435
                if (origin.requires().get(mn)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   436
                    return false;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   437
                }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   438
                return true;
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   439
            }
83c19f00452c 8054834: Modular Source Code
chegar
parents: 25692
diff changeset
   440
        };
16290
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   441
    }
b0b4f52de7ea 8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
diff changeset
   442
}