langtools/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/Module.java
author vasya
Mon, 14 Dec 2015 20:18:19 +0100
changeset 34752 9c262a013456
parent 30846 2b3f379840f0
child 36526 3b41f1c69604
permissions -rw-r--r--
8145342: Some copyright notices are inconsistently and ill formatted Summary: Adjusted formatting of copyright notices. Reviewed-by: darcy, alundblad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
     1
/*
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
     4
 *
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    10
 *
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    15
 * accompanied this code).
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    16
 *
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    20
 *
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    23
 * questions.
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    24
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 30846
diff changeset
    25
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    26
package com.sun.tools.jdeps;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    27
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    28
import java.util.Collections;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    29
import java.util.HashMap;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    30
import java.util.HashSet;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    31
import java.util.Map;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    32
import java.util.Objects;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    33
import java.util.Set;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    34
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    35
/**
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    36
 * JDeps internal representation of module for dependency analysis.
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    37
 */
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    38
final class Module extends Archive {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    39
    private final String moduleName;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    40
    private final Map<String, Boolean> requires;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    41
    private final Map<String, Set<String>> exports;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    42
    private final Set<String> packages;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    43
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    44
    private Module(ClassFileReader reader, String name,
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    45
                   Map<String, Boolean> requires,
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    46
                   Map<String, Set<String>> exports,
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    47
                   Set<String> packages) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    48
        super(name, reader);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    49
        this.moduleName = name;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    50
        this.requires = Collections.unmodifiableMap(requires);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    51
        this.exports = Collections.unmodifiableMap(exports);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    52
        this.packages = Collections.unmodifiableSet(packages);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    53
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    54
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    55
    public String name() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    56
        return moduleName;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    57
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    58
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    59
    public Map<String, Boolean> requires() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    60
        return requires;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    61
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    62
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    63
    public Map<String, Set<String>> exports() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    64
        return exports;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    65
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    66
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    67
    public Set<String> packages() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    68
        return packages;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    69
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    70
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    71
    /**
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    72
     * Tests if this module can read m
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    73
     */
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    74
    public boolean canRead(Module m) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    75
        // ## TODO: handle "re-exported=true"
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    76
        // all JDK modules require all modules containing its direct dependences
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    77
        // should not be an issue
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    78
        return requires.containsKey(m.name());
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    79
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    80
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    81
    /**
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    82
     * Tests if a given fully-qualified name is an exported type.
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    83
     */
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    84
    public boolean isExported(String cn) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    85
        int i = cn.lastIndexOf('.');
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    86
        String pn = i > 0 ? cn.substring(0, i) : "";
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    87
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    88
        return isExportedPackage(pn);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    89
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    90
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    91
    /**
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    92
     * Tests if a given package name is exported.
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    93
     */
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    94
    public boolean isExportedPackage(String pn) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    95
        return exports.containsKey(pn) ? exports.get(pn).isEmpty() : false;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    96
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    97
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    98
    /**
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    99
     * Tests if the given classname is accessible to module m
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   100
     */
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   101
    public boolean isAccessibleTo(String classname, Module m) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   102
        int i = classname.lastIndexOf('.');
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   103
        String pn = i > 0 ? classname.substring(0, i) : "";
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   104
        if (!packages.contains(pn)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   105
            throw new IllegalArgumentException(classname + " is not a member of module " + name());
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   106
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   107
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   108
        if (m != null && !m.canRead(this)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   109
            trace("%s not readable by %s%n", this.name(), m.name());
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   110
            return false;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   111
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   112
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   113
        // exported API
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   114
        Set<String> ms = exports().get(pn);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   115
        String mname = m != null ? m.name() : "unnamed";
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   116
        if (ms == null) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   117
            trace("%s not exported in %s%n", classname, this.name());
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   118
        } else if (!(ms.isEmpty() || ms.contains(mname))) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   119
            trace("%s not permit to %s %s%n", classname, mname, ms);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   120
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   121
        return ms != null && (ms.isEmpty() || ms.contains(mname));
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   122
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   123
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   124
    private static final boolean traceOn = Boolean.getBoolean("jdeps.debug");
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   125
    private void trace(String fmt, Object... args) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   126
        if (traceOn) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   127
            System.err.format(fmt, args);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   128
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   129
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   130
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   131
    @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   132
    public boolean equals(Object ob) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   133
        if (!(ob instanceof Module))
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   134
            return false;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   135
        Module that = (Module)ob;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   136
        return (moduleName.equals(that.moduleName)
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   137
                && requires.equals(that.requires)
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   138
                && exports.equals(that.exports)
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   139
                && packages.equals(that.packages));
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   140
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   141
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   142
    @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   143
    public int hashCode() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   144
        int hc = moduleName.hashCode();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   145
        hc = hc * 43 + requires.hashCode();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   146
        hc = hc * 43 + exports.hashCode();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   147
        hc = hc * 43 + packages.hashCode();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   148
        return hc;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   149
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   150
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   151
    @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   152
    public String toString() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   153
        return name();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   154
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   155
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   156
    public final static class Builder {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   157
        String name;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   158
        ClassFileReader reader;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   159
        final Map<String, Boolean> requires = new HashMap<>();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   160
        final Map<String, Set<String>> exports = new HashMap<>();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   161
        final Set<String> packages = new HashSet<>();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   162
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   163
        public Builder() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   164
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   165
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   166
        public Builder name(String n) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   167
            name = n;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   168
            return this;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   169
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   170
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   171
        public Builder require(String d, boolean reexport) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   172
         //   System.err.format("%s depend %s reexports %s%n", name, d, reexport);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   173
            requires.put(d, reexport);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   174
            return this;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   175
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   176
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   177
        public Builder packages(Set<String> pkgs) {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   178
            packages.addAll(pkgs);
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   179
            return this;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   180
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   181
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   182
        public Builder export(String p, Set<String> ms) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   183
            Objects.requireNonNull(p);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   184
            Objects.requireNonNull(ms);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   185
            exports.put(p, new HashSet<>(ms));
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   186
            return this;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   187
        }
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   188
        public Builder classes(ClassFileReader.ModuleClassReader reader) {
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   189
            this.reader = reader;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   190
            return this;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   191
        }
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 25874
diff changeset
   192
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   193
        public Module build() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   194
            Module m = new Module(reader, name, requires, exports, packages);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   195
            return m;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   196
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   197
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   198
}