Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/ToolEnvironment.java
41161 views
1
/*
2
* Copyright (c) 2000, 2021, 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 jdk.javadoc.internal.tool;
27
28
29
import java.util.*;
30
31
import javax.lang.model.element.Element;
32
import javax.lang.model.element.TypeElement;
33
import javax.lang.model.util.Elements;
34
import javax.tools.JavaFileManager;
35
import javax.tools.JavaFileObject;
36
import javax.tools.JavaFileObject.Kind;
37
38
import com.sun.source.util.DocTrees;
39
import com.sun.source.util.TreePath;
40
import com.sun.tools.javac.api.JavacTrees;
41
import com.sun.tools.javac.code.ClassFinder;
42
import com.sun.tools.javac.code.Flags;
43
import com.sun.tools.javac.code.Source;
44
import com.sun.tools.javac.code.Symbol;
45
import com.sun.tools.javac.code.Symbol.ClassSymbol;
46
import com.sun.tools.javac.code.Symbol.CompletionFailure;
47
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
48
import com.sun.tools.javac.code.Symtab;
49
import com.sun.tools.javac.comp.AttrContext;
50
import com.sun.tools.javac.comp.Check;
51
import com.sun.tools.javac.comp.Enter;
52
import com.sun.tools.javac.comp.Env;
53
import com.sun.tools.javac.file.JavacFileManager;
54
import com.sun.tools.javac.model.JavacElements;
55
import com.sun.tools.javac.model.JavacTypes;
56
import com.sun.tools.javac.tree.JCTree;
57
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
58
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
59
import com.sun.tools.javac.tree.JCTree.JCPackageDecl;
60
import com.sun.tools.javac.util.Context;
61
import com.sun.tools.javac.util.Convert;
62
import com.sun.tools.javac.util.Name;
63
import com.sun.tools.javac.util.Names;
64
65
/**
66
* Holds the environment for a run of javadoc.
67
* Holds only the information needed throughout the
68
* run and not the compiler info that could be GC'ed
69
* or ported.
70
*
71
* <p><b>This is NOT part of any supported API.
72
* If you write code that depends on this, you do so at your own risk.
73
* This code and its internal interfaces are subject to change or
74
* deletion without notice.</b>
75
*/
76
public class ToolEnvironment {
77
protected static final Context.Key<ToolEnvironment> ToolEnvKey = new Context.Key<>();
78
79
public static ToolEnvironment instance(Context context) {
80
ToolEnvironment instance = context.get(ToolEnvKey);
81
if (instance == null)
82
instance = new ToolEnvironment(context);
83
return instance;
84
}
85
86
final Messager messager;
87
88
/** Predefined symbols known to the compiler. */
89
public final Symtab syms;
90
91
/** JavaDoc's subtype of the compiler's class finder */
92
private final ClassFinder finder;
93
94
/** Javadoc's subtype of the compiler's enter phase. */
95
final Enter enter;
96
97
/** The name table. */
98
private Names names;
99
100
/** If true, prevent printing of any notifications. */
101
boolean quiet = false;
102
103
/** If true, ignore all errors encountered during Enter. */
104
boolean ignoreSourceErrors = false;
105
106
Check chk;
107
com.sun.tools.javac.code.Types types;
108
JavaFileManager fileManager;
109
public final Context context;
110
111
WeakHashMap<JCTree, TreePath> treePaths = new WeakHashMap<>();
112
113
/** Allow documenting from class files? */
114
boolean docClasses = false;
115
116
/**
117
* The source language version.
118
*/
119
public final Source source;
120
121
public final Elements elements;
122
123
public final JavacTypes typeutils;
124
125
protected DocEnvImpl docEnv;
126
127
public final DocTrees docTrees;
128
129
public final Map<Element, TreePath> elementToTreePath;
130
131
/**
132
* Constructor
133
*
134
* @param context Context for this javadoc instance.
135
*/
136
protected ToolEnvironment(Context context) {
137
context.put(ToolEnvKey, this);
138
this.context = context;
139
140
messager = Messager.instance0(context);
141
syms = Symtab.instance(context);
142
finder = JavadocClassFinder.instance(context);
143
enter = JavadocEnter.instance(context);
144
names = Names.instance(context);
145
chk = Check.instance(context);
146
types = com.sun.tools.javac.code.Types.instance(context);
147
fileManager = context.get(JavaFileManager.class);
148
if (fileManager instanceof JavacFileManager jfm) {
149
jfm.setSymbolFileEnabled(false);
150
}
151
docTrees = JavacTrees.instance(context);
152
source = Source.instance(context);
153
elements = JavacElements.instance(context);
154
typeutils = JavacTypes.instance(context);
155
elementToTreePath = new HashMap<>();
156
}
157
158
public void initialize(ToolOptions options) {
159
this.quiet = options.quiet();
160
this.ignoreSourceErrors = options.ignoreSourceErrors();
161
}
162
163
/**
164
* Load a class by qualified name.
165
*/
166
public TypeElement loadClass(String name) {
167
try {
168
Name nameImpl = names.fromString(name);
169
ModuleSymbol mod = syms.inferModule(Convert.packagePart(nameImpl));
170
ClassSymbol c = finder.loadClass(mod != null ? mod : syms.errModule, nameImpl);
171
return c;
172
} catch (CompletionFailure ex) {
173
chk.completionError(null, ex);
174
return null;
175
}
176
}
177
178
boolean isSynthetic(Symbol sym) {
179
return (sym.flags() & Flags.SYNTHETIC) != 0;
180
}
181
182
void setElementToTreePath(Element e, TreePath tree) {
183
if (e == null || tree == null)
184
return;
185
elementToTreePath.put(e, tree);
186
}
187
188
public Kind getFileKind(TypeElement te) {
189
JavaFileObject jfo = ((ClassSymbol)te).outermostClass().classfile;
190
return jfo == null ? Kind.SOURCE : jfo.getKind();
191
}
192
193
/**
194
* Print a notice, iff <em>quiet</em> is not specified.
195
*
196
* @param key selects message from resource
197
*/
198
public void notice(String key) {
199
if (quiet) {
200
return;
201
}
202
messager.noticeUsingKey(key);
203
}
204
205
/**
206
* Print a notice, iff <em>quiet</em> is not specified.
207
*
208
* @param key selects message from resource
209
* @param a1 first argument
210
*/
211
public void notice(String key, String a1) {
212
if (quiet) {
213
return;
214
}
215
messager.noticeUsingKey(key, a1);
216
}
217
218
TreePath getTreePath(JCCompilationUnit tree) {
219
TreePath p = treePaths.get(tree);
220
if (p == null)
221
treePaths.put(tree, p = new TreePath(tree));
222
return p;
223
}
224
225
TreePath getTreePath(JCCompilationUnit toplevel, JCPackageDecl tree) {
226
TreePath p = treePaths.get(tree);
227
if (p == null)
228
treePaths.put(tree, p = new TreePath(getTreePath(toplevel), tree));
229
return p;
230
}
231
232
TreePath getTreePath(JCCompilationUnit toplevel, JCClassDecl tree) {
233
TreePath p = treePaths.get(tree);
234
if (p == null)
235
treePaths.put(tree, p = new TreePath(getTreePath(toplevel), tree));
236
return p;
237
}
238
239
TreePath getTreePath(JCCompilationUnit toplevel, JCClassDecl cdecl, JCTree tree) {
240
return new TreePath(getTreePath(toplevel, cdecl), tree);
241
}
242
243
public com.sun.tools.javac.code.Types getTypes() {
244
return types;
245
}
246
247
public Env<AttrContext> getEnv(ClassSymbol tsym) {
248
return enter.getEnv(tsym);
249
}
250
251
public boolean isQuiet() {
252
return quiet;
253
}
254
}
255
256