test/tools/sjavac/DependencyCollection.java
Print this page
rev 2819 : imported patch my-classpath-deps-00
@@ -30,72 +30,82 @@
* @library /tools/lib
* @build Wrapper ToolBox
* @run main Wrapper DependencyCollection
*/
+import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
-import javax.tools.JavaCompiler;
-import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
-import javax.tools.ToolProvider;
import com.sun.tools.javac.api.JavacTaskImpl;
-import com.sun.tools.javac.code.Symbol.PackageSymbol;
+import com.sun.tools.javac.api.JavacTool;
+import com.sun.tools.javac.code.Symbol;
+import com.sun.tools.javac.code.Symbol.ClassSymbol;
+import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
+import com.sun.tools.javac.util.Assert;
+import com.sun.tools.javac.util.Context;
import com.sun.tools.sjavac.comp.SmartFileManager;
import com.sun.tools.sjavac.comp.dependencies.DependencyCollector;
public class DependencyCollection {
public static void main(String[] args) throws IOException {
Path src = Paths.get(ToolBox.testSrc, "test-input", "src");
-
- JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
- try (StandardJavaFileManager fileManager = javac.getStandardFileManager(null, null, null)) {
- SmartFileManager smartFileManager = new SmartFileManager(fileManager);
+ Context context = new Context();
+ JavacTool javac = JavacTool.create();
+ try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
+ SmartFileManager smartFileManager = new SmartFileManager(fm);
smartFileManager.setSymbolFileEnabled(false);
- Iterable<? extends JavaFileObject> fileObjects =
- fileManager.getJavaFileObjectsFromFiles(Arrays.asList(src.resolve("pkg/Test.java").toFile()));
- JavacTaskImpl task = (JavacTaskImpl) javac.getTask(new PrintWriter(System.out),
+ List<File> srcList = Arrays.asList(src.resolve("pkg/Test.java").toFile());
+ JavacTaskImpl task = (JavacTaskImpl) javac.getTask(
+ new PrintWriter(System.out),
smartFileManager,
null,
Arrays.asList("-d", "classes",
"-sourcepath", src.toAbsolutePath().toString()),
null,
- fileObjects);
- DependencyCollector depsCollector = new DependencyCollector();
- task.addTaskListener(depsCollector);
+ fm.getJavaFileObjectsFromFiles(srcList),
+ context);
+ DependencyCollector dc = new DependencyCollector(context);
+ task.addTaskListener(dc);
task.doCall();
- // Find pkg symbol
- PackageSymbol pkg = findPkgSymbolWithName(depsCollector.getSourcePackages(), "pkg");
- Set<PackageSymbol> foundDependencies = depsCollector.getDependenciesForPkg(pkg);
+ // Find Tset.java compilation unit
+ JCCompilationUnit testCU = null;
+ for (JCCompilationUnit cu : dc.collectedDependencies.keySet()) {
+ if (cu.getSourceFile().getName().endsWith("/Test.java"))
+ testCU = cu;
+ }
+ Assert.check(testCU != null, "Could not find Test.java compilation unit.");
+ Set<ClassSymbol> foundDependencies = dc.collectedDependencies.get(testCU).get("pkg.Test");
// Print dependencies
- System.out.println("Found dependencies:");
foundDependencies.stream()
.sorted(Comparator.comparing(DependencyCollection::extractNumber))
.forEach(p -> System.out.println(" " + p));
// Check result
Set<Integer> found = foundDependencies.stream()
.map(DependencyCollection::extractNumber)
.collect(Collectors.toSet());
found.remove(-1); // Dependencies with no number (java.lang etc)
Set<Integer> expected = new HashSet<>();
- for (int i = 2; i <= 30; i++) {
- if (i == 15) continue; // Case 15 correspond to the type of a throw-away return value.
+ for (int i = 2; i <= 32; i++) {
+ if (i == 15) // Case 15 correspond to the type of a
+ continue; // throw-away return value.
expected.add(i);
}
Set<Integer> missing = new HashSet<>(expected);
missing.removeAll(found);
@@ -111,22 +121,17 @@
unexpected.forEach(i -> System.out.println(" Dependency " + i));
}
if (missing.size() > 0 || unexpected.size() > 0)
throw new AssertionError("Missing and/or unexpected dependencies found.");
- }
- }
- private static PackageSymbol findPkgSymbolWithName(Set<PackageSymbol> syms, String name) {
- for (PackageSymbol ps : syms)
- if (ps.fullname.toString().equals("pkg"))
- return ps;
- throw new AssertionError("Could not find package named \"pkg\".");
+ System.out.println("Test passed");
+ }
}
- public static int extractNumber(PackageSymbol p) {
- Matcher m = Pattern.compile("\\d+").matcher(p.fullname.toString());
+ public static int extractNumber(Symbol p) {
+ Matcher m = Pattern.compile("\\d+").matcher(p.name.toString());
if (!m.find())
return -1;
return Integer.parseInt(m.group());
}
}