Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/Analyzer.java
41161 views
/*1* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.jdeps;2627import com.sun.tools.classfile.Dependency.Location;2829import java.io.BufferedReader;30import java.io.IOException;31import java.io.InputStream;32import java.io.InputStreamReader;33import java.io.UncheckedIOException;34import java.lang.module.ModuleDescriptor;35import java.util.Collections;36import java.util.Comparator;37import java.util.HashMap;38import java.util.HashSet;39import java.util.Map;40import java.util.Objects;41import java.util.Set;42import java.util.function.Predicate;43import java.util.stream.Collectors;44import java.util.stream.Stream;4546/**47* Dependency Analyzer.48*/49public class Analyzer {50/**51* Type of the dependency analysis. Appropriate level of data52* will be stored.53*/54public enum Type {55SUMMARY,56MODULE, // equivalent to summary in addition, print module descriptor57PACKAGE,58CLASS,59VERBOSE60}6162/**63* Filter to be applied when analyzing the dependencies from the given archives.64* Only the accepted dependencies are recorded.65*/66interface Filter {67boolean accepts(Location origin, Archive originArchive,68Location target, Archive targetArchive);69}7071protected final JdepsConfiguration configuration;72protected final Type type;73protected final Filter filter;74protected final Map<Archive, Dependences> results = new HashMap<>();75protected final Map<Location, Archive> locationToArchive = new HashMap<>();76static final Archive NOT_FOUND77= new Archive(JdepsTask.getMessage("artifact.not.found"));78static final Predicate<Archive> ANY = a -> true;7980/**81* Constructs an Analyzer instance.82*83* @param type Type of the dependency analysis84* @param filter85*/86Analyzer(JdepsConfiguration config, Type type, Filter filter) {87this.configuration = config;88this.type = type;89this.filter = filter;90}9192/**93* Performs the dependency analysis on the given archives.94*/95boolean run(Iterable<? extends Archive> archives,96Map<Location, Archive> locationMap)97{98this.locationToArchive.putAll(locationMap);99100// traverse and analyze all dependencies101for (Archive archive : archives) {102Dependences deps = new Dependences(archive, type);103archive.visitDependences(deps);104results.put(archive, deps);105}106return true;107}108109/**110* Returns the analyzed archives111*/112Set<Archive> archives() {113return results.keySet();114}115116/**117* Returns true if the given archive has dependences.118*/119boolean hasDependences(Archive archive) {120if (results.containsKey(archive)) {121return results.get(archive).dependencies().size() > 0;122}123return false;124}125126/**127* Returns the dependences, either class name or package name128* as specified in the given verbose level, from the given source.129*/130Set<String> dependences(Archive source) {131if (!results.containsKey(source)) {132return Collections.emptySet();133}134135return results.get(source).dependencies()136.stream()137.map(Dep::target)138.collect(Collectors.toSet());139}140141/**142* Returns the direct dependences of the given source143*/144Stream<Archive> requires(Archive source) {145if (!results.containsKey(source)) {146return Stream.empty();147}148return results.get(source).requires()149.stream();150}151152interface Visitor {153/**154* Visits a recorded dependency from origin to target which can be155* a fully-qualified classname, a package name, a module or156* archive name depending on the Analyzer's type.157*/158public void visitDependence(String origin, Archive originArchive,159String target, Archive targetArchive);160}161162/**163* Visit the dependencies of the given source.164* If the requested level is SUMMARY, it will visit the required archives list.165*/166void visitDependences(Archive source, Visitor v, Type level, Predicate<Archive> targetFilter) {167if (level == Type.SUMMARY) {168final Dependences result = results.get(source);169final Set<Archive> reqs = result.requires();170Stream<Archive> stream = reqs.stream();171if (reqs.isEmpty()) {172if (hasDependences(source)) {173// If reqs.isEmpty() and we have dependences, then it means174// that the dependences are from 'source' onto itself.175stream = Stream.of(source);176}177}178stream.sorted(Comparator.comparing(Archive::getName))179.forEach(archive -> {180Profile profile = result.getTargetProfile(archive);181v.visitDependence(source.getName(), source,182profile != null ? profile.profileName()183: archive.getName(), archive);184});185} else {186Dependences result = results.get(source);187if (level != type) {188// requesting different level of analysis189result = new Dependences(source, level, targetFilter);190source.visitDependences(result);191}192result.dependencies().stream()193.sorted(Comparator.comparing(Dep::origin)194.thenComparing(Dep::target))195.forEach(d -> v.visitDependence(d.origin(), d.originArchive(),196d.target(), d.targetArchive()));197}198}199200void visitDependences(Archive source, Visitor v) {201visitDependences(source, v, type, ANY);202}203204void visitDependences(Archive source, Visitor v, Type level) {205visitDependences(source, v, level, ANY);206}207208/**209* Dependences contains the dependencies for an Archive that can have one or210* more classes.211*/212class Dependences implements Archive.Visitor {213protected final Archive archive;214protected final Set<Archive> requires;215protected final Set<Dep> deps;216protected final Type level;217protected final Predicate<Archive> targetFilter;218private Profile profile;219Dependences(Archive archive, Type level) {220this(archive, level, ANY);221}222Dependences(Archive archive, Type level, Predicate<Archive> targetFilter) {223this.archive = archive;224this.deps = new HashSet<>();225this.requires = new HashSet<>();226this.level = level;227this.targetFilter = targetFilter;228}229230Set<Dep> dependencies() {231return deps;232}233234Set<Archive> requires() {235return requires;236}237238Profile getTargetProfile(Archive target) {239if (target.getModule().isJDK()) {240return Profile.getProfile((Module) target);241} else {242return null;243}244}245246/*247* Returns the archive that contains the given location.248*/249Archive findArchive(Location t) {250// local in this archive251if (archive.getClasses().contains(t))252return archive;253254Archive target;255if (locationToArchive.containsKey(t)) {256target = locationToArchive.get(t);257} else {258// special case JDK removed API259target = configuration.findClass(t)260.orElseGet(() -> REMOVED_JDK_INTERNALS.contains(t)261? REMOVED_JDK_INTERNALS262: NOT_FOUND);263}264return locationToArchive.computeIfAbsent(t, _k -> target);265}266267// return classname or package name depending on the level268private String getLocationName(Location o) {269if (level == Type.CLASS || level == Type.VERBOSE) {270return VersionHelper.get(o.getClassName());271} else {272String pkg = o.getPackageName();273return pkg.isEmpty() ? "<unnamed>" : pkg;274}275}276277@Override278public void visit(Location o, Location t) {279Archive targetArchive = findArchive(t);280if (filter.accepts(o, archive, t, targetArchive) && targetFilter.test(targetArchive)) {281addDep(o, t);282if (archive != targetArchive && !requires.contains(targetArchive)) {283requires.add(targetArchive);284}285}286if (targetArchive.getModule().isNamed()) {287Profile p = Profile.getProfile(t.getPackageName());288if (profile == null || (p != null && p.compareTo(profile) > 0)) {289profile = p;290}291}292}293294private Dep curDep;295protected Dep addDep(Location o, Location t) {296String origin = getLocationName(o);297String target = getLocationName(t);298Archive targetArchive = findArchive(t);299if (curDep != null &&300curDep.origin().equals(origin) &&301curDep.originArchive() == archive &&302curDep.target().equals(target) &&303curDep.targetArchive() == targetArchive) {304return curDep;305}306307Dep e = new Dep(origin, archive, target, targetArchive);308if (deps.contains(e)) {309for (Dep e1 : deps) {310if (e.equals(e1)) {311curDep = e1;312}313}314} else {315deps.add(e);316curDep = e;317}318return curDep;319}320}321322/*323* Class-level or package-level dependency324*/325class Dep {326final String origin;327final Archive originArchive;328final String target;329final Archive targetArchive;330331Dep(String origin, Archive originArchive, String target, Archive targetArchive) {332this.origin = origin;333this.originArchive = originArchive;334this.target = target;335this.targetArchive = targetArchive;336}337338String origin() {339return origin;340}341342Archive originArchive() {343return originArchive;344}345346String target() {347return target;348}349350Archive targetArchive() {351return targetArchive;352}353354@Override355@SuppressWarnings("unchecked")356public boolean equals(Object o) {357if (o instanceof Dep) {358Dep d = (Dep) o;359return this.origin.equals(d.origin) &&360this.originArchive == d.originArchive &&361this.target.equals(d.target) &&362this.targetArchive == d.targetArchive;363}364return false;365}366367@Override368public int hashCode() {369return Objects.hash(this.origin,370this.originArchive,371this.target,372this.targetArchive);373}374375public String toString() {376return String.format("%s (%s) -> %s (%s)%n",377origin, originArchive.getName(),378target, targetArchive.getName());379}380}381382/*383* Returns true if the given archive represents not found.384*/385static boolean notFound(Archive archive) {386return archive == NOT_FOUND || archive == REMOVED_JDK_INTERNALS;387}388389static final Jdk8Internals REMOVED_JDK_INTERNALS = new Jdk8Internals();390391static class Jdk8Internals extends Module {392private static final String NAME = "JDK removed internal API";393private static final String JDK8_INTERNALS = "/com/sun/tools/jdeps/resources/jdk8_internals.txt";394private final Set<String> jdk8Internals;395private Jdk8Internals() {396super(NAME, ModuleDescriptor.newModule("jdk8internals").build(), true);397try (InputStream in = JdepsTask.class.getResourceAsStream(JDK8_INTERNALS);398BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {399this.jdk8Internals = reader.lines()400.filter(ln -> !ln.startsWith("#"))401.collect(Collectors.toSet());402} catch (IOException e) {403throw new UncheckedIOException(e);404}405}406407/*408* Ignore the module name which should not be shown in the output409*/410@Override411public String name() {412return getName();413}414415public boolean contains(Location location) {416String cn = location.getClassName();417int i = cn.lastIndexOf('.');418String pn = i > 0 ? cn.substring(0, i) : "";419420return jdk8Internals.contains(pn);421}422423@Override424public boolean isJDK() {425return true;426}427428@Override429public boolean isExported(String pn) {430return false;431}432}433}434435436