This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
AbstractExecutableMemberWriter.java
314 lines (287 loc) · 12.4 KB
/
AbstractExecutableMemberWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.javadoc.internal.doclets.formats.html;
import java.util.List;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.SimpleTypeVisitor14;
import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
import jdk.javadoc.internal.doclets.formats.html.markup.Entity;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
import jdk.javadoc.internal.doclets.toolkit.Content;
import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants;
import static jdk.javadoc.internal.doclets.formats.html.LinkInfoImpl.Kind.EXECUTABLE_MEMBER_PARAM;
import static jdk.javadoc.internal.doclets.formats.html.LinkInfoImpl.Kind.MEMBER;
import static jdk.javadoc.internal.doclets.formats.html.LinkInfoImpl.Kind.MEMBER_DEPRECATED_PREVIEW;
import static jdk.javadoc.internal.doclets.formats.html.LinkInfoImpl.Kind.MEMBER_TYPE_PARAMS;
import static jdk.javadoc.internal.doclets.formats.html.LinkInfoImpl.Kind.RECEIVER_TYPE;
import static jdk.javadoc.internal.doclets.formats.html.LinkInfoImpl.Kind.THROWS_TYPE;
/**
* Print method and constructor info.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
public abstract class AbstractExecutableMemberWriter extends AbstractMemberWriter {
public AbstractExecutableMemberWriter(SubWriterHolderWriter writer, TypeElement typeElement) {
super(writer, typeElement);
}
public AbstractExecutableMemberWriter(SubWriterHolderWriter writer) {
super(writer);
}
/**
* Get the type parameters for the executable member.
*
* @param member the member for which to get the type parameters.
* @return the type parameters.
*/
protected Content getTypeParameters(ExecutableElement member) {
LinkInfoImpl linkInfo = new LinkInfoImpl(configuration, MEMBER_TYPE_PARAMS, member);
return writer.getTypeParameterLinks(linkInfo);
}
@Override
protected Content getSummaryLink(Element member) {
Content content = new ContentBuilder();
content.add(utils.getFullyQualifiedName(member));
if (!utils.isConstructor(member)) {
content.add(".");
content.add(member.getSimpleName());
}
String signature = utils.flatSignature((ExecutableElement) member, typeElement);
if (signature.length() > 2) {
content.add(Entity.ZERO_WIDTH_SPACE);
}
content.add(signature);
return writer.getDocLink(MEMBER_DEPRECATED_PREVIEW, utils.getEnclosingTypeElement(member),
member, content, false);
}
/**
* Add the summary link for the member.
*
* @param context the id of the context where the link will be printed
* @param te the type element being linked to
* @param member the member being linked to
* @param tdSummary the content tree to which the link will be added
*/
@Override
protected void addSummaryLink(LinkInfoImpl.Kind context, TypeElement te, Element member,
Content tdSummary) {
ExecutableElement ee = (ExecutableElement)member;
Content memberLink = HtmlTree.SPAN(HtmlStyle.memberNameLink,
writer.getDocLink(context, te, ee, name(ee)));
Content code = HtmlTree.CODE(memberLink);
addParameters(ee, code);
tdSummary.add(code);
}
/**
* Add the inherited summary link for the member.
*
* @param te the type element that we should link to
* @param member the member being linked to
* @param linksTree the content tree to which the link will be added
*/
@Override
protected void addInheritedSummaryLink(TypeElement te, Element member, Content linksTree) {
linksTree.add(writer.getDocLink(MEMBER, te, member, name(member)));
}
/**
* Add the parameter for the executable member.
*
* @param member the member to write parameter for.
* @param param the parameter that needs to be written.
* @param isVarArg true if this is a link to var arg.
* @param tree the content tree to which the parameter information will be added.
*/
protected void addParam(ExecutableElement member, VariableElement param, TypeMirror paramType,
boolean isVarArg, Content tree) {
Content link = writer.getLink(new LinkInfoImpl(configuration, EXECUTABLE_MEMBER_PARAM,
paramType).varargs(isVarArg));
tree.add(link);
if(name(param).length() > 0) {
tree.add(Entity.NO_BREAK_SPACE);
tree.add(name(param));
}
}
/**
* Add the receiver information.
*
* <p>Note: receivers can only have type-use annotations.</p>
*
* @param member the member to write receiver annotations for.
* @param rcvrType the receiver type.
* @param tree the content tree to which the information will be added.
*/
protected void addReceiver(ExecutableElement member, TypeMirror rcvrType, Content tree) {
var info = new LinkInfoImpl(configuration, RECEIVER_TYPE, rcvrType);
info.linkToSelf = false;
tree.add(writer.getLink(info));
tree.add(Entity.NO_BREAK_SPACE);
if (member.getKind() == ElementKind.CONSTRUCTOR) {
tree.add(utils.getTypeName(rcvrType, false));
tree.add(".");
}
tree.add("this");
}
/**
* Returns {@code true} if a receiver type is annotated anywhere in its type for
* inclusion in member details.
*
* @param receiverType the receiver type.
* @return {@code true} if the receiver is annotated
*/
protected boolean isAnnotatedReceiver(TypeMirror receiverType) {
return new SimpleTypeVisitor14<Boolean, Void>() {
@Override
protected Boolean defaultAction(TypeMirror e, Void unused) {
return utils.isAnnotated(e);
}
@Override
public Boolean visitDeclared(DeclaredType t, Void unused) {
if (super.visitDeclared(t, unused) || visit(t.getEnclosingType())) {
return true;
}
for (var e : t.getTypeArguments()) {
if (visit(e)) {
return true;
}
}
return false;
}
}.visit(receiverType);
}
/**
* Add all the parameters for the executable member.
*
* @param member the member to write parameters for.
* @param htmltree the content tree to which the parameters information will be added.
*/
protected void addParameters(ExecutableElement member, Content htmltree) {
Content paramTree = getParameters(member, false);
if (paramTree.charCount() > 2) {
// only add zero-width-space for non-empty parameters
htmltree.add(Entity.ZERO_WIDTH_SPACE);
}
htmltree.add(paramTree);
}
/**
* Add all the parameters for the executable member.
*
* @param member the member to write parameters for.
* @param includeAnnotations true if annotation information needs to be added.
* @return the content tree containing the parameter information
*/
protected Content getParameters(ExecutableElement member, boolean includeAnnotations) {
Content paramTree = new ContentBuilder();
paramTree.add("(");
String sep = "";
List<? extends VariableElement> parameters = member.getParameters();
TypeMirror rcvrType = member.getReceiverType();
if (includeAnnotations && rcvrType != null && isAnnotatedReceiver(rcvrType)) {
addReceiver(member, rcvrType, paramTree);
sep = "," + DocletConstants.NL + " ";
}
int paramstart;
ExecutableType instMeth = utils.asInstantiatedMethodType(typeElement, member);
for (paramstart = 0; paramstart < parameters.size(); paramstart++) {
paramTree.add(sep);
VariableElement param = parameters.get(paramstart);
TypeMirror paramType = instMeth.getParameterTypes().get(paramstart);
if (param.getKind() != ElementKind.INSTANCE_INIT) {
if (includeAnnotations) {
Content annotationInfo = writer.getAnnotationInfo(param, false);
if (!annotationInfo.isEmpty()) {
paramTree.add(annotationInfo)
.add(DocletConstants.NL)
.add(" ");
}
}
addParam(member, param, paramType,
(paramstart == parameters.size() - 1) && member.isVarArgs(), paramTree);
break;
}
}
for (int i = paramstart + 1; i < parameters.size(); i++) {
paramTree.add(",");
paramTree.add(DocletConstants.NL);
paramTree.add(" ");
if (includeAnnotations) {
Content annotationInfo = writer.getAnnotationInfo(parameters.get(i), false);
if (!annotationInfo.isEmpty()) {
paramTree.add(annotationInfo)
.add(DocletConstants.NL)
.add(" ");
}
}
addParam(member, parameters.get(i), instMeth.getParameterTypes().get(i),
(i == parameters.size() - 1) && member.isVarArgs(),
paramTree);
}
paramTree.add(")");
return paramTree;
}
/**
* Get a content tree containing the exception information for the executable member.
*
* @param member the member to write exceptions for.
* @return the content tree containing the exceptions information.
*/
protected Content getExceptions(ExecutableElement member) {
List<? extends TypeMirror> exceptions = utils.asInstantiatedMethodType(typeElement, member).getThrownTypes();
Content htmlTree = new ContentBuilder();
for (TypeMirror t : exceptions) {
if (!htmlTree.isEmpty()) {
htmlTree.add(",");
htmlTree.add(DocletConstants.NL);
}
Content link = writer.getLink(new LinkInfoImpl(configuration, THROWS_TYPE, t));
htmlTree.add(link);
}
return htmlTree;
}
protected TypeElement implementsMethodInIntfac(ExecutableElement method,
List<TypeElement> intfacs) {
for (TypeElement intf : intfacs) {
List<ExecutableElement> methods = utils.getMethods(intf);
if (!methods.isEmpty()) {
for (ExecutableElement md : methods) {
if (name(md).equals(name(method)) &&
md.toString().equals(method.toString())) {
return intf;
}
}
}
}
return null;
}
}