package com.sun.tools.jdeps;
import com.sun.tools.classfile.Dependency.Location;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.lang.module.ModuleDescriptor;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Analyzer {
public enum Type {
SUMMARY,
MODULE,
PACKAGE,
CLASS,
VERBOSE
}
interface Filter {
boolean accepts(Location origin, Archive originArchive,
Location target, Archive targetArchive);
}
protected final JdepsConfiguration configuration;
protected final Type type;
protected final Filter filter;
protected final Map<Archive, Dependences> results = new HashMap<>();
protected final Map<Location, Archive> locationToArchive = new HashMap<>();
static final Archive NOT_FOUND
= new Archive(JdepsTask.getMessage("artifact.not.found"));
static final Predicate<Archive> ANY = a -> true;
Analyzer(JdepsConfiguration config, Type type, Filter filter) {
this.configuration = config;
this.type = type;
this.filter = filter;
}
boolean run(Iterable<? extends Archive> archives,
Map<Location, Archive> locationMap)
{
this.locationToArchive.putAll(locationMap);
for (Archive archive : archives) {
Dependences deps = new Dependences(archive, type);
archive.visitDependences(deps);
results.put(archive, deps);
}
return true;
}
Set<Archive> archives() {
return results.keySet();
}
boolean hasDependences(Archive archive) {
if (results.containsKey(archive)) {
return results.get(archive).dependencies().size() > 0;
}
return false;
}
Set<String> dependences(Archive source) {
if (!results.containsKey(source)) {
return Collections.emptySet();
}
return results.get(source).dependencies()
.stream()
.map(Dep::target)
.collect(Collectors.toSet());
}
Stream<Archive> requires(Archive source) {
if (!results.containsKey(source)) {
return Stream.empty();
}
return results.get(source).requires()
.stream();
}
interface Visitor {
public void visitDependence(String origin, Archive originArchive,
String target, Archive targetArchive);
}
void visitDependences(Archive source, Visitor v, Type level, Predicate<Archive> targetFilter) {
if (level == Type.SUMMARY) {
final Dependences result = results.get(source);
final Set<Archive> reqs = result.requires();
Stream<Archive> stream = reqs.stream();
if (reqs.isEmpty()) {
if (hasDependences(source)) {
stream = Stream.of(source);
}
}
stream.sorted(Comparator.comparing(Archive::getName))
.forEach(archive -> {
Profile profile = result.getTargetProfile(archive);
v.visitDependence(source.getName(), source,
profile != null ? profile.profileName()
: archive.getName(), archive);
});
} else {
Dependences result = results.get(source);
if (level != type) {
result = new Dependences(source, level, targetFilter);
source.visitDependences(result);
}
result.dependencies().stream()
.sorted(Comparator.comparing(Dep::origin)
.thenComparing(Dep::target))
.forEach(d -> v.visitDependence(d.origin(), d.originArchive(),
d.target(), d.targetArchive()));
}
}
void visitDependences(Archive source, Visitor v) {
visitDependences(source, v, type, ANY);
}
void visitDependences(Archive source, Visitor v, Type level) {
visitDependences(source, v, level, ANY);
}
class Dependences implements Archive.Visitor {
protected final Archive archive;
protected final Set<Archive> requires;
protected final Set<Dep> deps;
protected final Type level;
protected final Predicate<Archive> targetFilter;
private Profile profile;
Dependences(Archive archive, Type level) {
this(archive, level, ANY);
}
Dependences(Archive archive, Type level, Predicate<Archive> targetFilter) {
this.archive = archive;
this.deps = new HashSet<>();
this.requires = new HashSet<>();
this.level = level;
this.targetFilter = targetFilter;
}
Set<Dep> dependencies() {
return deps;
}
Set<Archive> requires() {
return requires;
}
Profile getTargetProfile(Archive target) {
if (target.getModule().isJDK()) {
return Profile.getProfile((Module) target);
} else {
return null;
}
}
Archive findArchive(Location t) {
if (archive.getClasses().contains(t))
return archive;
Archive target;
if (locationToArchive.containsKey(t)) {
target = locationToArchive.get(t);
} else {
target = configuration.findClass(t)
.orElseGet(() -> REMOVED_JDK_INTERNALS.contains(t)
? REMOVED_JDK_INTERNALS
: NOT_FOUND);
}
return locationToArchive.computeIfAbsent(t, _k -> target);
}
private String getLocationName(Location o) {
if (level == Type.CLASS || level == Type.VERBOSE) {
return VersionHelper.get(o.getClassName());
} else {
String pkg = o.getPackageName();
return pkg.isEmpty() ? "<unnamed>" : pkg;
}
}
@Override
public void visit(Location o, Location t) {
Archive targetArchive = findArchive(t);
if (filter.accepts(o, archive, t, targetArchive) && targetFilter.test(targetArchive)) {
addDep(o, t);
if (archive != targetArchive && !requires.contains(targetArchive)) {
requires.add(targetArchive);
}
}
if (targetArchive.getModule().isNamed()) {
Profile p = Profile.getProfile(t.getPackageName());
if (profile == null || (p != null && p.compareTo(profile) > 0)) {
profile = p;
}
}
}
private Dep curDep;
protected Dep addDep(Location o, Location t) {
String origin = getLocationName(o);
String target = getLocationName(t);
Archive targetArchive = findArchive(t);
if (curDep != null &&
curDep.origin().equals(origin) &&
curDep.originArchive() == archive &&
curDep.target().equals(target) &&
curDep.targetArchive() == targetArchive) {
return curDep;
}
Dep e = new Dep(origin, archive, target, targetArchive);
if (deps.contains(e)) {
for (Dep e1 : deps) {
if (e.equals(e1)) {
curDep = e1;
}
}
} else {
deps.add(e);
curDep = e;
}
return curDep;
}
}
class Dep {
final String origin;
final Archive originArchive;
final String target;
final Archive targetArchive;
Dep(String origin, Archive originArchive, String target, Archive targetArchive) {
this.origin = origin;
this.originArchive = originArchive;
this.target = target;
this.targetArchive = targetArchive;
}
String origin() {
return origin;
}
Archive originArchive() {
return originArchive;
}
String target() {
return target;
}
Archive targetArchive() {
return targetArchive;
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object o) {
if (o instanceof Dep) {
Dep d = (Dep) o;
return this.origin.equals(d.origin) &&
this.originArchive == d.originArchive &&
this.target.equals(d.target) &&
this.targetArchive == d.targetArchive;
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(this.origin,
this.originArchive,
this.target,
this.targetArchive);
}
public String toString() {
return String.format("%s (%s) -> %s (%s)%n",
origin, originArchive.getName(),
target, targetArchive.getName());
}
}
static boolean notFound(Archive archive) {
return archive == NOT_FOUND || archive == REMOVED_JDK_INTERNALS;
}
static final Jdk8Internals REMOVED_JDK_INTERNALS = new Jdk8Internals();
static class Jdk8Internals extends Module {
private static final String NAME = "JDK removed internal API";
private static final String JDK8_INTERNALS = "/com/sun/tools/jdeps/resources/jdk8_internals.txt";
private final Set<String> jdk8Internals;
private Jdk8Internals() {
super(NAME, ModuleDescriptor.newModule("jdk8internals").build(), true);
try (InputStream in = JdepsTask.class.getResourceAsStream(JDK8_INTERNALS);
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
this.jdk8Internals = reader.lines()
.filter(ln -> !ln.startsWith("#"))
.collect(Collectors.toSet());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public boolean contains(Location location) {
String cn = location.getClassName();
int i = cn.lastIndexOf('.');
String pn = i > 0 ? cn.substring(0, i) : "";
return jdk8Internals.contains(pn);
}
@Override
public boolean isJDK() {
return true;
}
@Override
public boolean isExported(String pn) {
return false;
}
}
}