1 /* |
|
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
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 * @modules jdk.compiler/com.sun.tools.javac.api |
|
32 * jdk.compiler/com.sun.tools.javac.code |
|
33 * jdk.compiler/com.sun.tools.javac.file |
|
34 * jdk.compiler/com.sun.tools.javac.main |
|
35 * jdk.compiler/com.sun.tools.javac.util |
|
36 * jdk.compiler/com.sun.tools.sjavac.comp |
|
37 * jdk.compiler/com.sun.tools.sjavac.comp.dependencies |
|
38 * @build Wrapper ToolBox |
|
39 * @run main Wrapper DependencyCollection |
|
40 */ |
|
41 |
|
42 import java.io.IOException; |
|
43 import java.io.PrintWriter; |
|
44 import java.nio.file.Path; |
|
45 import java.nio.file.Paths; |
|
46 import java.util.Arrays; |
|
47 import java.util.Comparator; |
|
48 import java.util.HashSet; |
|
49 import java.util.Set; |
|
50 import java.util.regex.Matcher; |
|
51 import java.util.regex.Pattern; |
|
52 import java.util.stream.Collectors; |
|
53 |
|
54 import javax.tools.JavaCompiler; |
|
55 import javax.tools.JavaFileObject; |
|
56 import javax.tools.StandardJavaFileManager; |
|
57 import javax.tools.ToolProvider; |
|
58 |
|
59 import com.sun.tools.javac.api.JavacTaskImpl; |
|
60 import com.sun.tools.javac.code.Symbol.PackageSymbol; |
|
61 import com.sun.tools.sjavac.comp.SmartFileManager; |
|
62 import com.sun.tools.sjavac.comp.dependencies.DependencyCollector; |
|
63 |
|
64 public class DependencyCollection { |
|
65 |
|
66 public static void main(String[] args) throws IOException { |
|
67 Path src = Paths.get(ToolBox.testSrc, "test-input", "src"); |
|
68 |
|
69 JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); |
|
70 try (StandardJavaFileManager fileManager = javac.getStandardFileManager(null, null, null)) { |
|
71 SmartFileManager smartFileManager = new SmartFileManager(fileManager); |
|
72 smartFileManager.setSymbolFileEnabled(false); |
|
73 Iterable<? extends JavaFileObject> fileObjects = |
|
74 fileManager.getJavaFileObjectsFromFiles(Arrays.asList(src.resolve("pkg/Test.java").toFile())); |
|
75 JavacTaskImpl task = (JavacTaskImpl) javac.getTask(new PrintWriter(System.out), |
|
76 smartFileManager, |
|
77 null, |
|
78 Arrays.asList("-d", "classes", |
|
79 "-sourcepath", src.toAbsolutePath().toString()), |
|
80 null, |
|
81 fileObjects); |
|
82 DependencyCollector depsCollector = new DependencyCollector(); |
|
83 task.addTaskListener(depsCollector); |
|
84 task.doCall(); |
|
85 |
|
86 // Find pkg symbol |
|
87 PackageSymbol pkg = findPkgSymbolWithName(depsCollector.getSourcePackages(), "pkg"); |
|
88 Set<PackageSymbol> foundDependencies = depsCollector.getDependenciesForPkg(pkg); |
|
89 |
|
90 // Print dependencies |
|
91 System.out.println("Found dependencies:"); |
|
92 foundDependencies.stream() |
|
93 .sorted(Comparator.comparing(DependencyCollection::extractNumber)) |
|
94 .forEach(p -> System.out.println(" " + p)); |
|
95 |
|
96 // Check result |
|
97 Set<Integer> found = foundDependencies.stream() |
|
98 .map(DependencyCollection::extractNumber) |
|
99 .collect(Collectors.toSet()); |
|
100 found.remove(-1); // Dependencies with no number (java.lang etc) |
|
101 Set<Integer> expected = new HashSet<>(); |
|
102 for (int i = 2; i <= 30; i++) { |
|
103 if (i == 15) continue; // Case 15 correspond to the type of a throw-away return value. |
|
104 expected.add(i); |
|
105 } |
|
106 |
|
107 Set<Integer> missing = new HashSet<>(expected); |
|
108 missing.removeAll(found); |
|
109 if (missing.size() > 0) { |
|
110 System.out.println("Missing dependencies:"); |
|
111 missing.forEach(i -> System.out.println(" Dependency " + i)); |
|
112 } |
|
113 |
|
114 Set<Integer> unexpected = new HashSet<>(found); |
|
115 unexpected.removeAll(expected); |
|
116 if (unexpected.size() > 0) { |
|
117 System.out.println("Unexpected dependencies found:"); |
|
118 unexpected.forEach(i -> System.out.println(" Dependency " + i)); |
|
119 } |
|
120 |
|
121 if (missing.size() > 0 || unexpected.size() > 0) |
|
122 throw new AssertionError("Missing and/or unexpected dependencies found."); |
|
123 } |
|
124 } |
|
125 |
|
126 private static PackageSymbol findPkgSymbolWithName(Set<PackageSymbol> syms, String name) { |
|
127 for (PackageSymbol ps : syms) |
|
128 if (ps.fullname.toString().equals("pkg")) |
|
129 return ps; |
|
130 throw new AssertionError("Could not find package named \"pkg\"."); |
|
131 } |
|
132 |
|
133 public static int extractNumber(PackageSymbol p) { |
|
134 Matcher m = Pattern.compile("\\d+").matcher(p.fullname.toString()); |
|
135 if (!m.find()) |
|
136 return -1; |
|
137 return Integer.parseInt(m.group()); |
|
138 } |
|
139 } |
|