src/share/classes/com/sun/tools/jdeps/Analyzer.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25 package com.sun.tools.jdeps;
aoqi@0 26
aoqi@0 27 import com.sun.tools.classfile.Dependency.Location;
aoqi@0 28 import com.sun.tools.jdeps.PlatformClassPath.JDKArchive;
aoqi@0 29 import java.util.ArrayList;
aoqi@0 30 import java.util.HashMap;
aoqi@0 31 import java.util.List;
aoqi@0 32 import java.util.Map;
aoqi@0 33 import java.util.Objects;
aoqi@0 34 import java.util.Set;
aoqi@0 35 import java.util.SortedMap;
aoqi@0 36 import java.util.SortedSet;
aoqi@0 37 import java.util.TreeMap;
aoqi@0 38 import java.util.TreeSet;
aoqi@0 39
aoqi@0 40 /**
aoqi@0 41 * Dependency Analyzer.
aoqi@0 42 */
aoqi@0 43 public class Analyzer {
aoqi@0 44 /**
aoqi@0 45 * Type of the dependency analysis. Appropriate level of data
aoqi@0 46 * will be stored.
aoqi@0 47 */
aoqi@0 48 public enum Type {
aoqi@0 49 SUMMARY,
aoqi@0 50 PACKAGE,
aoqi@0 51 CLASS,
aoqi@0 52 VERBOSE
aoqi@0 53 };
aoqi@0 54
aoqi@0 55 private final Type type;
aoqi@0 56 private final Map<Archive, ArchiveDeps> results = new HashMap<>();
aoqi@0 57 private final Map<Location, Archive> map = new HashMap<>();
aoqi@0 58 private final Archive NOT_FOUND
aoqi@0 59 = new Archive(JdepsTask.getMessage("artifact.not.found"));
aoqi@0 60
aoqi@0 61 /**
aoqi@0 62 * Constructs an Analyzer instance.
aoqi@0 63 *
aoqi@0 64 * @param type Type of the dependency analysis
aoqi@0 65 */
aoqi@0 66 public Analyzer(Type type) {
aoqi@0 67 this.type = type;
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 /**
aoqi@0 71 * Performs the dependency analysis on the given archives.
aoqi@0 72 */
aoqi@0 73 public void run(List<Archive> archives) {
aoqi@0 74 // build a map from Location to Archive
aoqi@0 75 for (Archive archive: archives) {
aoqi@0 76 for (Location l: archive.getClasses()) {
aoqi@0 77 if (!map.containsKey(l)) {
aoqi@0 78 map.put(l, archive);
aoqi@0 79 } else {
aoqi@0 80 // duplicated class warning?
aoqi@0 81 }
aoqi@0 82 }
aoqi@0 83 }
aoqi@0 84 // traverse and analyze all dependencies
aoqi@0 85 for (Archive archive : archives) {
aoqi@0 86 ArchiveDeps deps;
aoqi@0 87 if (type == Type.CLASS || type == Type.VERBOSE) {
aoqi@0 88 deps = new ClassVisitor(archive);
aoqi@0 89 } else {
aoqi@0 90 deps = new PackageVisitor(archive);
aoqi@0 91 }
aoqi@0 92 archive.visitDependences(deps);
aoqi@0 93 results.put(archive, deps);
aoqi@0 94 }
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 public boolean hasDependences(Archive archive) {
aoqi@0 98 if (results.containsKey(archive)) {
aoqi@0 99 return results.get(archive).deps.size() > 0;
aoqi@0 100 }
aoqi@0 101 return false;
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 public interface Visitor {
aoqi@0 105 /**
aoqi@0 106 * Visits the source archive to its destination archive of
aoqi@0 107 * a recorded dependency.
aoqi@0 108 */
aoqi@0 109 void visitArchiveDependence(Archive origin, Archive target, Profile profile);
aoqi@0 110 /**
aoqi@0 111 * Visits a recorded dependency from origin to target which can be
aoqi@0 112 * a fully-qualified classname, a package name, a profile or
aoqi@0 113 * archive name depending on the Analyzer's type.
aoqi@0 114 */
aoqi@0 115 void visitDependence(String origin, Archive source, String target, Archive archive, Profile profile);
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 public void visitArchiveDependences(Archive source, Visitor v) {
aoqi@0 119 ArchiveDeps r = results.get(source);
aoqi@0 120 for (ArchiveDeps.Dep d: r.requireArchives()) {
aoqi@0 121 v.visitArchiveDependence(r.archive, d.archive, d.profile);
aoqi@0 122 }
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 public void visitDependences(Archive source, Visitor v) {
aoqi@0 126 ArchiveDeps r = results.get(source);
aoqi@0 127 for (Map.Entry<String, SortedSet<ArchiveDeps.Dep>> e: r.deps.entrySet()) {
aoqi@0 128 String origin = e.getKey();
aoqi@0 129 for (ArchiveDeps.Dep d: e.getValue()) {
aoqi@0 130 // filter intra-dependency unless in verbose mode
aoqi@0 131 if (type == Type.VERBOSE || d.archive != source) {
aoqi@0 132 v.visitDependence(origin, source, d.target, d.archive, d.profile);
aoqi@0 133 }
aoqi@0 134 }
aoqi@0 135 }
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 /**
aoqi@0 139 * ArchiveDeps contains the dependencies for an Archive that
aoqi@0 140 * can have one or more classes.
aoqi@0 141 */
aoqi@0 142 private abstract class ArchiveDeps implements Archive.Visitor {
aoqi@0 143 final Archive archive;
aoqi@0 144 final SortedMap<String, SortedSet<Dep>> deps;
aoqi@0 145 ArchiveDeps(Archive archive) {
aoqi@0 146 this.archive = archive;
aoqi@0 147 this.deps = new TreeMap<>();
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 void add(String origin, String target, Archive targetArchive, String pkgName) {
aoqi@0 151 SortedSet<Dep> set = deps.get(origin);
aoqi@0 152 if (set == null) {
aoqi@0 153 deps.put(origin, set = new TreeSet<>());
aoqi@0 154 }
aoqi@0 155 Profile p = targetArchive instanceof JDKArchive
aoqi@0 156 ? Profile.getProfile(pkgName) : null;
aoqi@0 157 set.add(new Dep(target, targetArchive, p));
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 /**
aoqi@0 161 * Returns the list of Archive dependences. The returned
aoqi@0 162 * list contains one {@code Dep} instance per one archive
aoqi@0 163 * and with the minimum profile this archive depends on.
aoqi@0 164 */
aoqi@0 165 List<Dep> requireArchives() {
aoqi@0 166 Map<Archive,Profile> map = new HashMap<>();
aoqi@0 167 for (Set<Dep> set: deps.values()) {
aoqi@0 168 for (Dep d: set) {
aoqi@0 169 if (this.archive != d.archive) {
aoqi@0 170 Profile p = map.get(d.archive);
aoqi@0 171 if (p == null || (d.profile != null && p.profile < d.profile.profile)) {
aoqi@0 172 map.put(d.archive, d.profile);
aoqi@0 173 }
aoqi@0 174 }
aoqi@0 175 }
aoqi@0 176 }
aoqi@0 177 List<Dep> list = new ArrayList<>();
aoqi@0 178 for (Map.Entry<Archive,Profile> e: map.entrySet()) {
aoqi@0 179 list.add(new Dep("", e.getKey(), e.getValue()));
aoqi@0 180 }
aoqi@0 181 return list;
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 /**
aoqi@0 185 * Dep represents a dependence where the target can be
aoqi@0 186 * a classname or packagename and the archive and profile
aoqi@0 187 * the target belongs to.
aoqi@0 188 */
aoqi@0 189 class Dep implements Comparable<Dep> {
aoqi@0 190 final String target;
aoqi@0 191 final Archive archive;
aoqi@0 192 final Profile profile;
aoqi@0 193 Dep(String target, Archive archive, Profile p) {
aoqi@0 194 this.target = target;
aoqi@0 195 this.archive = archive;
aoqi@0 196 this.profile = p;
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 @Override
aoqi@0 200 public boolean equals(Object o) {
aoqi@0 201 if (o instanceof Dep) {
aoqi@0 202 Dep d = (Dep)o;
aoqi@0 203 return this.archive == d.archive && this.target.equals(d.target);
aoqi@0 204 }
aoqi@0 205 return false;
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 @Override
aoqi@0 209 public int hashCode() {
aoqi@0 210 int hash = 3;
aoqi@0 211 hash = 17 * hash + Objects.hashCode(this.archive);
aoqi@0 212 hash = 17 * hash + Objects.hashCode(this.target);
aoqi@0 213 return hash;
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 @Override
aoqi@0 217 public int compareTo(Dep o) {
aoqi@0 218 if (this.target.equals(o.target)) {
aoqi@0 219 if (this.archive == o.archive) {
aoqi@0 220 return 0;
aoqi@0 221 } else {
aoqi@0 222 return this.archive.getFileName().compareTo(o.archive.getFileName());
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225 return this.target.compareTo(o.target);
aoqi@0 226 }
aoqi@0 227 }
aoqi@0 228 public abstract void visit(Location o, Location t);
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 private class ClassVisitor extends ArchiveDeps {
aoqi@0 232 ClassVisitor(Archive archive) {
aoqi@0 233 super(archive);
aoqi@0 234 }
aoqi@0 235 @Override
aoqi@0 236 public void visit(Location o, Location t) {
aoqi@0 237 Archive targetArchive =
aoqi@0 238 this.archive.getClasses().contains(t) ? this.archive : map.get(t);
aoqi@0 239 if (targetArchive == null) {
aoqi@0 240 map.put(t, targetArchive = NOT_FOUND);
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 String origin = o.getClassName();
aoqi@0 244 String target = t.getClassName();
aoqi@0 245 add(origin, target, targetArchive, t.getPackageName());
aoqi@0 246 }
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 private class PackageVisitor extends ArchiveDeps {
aoqi@0 250 PackageVisitor(Archive archive) {
aoqi@0 251 super(archive);
aoqi@0 252 }
aoqi@0 253 @Override
aoqi@0 254 public void visit(Location o, Location t) {
aoqi@0 255 Archive targetArchive =
aoqi@0 256 this.archive.getClasses().contains(t) ? this.archive : map.get(t);
aoqi@0 257 if (targetArchive == null) {
aoqi@0 258 map.put(t, targetArchive = NOT_FOUND);
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 String origin = packageOf(o);
aoqi@0 262 String target = packageOf(t);
aoqi@0 263 add(origin, target, targetArchive, t.getPackageName());
aoqi@0 264 }
aoqi@0 265 public String packageOf(Location o) {
aoqi@0 266 String pkg = o.getPackageName();
aoqi@0 267 return pkg.isEmpty() ? "<unnamed>" : pkg;
aoqi@0 268 }
aoqi@0 269 }
aoqi@0 270 }

mercurial