/
Signatures.java
474 lines (428 loc) · 19.8 KB
/
Signatures.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* Copyright (c) 2020, 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 jdk.javadoc.doclet.DocletEnvironment;
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.formats.html.markup.StringContent;
import jdk.javadoc.internal.doclets.formats.html.markup.TagName;
import jdk.javadoc.internal.doclets.toolkit.Content;
import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants;
import jdk.javadoc.internal.doclets.toolkit.util.Utils;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.RecordComponentElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import static javax.lang.model.element.Modifier.ABSTRACT;
import static javax.lang.model.element.Modifier.NATIVE;
import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.lang.model.element.Modifier.STRICTFP;
import static javax.lang.model.element.Modifier.SYNCHRONIZED;
public class Signatures {
public static Content getModuleSignature(ModuleElement mdle, ModuleWriterImpl moduleWriter) {
Content signature = HtmlTree.DIV(HtmlStyle.moduleSignature);
Content annotations = moduleWriter.getAnnotationInfo(mdle, true);
if (!annotations.isEmpty()) {
signature.add(HtmlTree.SPAN(HtmlStyle.annotations, annotations));
}
DocletEnvironment docEnv = moduleWriter.configuration.docEnv;
String label = mdle.isOpen() && (docEnv.getModuleMode() == DocletEnvironment.ModuleMode.ALL)
? "open module" : "module";
signature.add(label);
signature.add(" ");
HtmlTree nameSpan = new HtmlTree(TagName.SPAN).setStyle(HtmlStyle.elementName);
nameSpan.add(mdle.getQualifiedName().toString());
signature.add(nameSpan);
return signature;
}
public static Content getPackageSignature(PackageElement pkg, PackageWriterImpl pkgWriter) {
if (pkg.isUnnamed()) {
return new StringContent();
}
Content signature = HtmlTree.DIV(HtmlStyle.packageSignature);
Content annotations = pkgWriter.getAnnotationInfo(pkg, true);
if (!annotations.isEmpty()) {
signature.add(HtmlTree.SPAN(HtmlStyle.annotations, annotations));
}
signature.add("package ");
HtmlTree nameSpan = new HtmlTree(TagName.SPAN).setStyle(HtmlStyle.elementName);
nameSpan.add(pkg.getQualifiedName().toString());
signature.add(nameSpan);
return signature;
}
static class TypeSignature {
private final TypeElement typeElement;
private final ClassWriterImpl classWriter;
private final Utils utils;
private final HtmlConfiguration configuration;
private Content modifiers;
TypeSignature(TypeElement typeElement, ClassWriterImpl classWriter) {
this.typeElement = typeElement;
this.classWriter = classWriter;
this.utils = classWriter.utils;
this.configuration = classWriter.configuration;
}
public TypeSignature setModifiers(Content modifiers) {
this.modifiers = modifiers;
return this;
}
@SuppressWarnings("preview")
public Content toContent() {
Content content = new ContentBuilder();
Content annotationInfo = classWriter.getAnnotationInfo(typeElement, true);
if (!annotationInfo.isEmpty()) {
content.add(HtmlTree.SPAN(HtmlStyle.annotations, annotationInfo));
}
content.add(HtmlTree.SPAN(HtmlStyle.modifiers, modifiers));
HtmlTree nameSpan = new HtmlTree(TagName.SPAN).setStyle(HtmlStyle.elementName);
Content className = new StringContent(utils.getSimpleName(typeElement));
if (classWriter.options.linkSource()) {
classWriter.addSrcLink(typeElement, className, nameSpan);
} else {
nameSpan.addStyle(HtmlStyle.typeNameLabel).add(className);
}
HtmlLinkInfo linkInfo = new HtmlLinkInfo(configuration,
HtmlLinkInfo.Kind.CLASS_SIGNATURE, typeElement);
//Let's not link to ourselves in the signature.
linkInfo.linkToSelf = false;
nameSpan.add(classWriter.getTypeParameterLinks(linkInfo));
content.add(nameSpan);
if (utils.isRecord(typeElement)) {
content.add(getRecordComponents());
}
if (!utils.isAnnotationType(typeElement)) {
Content extendsImplements = new HtmlTree(TagName.SPAN)
.setStyle(HtmlStyle.extendsImplements);
if (!utils.isInterface(typeElement)) {
TypeMirror superclass = utils.getFirstVisibleSuperClass(typeElement);
if (superclass != null) {
content.add(DocletConstants.NL);
extendsImplements.add("extends ");
Content link = classWriter.getLink(new HtmlLinkInfo(configuration,
HtmlLinkInfo.Kind.CLASS_SIGNATURE_PARENT_NAME,
superclass));
extendsImplements.add(link);
}
}
List<? extends TypeMirror> interfaces = typeElement.getInterfaces();
if (!interfaces.isEmpty()) {
boolean isFirst = true;
for (TypeMirror type : interfaces) {
TypeElement tDoc = utils.asTypeElement(type);
if (!(utils.isPublic(tDoc) || utils.isLinkable(tDoc))) {
continue;
}
if (isFirst) {
extendsImplements.add(DocletConstants.NL);
extendsImplements.add(utils.isInterface(typeElement) ? "extends " : "implements ");
isFirst = false;
} else {
extendsImplements.add(", ");
}
Content link = classWriter.getLink(new HtmlLinkInfo(configuration,
HtmlLinkInfo.Kind.CLASS_SIGNATURE_PARENT_NAME,
type));
extendsImplements.add(link);
}
}
if (!extendsImplements.isEmpty()) {
content.add(extendsImplements);
}
}
List<? extends TypeMirror> permits = typeElement.getPermittedSubclasses();
List<? extends TypeMirror> linkablePermits = permits.stream()
.filter(t -> utils.isLinkable(utils.asTypeElement(t)))
.collect(Collectors.toList());
if (!linkablePermits.isEmpty()) {
Content permitsSpan = new HtmlTree(TagName.SPAN).setStyle(HtmlStyle.permits);
boolean isFirst = true;
for (TypeMirror type : linkablePermits) {
if (isFirst) {
content.add(DocletConstants.NL);
permitsSpan.add("permits");
Content link =
classWriter.links.createLink(classWriter.htmlIds.forPreviewSection(typeElement),
classWriter.contents.previewMark);
permitsSpan.add(HtmlTree.SUP(link));
permitsSpan.add(" ");
isFirst = false;
} else {
permitsSpan.add(", ");
}
Content link = classWriter.getLink(new HtmlLinkInfo(configuration,
HtmlLinkInfo.Kind.PERMITTED_SUBCLASSES,
type));
permitsSpan.add(link);
}
if (linkablePermits.size() < permits.size()) {
Content c = new StringContent(classWriter.resources.getText("doclet.not.exhaustive"));
permitsSpan.add(" ");
permitsSpan.add(HtmlTree.SPAN(HtmlStyle.permitsNote, c));
}
content.add(permitsSpan);
}
return HtmlTree.DIV(HtmlStyle.typeSignature, content);
}
@SuppressWarnings("preview")
private Content getRecordComponents() {
Content content = new ContentBuilder();
content.add("(");
String sep = "";
for (RecordComponentElement e : typeElement.getRecordComponents()) {
content.add(sep);
classWriter.getAnnotations(e.getAnnotationMirrors(), false)
.forEach(a -> { content.add(a).add(" "); });
Content link = classWriter.getLink(new HtmlLinkInfo(configuration, HtmlLinkInfo.Kind.RECORD_COMPONENT,
e.asType()));
content.add(link);
content.add(Entity.NO_BREAK_SPACE);
content.add(e.getSimpleName());
sep = ", ";
}
content.add(")");
return content;
}
}
/**
* A content builder for member signatures.
*/
static class MemberSignature {
private final AbstractMemberWriter memberWriter;
private final Utils utils;
private final Element element;
private Content annotations;
private Content typeParameters;
private Content returnType;
private Content parameters;
private Content exceptions;
// Threshold for length of type parameters before switching from inline to block representation.
private static final int TYPE_PARAMS_MAX_INLINE_LENGTH = 50;
// Threshold for combined length of modifiers, type params and return type before breaking
// it up with a line break before the return type.
private static final int RETURN_TYPE_MAX_LINE_LENGTH = 50;
/**
* Creates a new member signature builder.
*
* @param element the element for which to create a signature
* @param memberWriter the member writer
*/
MemberSignature(Element element, AbstractMemberWriter memberWriter) {
this.element = element;
this.memberWriter = memberWriter;
this.utils = memberWriter.utils;
}
/**
* Set the type parameters for an executable member.
*
* @param typeParameters the content tree containing the type parameters to add.
* @return this instance
*/
MemberSignature setTypeParameters(Content typeParameters) {
this.typeParameters = typeParameters;
return this;
}
/**
* Set the return type for an executable member.
*
* @param returnType the content tree containing the return type to add.
* @return this instance
*/
MemberSignature setReturnType(Content returnType) {
this.returnType = returnType;
return this;
}
/**
* Set the type information for a non-executable member.
*
* @param type the type of the member.
* @return this instance
*/
MemberSignature setType(TypeMirror type) {
this.returnType = memberWriter.writer.getLink(new HtmlLinkInfo(memberWriter.configuration, HtmlLinkInfo.Kind.MEMBER, type));
return this;
}
/**
* Set the parameter information of an executable member.
*
* @param paramTree the content tree containing the parameter information.
* @return this instance
*/
MemberSignature setParameters(Content paramTree) {
this.parameters = paramTree;
return this;
}
/**
* Set the exception information of an executable member.
*
* @param exceptionTree the content tree containing the exception information
* @return this instance
*/
MemberSignature setExceptions(Content exceptionTree) {
this.exceptions = exceptionTree;
return this;
}
/**
* Set the annotation information of a member.
*
* @param annotationTree the content tree containing the exception information
* @return this instance
*/
MemberSignature setAnnotations(Content annotationTree) {
this.annotations = annotationTree;
return this;
}
/**
* Returns an HTML tree containing the member signature.
*
* @return an HTML tree containing the member signature
*/
Content toContent() {
Content content = new ContentBuilder();
// Position of last line separator.
int lastLineSeparator = 0;
// Annotations
if (annotations != null && !annotations.isEmpty()) {
content.add(HtmlTree.SPAN(HtmlStyle.annotations, annotations));
lastLineSeparator = content.charCount();
}
// Modifiers
appendModifiers(content);
// Type parameters
if (typeParameters != null && !typeParameters.isEmpty()) {
lastLineSeparator = appendTypeParameters(content, lastLineSeparator);
}
// Return type
if (returnType != null) {
content.add(HtmlTree.SPAN(HtmlStyle.returnType, returnType));
content.add(Entity.NO_BREAK_SPACE);
}
// Name
HtmlTree nameSpan = new HtmlTree(TagName.SPAN).setStyle(HtmlStyle.elementName);
if (memberWriter.options.linkSource()) {
Content name = new StringContent(memberWriter.name(element));
memberWriter.writer.addSrcLink(element, name, nameSpan);
} else {
nameSpan.add(memberWriter.name(element));
}
content.add(nameSpan);
// Parameters and exceptions
if (parameters != null) {
appendParametersAndExceptions(content, lastLineSeparator);
}
return HtmlTree.DIV(HtmlStyle.memberSignature, content);
}
/**
* Adds the modifier for the member. The modifiers are ordered as specified
* by <em>The Java Language Specification</em>.
*
* @param htmlTree the content tree to which the modifier information will be added
*/
private void appendModifiers(Content htmlTree) {
Set<Modifier> set = new TreeSet<>(element.getModifiers());
// remove the ones we really don't need
set.remove(NATIVE);
set.remove(SYNCHRONIZED);
set.remove(STRICTFP);
// According to JLS, we should not be showing public modifier for
// interface methods and fields.
if ((utils.isField(element) || utils.isMethod(element))) {
Element te = element.getEnclosingElement();
if (utils.isInterface(te) || utils.isAnnotationType(te)) {
// Remove the implicit abstract and public modifiers
if (utils.isMethod(element)) {
set.remove(ABSTRACT);
}
set.remove(PUBLIC);
}
}
if (!set.isEmpty()) {
String mods = set.stream().map(Modifier::toString).collect(Collectors.joining(" "));
htmlTree.add(HtmlTree.SPAN(HtmlStyle.modifiers, new StringContent(mods)))
.add(Entity.NO_BREAK_SPACE);
}
}
/**
* Appends the type parameter information to the HTML tree.
*
* @param htmlTree the HTML tree
* @param lastLineSeparator index of last line separator in the HTML tree
* @return the new index of the last line separator
*/
private int appendTypeParameters(Content htmlTree, int lastLineSeparator) {
// Apply different wrapping strategies for type parameters
// depending of combined length of type parameters and return type.
int typeParamLength = typeParameters.charCount();
if (typeParamLength >= TYPE_PARAMS_MAX_INLINE_LENGTH) {
htmlTree.add(HtmlTree.SPAN(HtmlStyle.typeParametersLong, typeParameters));
} else {
htmlTree.add(HtmlTree.SPAN(HtmlStyle.typeParameters, typeParameters));
}
int lineLength = htmlTree.charCount() - lastLineSeparator;
int newLastLineSeparator = lastLineSeparator;
// sum below includes length of modifiers plus type params added above
if (lineLength + returnType.charCount() > RETURN_TYPE_MAX_LINE_LENGTH) {
htmlTree.add(DocletConstants.NL);
newLastLineSeparator = htmlTree.charCount();
} else {
htmlTree.add(Entity.NO_BREAK_SPACE);
}
return newLastLineSeparator;
}
/**
* Appends the parameters and exceptions information to the HTML tree.
*
* @param htmlTree the HTML tree
* @param lastLineSeparator the index of the last line separator in the HTML tree
*/
private void appendParametersAndExceptions(Content htmlTree, int lastLineSeparator) {
// Record current position for indentation of exceptions
int indentSize = htmlTree.charCount() - lastLineSeparator;
if (parameters.charCount() == 2) {
// empty parameters are added without packing
htmlTree.add(parameters);
} else {
htmlTree.add(Entity.ZERO_WIDTH_SPACE)
.add(HtmlTree.SPAN(HtmlStyle.parameters, parameters));
}
// Exceptions
if (exceptions != null && !exceptions.isEmpty()) {
CharSequence indent = " ".repeat(Math.max(0, indentSize + 1 - 7));
htmlTree.add(DocletConstants.NL)
.add(indent)
.add("throws ")
.add(HtmlTree.SPAN(HtmlStyle.exceptions, exceptions));
}
}
}
}