author | alundblad |
Tue, 07 Oct 2014 21:21:42 +0200 | |
changeset 26992 | 92e69fa21956 |
parent 26991 | 88d998b3bb4b |
child 27319 | 030080f03e4f |
permissions | -rw-r--r-- |
26991
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
1 |
/* |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
4 |
* |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
10 |
* |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
15 |
* accompanied this code). |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
16 |
* |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
20 |
* |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
23 |
* questions. |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
24 |
*/ |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
25 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
26 |
/* |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
27 |
* @test |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
28 |
* @bug 8056258 8048609 |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
29 |
* @summary Ensures that the DependencyCollector covers various cases. |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
30 |
* @library /tools/lib |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
31 |
* @build Wrapper ToolBox |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
32 |
* @run main Wrapper DependencyCollection |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
33 |
*/ |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
34 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
35 |
import java.io.PrintWriter; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
36 |
import java.nio.file.Path; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
37 |
import java.nio.file.Paths; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
38 |
import java.util.Arrays; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
39 |
import java.util.Comparator; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
40 |
import java.util.HashSet; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
41 |
import java.util.Set; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
42 |
import java.util.regex.Matcher; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
43 |
import java.util.regex.Pattern; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
44 |
import java.util.stream.Collectors; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
45 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
46 |
import javax.tools.JavaCompiler; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
47 |
import javax.tools.JavaFileObject; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
48 |
import javax.tools.StandardJavaFileManager; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
49 |
import javax.tools.ToolProvider; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
50 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
51 |
import com.sun.tools.javac.api.JavacTaskImpl; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
52 |
import com.sun.tools.javac.code.Symbol.PackageSymbol; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
53 |
import com.sun.tools.sjavac.comp.SmartFileManager; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
54 |
import com.sun.tools.sjavac.comp.dependencies.DependencyCollector; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
55 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
56 |
public class DependencyCollection { |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
57 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
58 |
public static void main(String[] args) { |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
59 |
Path src = Paths.get(ToolBox.testSrc, "test-input", "src"); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
60 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
61 |
JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
62 |
StandardJavaFileManager fileManager = javac.getStandardFileManager(null, null, null); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
63 |
SmartFileManager smartFileManager = new SmartFileManager(fileManager); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
64 |
smartFileManager.setSymbolFileEnabled(false); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
65 |
Iterable<? extends JavaFileObject> fileObjects = |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
66 |
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(src.resolve("pkg/Test.java").toFile())); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
67 |
JavacTaskImpl task = (JavacTaskImpl) javac.getTask(new PrintWriter(System.out), |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
68 |
smartFileManager, |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
69 |
null, |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
70 |
Arrays.asList("-d", "classes", |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
71 |
"-sourcepath", src.toAbsolutePath().toString()), |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
72 |
null, |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
73 |
fileObjects); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
74 |
DependencyCollector depsCollector = new DependencyCollector(); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
75 |
task.addTaskListener(depsCollector); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
76 |
task.doCall(); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
77 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
78 |
// Find pkg symbol |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
79 |
PackageSymbol pkg = findPkgSymbolWithName(depsCollector.getSourcePackages(), "pkg"); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
80 |
Set<PackageSymbol> foundDependencies = depsCollector.getDependenciesForPkg(pkg); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
81 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
82 |
// Print dependencies |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
83 |
System.out.println("Found dependencies:"); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
84 |
foundDependencies.stream() |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
85 |
.sorted(Comparator.comparing(DependencyCollection::extractNumber)) |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
86 |
.forEach(p -> System.out.println(" " + p)); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
87 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
88 |
// Check result |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
89 |
Set<Integer> found = foundDependencies.stream() |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
90 |
.map(DependencyCollection::extractNumber) |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
91 |
.collect(Collectors.toSet()); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
92 |
found.remove(-1); // Dependencies with no number (java.lang etc) |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
93 |
Set<Integer> expected = new HashSet<>(); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
94 |
for (int i = 2; i <= 30; i++) { |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
95 |
if (i == 15) continue; // Case 15 correspond to the type of a throw-away return value. |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
96 |
expected.add(i); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
97 |
} |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
98 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
99 |
Set<Integer> missing = new HashSet<>(expected); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
100 |
missing.removeAll(found); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
101 |
if (missing.size() > 0) { |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
102 |
System.out.println("Missing dependencies:"); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
103 |
missing.forEach(i -> System.out.println(" Dependency " + i)); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
104 |
} |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
105 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
106 |
Set<Integer> unexpected = new HashSet<>(found); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
107 |
unexpected.removeAll(expected); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
108 |
if (unexpected.size() > 0) { |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
109 |
System.out.println("Unexpected dependencies found:"); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
110 |
unexpected.forEach(i -> System.out.println(" Dependency " + i)); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
111 |
} |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
112 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
113 |
if (missing.size() > 0 || unexpected.size() > 0) |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
114 |
throw new AssertionError("Missing and/or unexpected dependencies found."); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
115 |
} |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
116 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
117 |
private static PackageSymbol findPkgSymbolWithName(Set<PackageSymbol> syms, String name) { |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
118 |
for (PackageSymbol ps : syms) |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
119 |
if (ps.fullname.toString().equals("pkg")) |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
120 |
return ps; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
121 |
throw new AssertionError("Could not find package named \"pkg\"."); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
122 |
} |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
123 |
|
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
124 |
public static int extractNumber(PackageSymbol p) { |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
125 |
Matcher m = Pattern.compile("\\d+").matcher(p.fullname.toString()); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
126 |
if (!m.find()) |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
127 |
return -1; |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
128 |
return Integer.parseInt(m.group()); |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
129 |
} |
88d998b3bb4b
8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff
changeset
|
130 |
} |