--- a/langtools/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/Analyzer.java Tue Mar 15 13:48:30 2016 -0700
+++ b/langtools/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/Analyzer.java Thu Mar 17 19:04:28 2016 +0000
@@ -26,9 +26,13 @@
package com.sun.tools.jdeps;
import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.Comparator;
+import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -63,9 +67,9 @@
protected final Type type;
protected final Filter filter;
- protected final Map<Archive, ArchiveDeps> results = new HashMap<>();
- protected final Map<Location, Archive> map = new HashMap<>();
- private static final Archive NOT_FOUND
+ 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"));
/**
@@ -82,29 +86,31 @@
/**
* Performs the dependency analysis on the given archives.
*/
- public boolean run(List<Archive> archives) {
+ public boolean run(Stream<? extends Archive> archives) {
+ return run(archives.collect(Collectors.toList()));
+ }
+
+ /**
+ * Performs the dependency analysis on the given archives.
+ */
+ public boolean run(Iterable<? extends Archive> archives) {
// build a map from Location to Archive
buildLocationArchiveMap(archives);
// traverse and analyze all dependencies
for (Archive archive : archives) {
- ArchiveDeps deps = new ArchiveDeps(archive, type);
+ Dependences deps = new Dependences(archive, type);
archive.visitDependences(deps);
results.put(archive, deps);
}
return true;
}
- protected void buildLocationArchiveMap(List<Archive> archives) {
+ protected void buildLocationArchiveMap(Iterable<? extends Archive> archives) {
// build a map from Location to Archive
for (Archive archive: archives) {
- for (Location l: archive.getClasses()) {
- if (!map.containsKey(l)) {
- map.put(l, archive);
- } else {
- // duplicated class warning?
- }
- }
+ archive.getClasses()
+ .forEach(l -> locationToArchive.putIfAbsent(l, archive));
}
}
@@ -116,12 +122,23 @@
}
public Set<String> dependences(Archive source) {
- ArchiveDeps result = results.get(source);
+ if (!results.containsKey(source)) {
+ return Collections.emptySet();
+ }
+ Dependences result = results.get(source);
return result.dependencies().stream()
.map(Dep::target)
.collect(Collectors.toSet());
}
+ public Stream<Archive> requires(Archive source) {
+ if (!results.containsKey(source)) {
+ return Stream.empty();
+ }
+ Dependences result = results.get(source);
+ return result.requires().stream().filter(a -> !a.isEmpty());
+ }
+
public interface Visitor {
/**
* Visits a recorded dependency from origin to target which can be
@@ -138,7 +155,7 @@
*/
public void visitDependences(Archive source, Visitor v, Type level) {
if (level == Type.SUMMARY) {
- final ArchiveDeps result = results.get(source);
+ final Dependences result = results.get(source);
final Set<Archive> reqs = result.requires();
Stream<Archive> stream = reqs.stream();
if (reqs.isEmpty()) {
@@ -152,19 +169,21 @@
.forEach(archive -> {
Profile profile = result.getTargetProfile(archive);
v.visitDependence(source.getName(), source,
- profile != null ? profile.profileName() : archive.getName(), archive);
+ profile != null ? profile.profileName()
+ : archive.getName(), archive);
});
} else {
- ArchiveDeps result = results.get(source);
+ Dependences result = results.get(source);
if (level != type) {
// requesting different level of analysis
- result = new ArchiveDeps(source, level);
+ result = new Dependences(source, level);
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()));
+ .forEach(d -> v.visitDependence(d.origin(), d.originArchive(),
+ d.target(), d.targetArchive()));
}
}
@@ -173,16 +192,16 @@
}
/**
- * ArchiveDeps contains the dependencies for an Archive that can have one or
+ * Dependences contains the dependencies for an Archive that can have one or
* more classes.
*/
- class ArchiveDeps implements Archive.Visitor {
+ class Dependences implements Archive.Visitor {
protected final Archive archive;
protected final Set<Archive> requires;
protected final Set<Dep> deps;
protected final Type level;
private Profile profile;
- ArchiveDeps(Archive archive, Type level) {
+ Dependences(Archive archive, Type level) {
this.archive = archive;
this.deps = new HashSet<>();
this.requires = new HashSet<>();
@@ -198,7 +217,7 @@
}
Profile getTargetProfile(Archive target) {
- if (target instanceof Module) {
+ if (target.getModule().isJDK()) {
return Profile.getProfile((Module) target);
} else {
return null;
@@ -206,11 +225,10 @@
}
Archive findArchive(Location t) {
- Archive target = archive.getClasses().contains(t) ? archive : map.get(t);
- if (target == null) {
- map.put(t, target = NOT_FOUND);
- }
- return target;
+ if (archive.getClasses().contains(t))
+ return archive;
+
+ return locationToArchive.computeIfAbsent(t, _k -> NOT_FOUND);
}
// return classname or package name depedning on the level
@@ -232,7 +250,7 @@
requires.add(targetArchive);
}
}
- if (targetArchive instanceof Module) {
+ if (targetArchive.getModule().isNamed()) {
Profile p = Profile.getProfile(t.getPackageName());
if (profile == null || (p != null && p.compareTo(profile) > 0)) {
profile = p;
@@ -315,12 +333,10 @@
@Override
public int hashCode() {
- int hash = 7;
- hash = 67*hash + Objects.hashCode(this.origin)
- + Objects.hashCode(this.originArchive)
- + Objects.hashCode(this.target)
- + Objects.hashCode(this.targetArchive);
- return hash;
+ return Objects.hash(this.origin,
+ this.originArchive,
+ this.target,
+ this.targetArchive);
}
public String toString() {
@@ -329,124 +345,4 @@
target, targetArchive.getName());
}
}
-
- static Analyzer getExportedAPIsAnalyzer() {
- return new ModuleAccessAnalyzer(ModuleAccessAnalyzer.reexportsFilter, true);
- }
-
- static Analyzer getModuleAccessAnalyzer() {
- return new ModuleAccessAnalyzer(ModuleAccessAnalyzer.accessCheckFilter, false);
- }
-
- private static class ModuleAccessAnalyzer extends Analyzer {
- private final boolean apionly;
- ModuleAccessAnalyzer(Filter filter, boolean apionly) {
- super(Type.VERBOSE, filter);
- this.apionly = apionly;
- }
- /**
- * Verify module access
- */
- public boolean run(List<Archive> archives) {
- // build a map from Location to Archive
- buildLocationArchiveMap(archives);
-
- // traverse and analyze all dependencies
- int count = 0;
- for (Archive archive : archives) {
- ArchiveDeps checker = new ArchiveDeps(archive, type);
- archive.visitDependences(checker);
- count += checker.dependencies().size();
- // output if any error
- Module m = (Module)archive;
- printDependences(System.err, m, checker.dependencies());
- results.put(archive, checker);
- }
- return count == 0;
- }
-
- private void printDependences(PrintStream out, Module m, Set<Dep> deps) {
- if (deps.isEmpty())
- return;
-
- String msg = apionly ? "API reference:" : "inaccessible reference:";
- deps.stream().sorted(Comparator.comparing(Dep::origin)
- .thenComparing(Dep::target))
- .forEach(d -> out.format("%s %s (%s) -> %s (%s)%n", msg,
- d.origin(), d.originArchive().getName(),
- d.target(), d.targetArchive().getName()));
- if (apionly) {
- out.format("Dependences missing re-exports=\"true\" attribute:%n");
- deps.stream()
- .map(Dep::targetArchive)
- .map(Archive::getName)
- .distinct()
- .sorted()
- .forEach(d -> out.format(" %s -> %s%n", m.name(), d));
- }
- }
-
- private static Module findModule(Archive archive) {
- if (Module.class.isInstance(archive)) {
- return (Module) archive;
- } else {
- return null;
- }
- }
-
- // returns true if target is accessible by origin
- private static boolean canAccess(Location o, Archive originArchive, Location t, Archive targetArchive) {
- Module origin = findModule(originArchive);
- Module target = findModule(targetArchive);
-
- if (targetArchive == Analyzer.NOT_FOUND) {
- return false;
- }
-
- // unnamed module
- // ## should check public type?
- if (target == null)
- return true;
-
- // module-private
- if (origin == target)
- return true;
-
- return target.isAccessibleTo(t.getClassName(), origin);
- }
-
- static final Filter accessCheckFilter = new Filter() {
- @Override
- public boolean accepts(Location o, Archive originArchive, Location t, Archive targetArchive) {
- return !canAccess(o, originArchive, t, targetArchive);
- }
- };
-
- static final Filter reexportsFilter = new Filter() {
- @Override
- public boolean accepts(Location o, Archive originArchive, Location t, Archive targetArchive) {
- Module origin = findModule(originArchive);
- Module target = findModule(targetArchive);
- if (!origin.isExportedPackage(o.getPackageName())) {
- // filter non-exported classes
- return false;
- }
-
- boolean accessible = canAccess(o, originArchive, t, targetArchive);
- if (!accessible)
- return true;
-
- String mn = target.name();
- // skip checking re-exports for java.base
- if (origin == target || "java.base".equals(mn))
- return false;
-
- assert origin.requires().containsKey(mn); // otherwise, should not be accessible
- if (origin.requires().get(mn)) {
- return false;
- }
- return true;
- }
- };
- }
}