Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/jdeprscan/TraverseProc.java
41161 views
1
/*
2
* Copyright (c) 2016, 2018, 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
package com.sun.tools.jdeprscan;
27
28
import java.util.ArrayList;
29
import java.util.Collection;
30
import java.util.Comparator;
31
import java.util.HashMap;
32
import java.util.HashSet;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Set;
36
37
import javax.annotation.processing.AbstractProcessor;
38
import javax.annotation.processing.Messager;
39
import javax.annotation.processing.ProcessingEnvironment;
40
import javax.annotation.processing.RoundEnvironment;
41
import javax.annotation.processing.SupportedAnnotationTypes;
42
import javax.annotation.processing.SupportedSourceVersion;
43
44
import javax.lang.model.SourceVersion;
45
import javax.lang.model.element.Element;
46
import javax.lang.model.element.ElementKind;
47
import javax.lang.model.element.Modifier;
48
import javax.lang.model.element.ModuleElement;
49
import javax.lang.model.element.PackageElement;
50
import javax.lang.model.element.TypeElement;
51
import javax.lang.model.util.Elements;
52
import javax.tools.Diagnostic;
53
54
@SupportedAnnotationTypes("*")
55
public class TraverseProc extends AbstractProcessor {
56
Elements elements;
57
Messager messager;
58
final List<String> moduleRoots;
59
Map<PackageElement, List<TypeElement>> publicTypes;
60
61
TraverseProc(List<String> roots) {
62
moduleRoots = roots;
63
}
64
65
@Override
66
public void init(ProcessingEnvironment pe) {
67
super.init(pe);
68
elements = pe.getElementUtils();
69
messager = pe.getMessager();
70
}
71
72
@Override
73
public SourceVersion getSupportedSourceVersion() {
74
return SourceVersion.latest();
75
}
76
77
@Override
78
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
79
if (roundEnv.processingOver()) {
80
return false;
81
}
82
83
Set<ModuleElement> modules = new HashSet<>();
84
for (String mname : moduleRoots) {
85
ModuleElement me = elements.getModuleElement(mname);
86
if (me == null) {
87
messager.printMessage(Diagnostic.Kind.ERROR,
88
String.format("module %s not found%n", mname));
89
} else {
90
modules.addAll(findModules(me));
91
}
92
}
93
94
Set<PackageElement> packages = findPackages(modules);
95
96
publicTypes = findPublicTypes(packages);
97
98
return true;
99
}
100
101
void printPublicTypes() {
102
printPublicTypes(publicTypes);
103
}
104
105
public Map<PackageElement, List<TypeElement>> getPublicTypes() {
106
return publicTypes;
107
}
108
109
void printPublicTypes(Map<PackageElement, List<TypeElement>> types) {
110
System.out.println("All public types:");
111
types.entrySet().stream()
112
.sorted(Comparator.comparing(e -> e.getKey().toString()))
113
.forEach(e -> {
114
System.out.println(" " + e.getKey());
115
e.getValue().stream()
116
.sorted(Comparator.comparing(TypeElement::toString))
117
.forEach(t -> System.out.println(" " + t));
118
});
119
System.out.println();
120
System.out.flush();
121
}
122
123
Set<ModuleElement> findModules(ModuleElement root) {
124
return findModules0(root, new HashSet<>(), 0);
125
}
126
127
Set<ModuleElement> findModules0(ModuleElement m, Set<ModuleElement> set, int nesting) {
128
set.add(m);
129
for (ModuleElement.Directive dir : m.getDirectives()) {
130
if (dir.getKind() == ModuleElement.DirectiveKind.REQUIRES) {
131
ModuleElement.RequiresDirective req = (ModuleElement.RequiresDirective)dir;
132
findModules0(req.getDependency(), set, nesting + 1);
133
}
134
}
135
return set;
136
}
137
138
Set<PackageElement> findPackages(Collection<ModuleElement> mods) {
139
Set<PackageElement> set = new HashSet<>();
140
for (ModuleElement m : mods) {
141
for (ModuleElement.Directive dir : m.getDirectives()) {
142
if (dir.getKind() == ModuleElement.DirectiveKind.EXPORTS) { //XXX
143
ModuleElement.ExportsDirective exp = (ModuleElement.ExportsDirective)dir;
144
if (exp.getTargetModules() == null) {
145
set.add(exp.getPackage());
146
}
147
}
148
}
149
}
150
return set;
151
}
152
153
Map<PackageElement, List<TypeElement>> findPublicTypes(Collection<PackageElement> pkgs) {
154
Map<PackageElement, List<TypeElement>> map = new HashMap<>();
155
for (PackageElement pkg : pkgs) {
156
List<TypeElement> enclosed = new ArrayList<>();
157
for (Element e : pkg.getEnclosedElements()) {
158
addPublicTypes(enclosed, e);
159
}
160
map.put(pkg, enclosed);
161
}
162
return map;
163
}
164
165
void addPublicTypes(List<TypeElement> list, Element e) {
166
ElementKind kind = e.getKind();
167
if ((kind.isClass() || kind.isInterface())
168
&& e.getModifiers().contains(Modifier.PUBLIC)) {
169
list.add((TypeElement)e);
170
for (Element enc : e.getEnclosedElements()) {
171
addPublicTypes(list, enc);
172
}
173
}
174
}
175
}
176
177