langtools/src/jdk.dev/share/classes/com/sun/tools/jdeps/Module.java
author attila
Wed, 01 Oct 2014 10:26:25 +0200
changeset 26888 2722a9c501a5
parent 25874 83c19f00452c
child 27579 d1a63c99cdd5
permissions -rw-r--r--
8059370: Unnecessary work in deoptimizing recompilation Reviewed-by: jlaskey, lagergren
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
 */
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    25
package com.sun.tools.jdeps;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    26
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    27
import java.util.Collections;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    28
import java.util.HashMap;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    29
import java.util.HashSet;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    30
import java.util.Map;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    31
import java.util.Objects;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    32
import java.util.Set;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    33
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    34
/**
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    35
 * JDeps internal representation of module for dependency analysis.
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    36
 */
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    37
final class Module extends Archive {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    38
    private final String moduleName;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    39
    private final Map<String, Boolean> requires;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    40
    private final Map<String, Set<String>> exports;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    41
    private final Set<String> packages;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    42
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    43
    private Module(ClassFileReader reader, String name,
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    44
                   Map<String, Boolean> requires,
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    45
                   Map<String, Set<String>> exports,
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    46
                   Set<String> packages) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    47
        super(name, reader);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    48
        this.moduleName = name;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    49
        this.requires = Collections.unmodifiableMap(requires);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    50
        this.exports = Collections.unmodifiableMap(exports);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    51
        this.packages = Collections.unmodifiableSet(packages);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    52
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    53
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    54
    public String name() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    55
        return moduleName;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    56
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    57
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    58
    public Map<String, Boolean> requires() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    59
        return requires;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    60
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    61
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    62
    public Map<String, Set<String>> exports() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    63
        return exports;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    64
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    65
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    66
    public Set<String> packages() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    67
        return packages;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    68
    }
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
     * Tests if this module can read m
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    72
     */
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    73
    public boolean canRead(Module m) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    74
        // ## TODO: handle "re-exported=true"
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    75
        // all JDK modules require all modules containing its direct dependences
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    76
        // should not be an issue
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    77
        return requires.containsKey(m.name());
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    78
    }
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
     * Tests if a given fully-qualified name is an exported type.
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    82
     */
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    83
    public boolean isExported(String cn) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    84
        int i = cn.lastIndexOf('.');
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    85
        String pn = i > 0 ? cn.substring(0, i) : "";
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    86
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    87
        return isExportedPackage(pn);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    88
    }
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
     * Tests if a given package name is exported.
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    92
     */
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    93
    public boolean isExportedPackage(String pn) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    94
        return exports.containsKey(pn) ? exports.get(pn).isEmpty() : false;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    95
    }
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
     * Tests if the given classname is accessible to module m
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
    99
     */
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   100
    public boolean isAccessibleTo(String classname, Module m) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   101
        int i = classname.lastIndexOf('.');
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   102
        String pn = i > 0 ? classname.substring(0, i) : "";
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   103
        if (!packages.contains(pn)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   104
            throw new IllegalArgumentException(classname + " is not a member of module " + name());
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   105
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   106
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   107
        if (m != null && !m.canRead(this)) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   108
            trace("%s not readable by %s%n", this.name(), m.name());
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   109
            return false;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   110
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   111
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   112
        // exported API
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   113
        Set<String> ms = exports().get(pn);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   114
        String mname = m != null ? m.name() : "unnamed";
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   115
        if (ms == null) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   116
            trace("%s not exported in %s%n", classname, this.name());
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   117
        } else if (!(ms.isEmpty() || ms.contains(mname))) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   118
            trace("%s not permit to %s %s%n", classname, mname, ms);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   119
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   120
        return ms != null && (ms.isEmpty() || ms.contains(mname));
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   121
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   122
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   123
    private static final boolean traceOn = Boolean.getBoolean("jdeps.debug");
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   124
    private void trace(String fmt, Object... args) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   125
        if (traceOn) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   126
            System.err.format(fmt, args);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   127
        }
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
    @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   131
    public boolean equals(Object ob) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   132
        if (!(ob instanceof Module))
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   133
            return false;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   134
        Module that = (Module)ob;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   135
        return (moduleName.equals(that.moduleName)
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   136
                && requires.equals(that.requires)
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   137
                && exports.equals(that.exports)
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   138
                && packages.equals(that.packages));
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   139
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   140
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   141
    @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   142
    public int hashCode() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   143
        int hc = moduleName.hashCode();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   144
        hc = hc * 43 + requires.hashCode();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   145
        hc = hc * 43 + exports.hashCode();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   146
        hc = hc * 43 + packages.hashCode();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   147
        return hc;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   148
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   149
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   150
    @Override
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   151
    public String toString() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   152
        return name();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   153
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   154
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   155
    public final static class Builder {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   156
        String name;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   157
        ClassFileReader reader;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   158
        final Map<String, Boolean> requires = new HashMap<>();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   159
        final Map<String, Set<String>> exports = new HashMap<>();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   160
        final Set<String> packages = new HashSet<>();
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   161
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   162
        public Builder() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   163
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   164
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   165
        public Builder name(String n) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   166
            name = n;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   167
            return this;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   168
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   169
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   170
        public Builder require(String d, boolean reexport) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   171
         //   System.err.format("%s depend %s reexports %s%n", name, d, reexport);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   172
            requires.put(d, reexport);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   173
            return this;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   174
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   175
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   176
        public Builder include(String p) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   177
            packages.add(p);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   178
            return this;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   179
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   180
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   181
        public Builder export(String p, Set<String> ms) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   182
            Objects.requireNonNull(p);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   183
            Objects.requireNonNull(ms);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   184
            exports.put(p, new HashSet<>(ms));
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   185
            return this;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   186
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   187
        public Builder classes(ClassFileReader reader) {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   188
            this.reader = reader;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   189
            return this;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   190
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   191
        public Module build() {
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   192
            Module m = new Module(reader, name, requires, exports, packages);
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   193
            return m;
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   194
        }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   195
    }
83c19f00452c 8054834: Modular Source Code
chegar
parents:
diff changeset
   196
}