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