15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 /*
27 * @test
28 * @bug 8056258 8048609
29 * @summary Ensures that the DependencyCollector covers various cases.
30 * @library /tools/lib
31 * @build Wrapper ToolBox
32 * @run main Wrapper DependencyCollection
33 */
34
35 import java.io.IOException;
36 import java.io.PrintWriter;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.Arrays;
40 import java.util.Comparator;
41 import java.util.HashSet;
42 import java.util.Set;
43 import java.util.regex.Matcher;
44 import java.util.regex.Pattern;
45 import java.util.stream.Collectors;
46
47 import javax.tools.JavaCompiler;
48 import javax.tools.JavaFileObject;
49 import javax.tools.StandardJavaFileManager;
50 import javax.tools.ToolProvider;
51
52 import com.sun.tools.javac.api.JavacTaskImpl;
53 import com.sun.tools.javac.code.Symbol.PackageSymbol;
54 import com.sun.tools.sjavac.comp.SmartFileManager;
55 import com.sun.tools.sjavac.comp.dependencies.DependencyCollector;
56
57 public class DependencyCollection {
58
59 public static void main(String[] args) throws IOException {
60 Path src = Paths.get(ToolBox.testSrc, "test-input", "src");
61
62 JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
63 try (StandardJavaFileManager fileManager = javac.getStandardFileManager(null, null, null)) {
64 SmartFileManager smartFileManager = new SmartFileManager(fileManager);
65 smartFileManager.setSymbolFileEnabled(false);
66 Iterable<? extends JavaFileObject> fileObjects =
67 fileManager.getJavaFileObjectsFromFiles(Arrays.asList(src.resolve("pkg/Test.java").toFile()));
68 JavacTaskImpl task = (JavacTaskImpl) javac.getTask(new PrintWriter(System.out),
69 smartFileManager,
70 null,
71 Arrays.asList("-d", "classes",
72 "-sourcepath", src.toAbsolutePath().toString()),
73 null,
74 fileObjects);
75 DependencyCollector depsCollector = new DependencyCollector();
76 task.addTaskListener(depsCollector);
77 task.doCall();
78
79 // Find pkg symbol
80 PackageSymbol pkg = findPkgSymbolWithName(depsCollector.getSourcePackages(), "pkg");
81 Set<PackageSymbol> foundDependencies = depsCollector.getDependenciesForPkg(pkg);
82
83 // Print dependencies
84 System.out.println("Found dependencies:");
85 foundDependencies.stream()
86 .sorted(Comparator.comparing(DependencyCollection::extractNumber))
87 .forEach(p -> System.out.println(" " + p));
88
89 // Check result
90 Set<Integer> found = foundDependencies.stream()
91 .map(DependencyCollection::extractNumber)
92 .collect(Collectors.toSet());
93 found.remove(-1); // Dependencies with no number (java.lang etc)
94 Set<Integer> expected = new HashSet<>();
95 for (int i = 2; i <= 30; i++) {
96 if (i == 15) continue; // Case 15 correspond to the type of a throw-away return value.
97 expected.add(i);
98 }
99
100 Set<Integer> missing = new HashSet<>(expected);
101 missing.removeAll(found);
102 if (missing.size() > 0) {
103 System.out.println("Missing dependencies:");
104 missing.forEach(i -> System.out.println(" Dependency " + i));
105 }
106
107 Set<Integer> unexpected = new HashSet<>(found);
108 unexpected.removeAll(expected);
109 if (unexpected.size() > 0) {
110 System.out.println("Unexpected dependencies found:");
111 unexpected.forEach(i -> System.out.println(" Dependency " + i));
112 }
113
114 if (missing.size() > 0 || unexpected.size() > 0)
115 throw new AssertionError("Missing and/or unexpected dependencies found.");
116 }
117 }
118
119 private static PackageSymbol findPkgSymbolWithName(Set<PackageSymbol> syms, String name) {
120 for (PackageSymbol ps : syms)
121 if (ps.fullname.toString().equals("pkg"))
122 return ps;
123 throw new AssertionError("Could not find package named \"pkg\".");
124 }
125
126 public static int extractNumber(PackageSymbol p) {
127 Matcher m = Pattern.compile("\\d+").matcher(p.fullname.toString());
128 if (!m.find())
129 return -1;
130 return Integer.parseInt(m.group());
131 }
132 }
|
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 /*
27 * @test
28 * @bug 8056258 8048609
29 * @summary Ensures that the DependencyCollector covers various cases.
30 * @library /tools/lib
31 * @build Wrapper ToolBox
32 * @run main Wrapper DependencyCollection
33 */
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.io.PrintWriter;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.Arrays;
41 import java.util.Comparator;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Set;
45 import java.util.regex.Matcher;
46 import java.util.regex.Pattern;
47 import java.util.stream.Collectors;
48
49 import javax.tools.StandardJavaFileManager;
50
51 import com.sun.tools.javac.api.JavacTaskImpl;
52 import com.sun.tools.javac.api.JavacTool;
53 import com.sun.tools.javac.code.Symbol;
54 import com.sun.tools.javac.code.Symbol.ClassSymbol;
55 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
56 import com.sun.tools.javac.util.Assert;
57 import com.sun.tools.javac.util.Context;
58 import com.sun.tools.sjavac.comp.SmartFileManager;
59 import com.sun.tools.sjavac.comp.dependencies.DependencyCollector;
60
61 public class DependencyCollection {
62
63 public static void main(String[] args) throws IOException {
64 Path src = Paths.get(ToolBox.testSrc, "test-input", "src");
65 Context context = new Context();
66 JavacTool javac = JavacTool.create();
67 try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
68 SmartFileManager smartFileManager = new SmartFileManager(fm);
69 smartFileManager.setSymbolFileEnabled(false);
70 List<File> srcList = Arrays.asList(src.resolve("pkg/Test.java").toFile());
71 JavacTaskImpl task = (JavacTaskImpl) javac.getTask(
72 new PrintWriter(System.out),
73 smartFileManager,
74 null,
75 Arrays.asList("-d", "classes",
76 "-sourcepath", src.toAbsolutePath().toString()),
77 null,
78 fm.getJavaFileObjectsFromFiles(srcList),
79 context);
80 DependencyCollector dc = new DependencyCollector(context);
81 task.addTaskListener(dc);
82 task.doCall();
83
84 // Find Tset.java compilation unit
85 JCCompilationUnit testCU = null;
86 for (JCCompilationUnit cu : dc.collectedDependencies.keySet()) {
87 if (cu.getSourceFile().getName().endsWith("/Test.java"))
88 testCU = cu;
89 }
90 Assert.check(testCU != null, "Could not find Test.java compilation unit.");
91 Set<ClassSymbol> foundDependencies = dc.collectedDependencies.get(testCU).get("pkg.Test");
92
93 // Print dependencies
94 foundDependencies.stream()
95 .sorted(Comparator.comparing(DependencyCollection::extractNumber))
96 .forEach(p -> System.out.println(" " + p));
97
98 // Check result
99 Set<Integer> found = foundDependencies.stream()
100 .map(DependencyCollection::extractNumber)
101 .collect(Collectors.toSet());
102 found.remove(-1); // Dependencies with no number (java.lang etc)
103 Set<Integer> expected = new HashSet<>();
104 for (int i = 2; i <= 32; i++) {
105 if (i == 15) // Case 15 correspond to the type of a
106 continue; // throw-away return value.
107 expected.add(i);
108 }
109
110 Set<Integer> missing = new HashSet<>(expected);
111 missing.removeAll(found);
112 if (missing.size() > 0) {
113 System.out.println("Missing dependencies:");
114 missing.forEach(i -> System.out.println(" Dependency " + i));
115 }
116
117 Set<Integer> unexpected = new HashSet<>(found);
118 unexpected.removeAll(expected);
119 if (unexpected.size() > 0) {
120 System.out.println("Unexpected dependencies found:");
121 unexpected.forEach(i -> System.out.println(" Dependency " + i));
122 }
123
124 if (missing.size() > 0 || unexpected.size() > 0)
125 throw new AssertionError("Missing and/or unexpected dependencies found.");
126
127 System.out.println("Test passed");
128 }
129 }
130
131 public static int extractNumber(Symbol p) {
132 Matcher m = Pattern.compile("\\d+").matcher(p.name.toString());
133 if (!m.find())
134 return -1;
135 return Integer.parseInt(m.group());
136 }
137 }
|