Skip to content

Commit 7778047

Browse files
committedOct 2, 2020
8253736: Cleanup some of WorkArounds and usage thereof
Reviewed-by: vromero, ksrini
1 parent 87d77eb commit 7778047

File tree

9 files changed

+268
-233
lines changed

9 files changed

+268
-233
lines changed
 

‎src/jdk.compiler/share/classes/com/sun/tools/doclint/DocLint.java

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
import com.sun.source.util.JavacTask;
3131
import com.sun.source.util.Plugin;
3232

33+
/**
34+
* The base class for the DocLint service used by javac.
35+
*
36+
* <p><b>This is NOT part of any supported API.
37+
* If you write code that depends on this, you do so at your own risk.
38+
* This code and its internal interfaces are subject to change or
39+
* deletion without notice.</b>
40+
*/
3341
public abstract class DocLint implements Plugin {
3442
public static final String XMSGS_OPTION = "-Xmsgs";
3543
public static final String XMSGS_CUSTOM_PREFIX = "-Xmsgs:";

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlConfiguration.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525

2626
package jdk.javadoc.internal.doclets.formats.html;
2727

28-
import java.util.*;
28+
import java.util.ArrayList;
29+
import java.util.Date;
30+
import java.util.HashMap;
31+
import java.util.List;
32+
import java.util.Locale;
33+
import java.util.Map;
2934
import java.util.stream.Collectors;
30-
3135
import javax.lang.model.element.Element;
3236
import javax.lang.model.element.PackageElement;
3337
import javax.lang.model.element.TypeElement;
@@ -36,7 +40,6 @@
3640
import javax.tools.StandardJavaFileManager;
3741

3842
import com.sun.source.util.DocTreePath;
39-
4043
import jdk.javadoc.doclet.Doclet;
4144
import jdk.javadoc.doclet.DocletEnvironment;
4245
import jdk.javadoc.doclet.Reporter;
@@ -201,7 +204,7 @@ public boolean finishOptionSettings() {
201204
docPaths = new DocPaths(utils);
202205
setCreateOverview();
203206
setTopFile(docEnv);
204-
workArounds.initDocLint(options.doclintOpts(), tagletManager.getAllTagletNames());
207+
initDocLint(options.doclintOpts(), tagletManager.getAllTagletNames());
205208
return true;
206209
}
207210

@@ -321,12 +324,12 @@ public JavaFileManager getFileManager() {
321324

322325
@Override
323326
public boolean showMessage(DocTreePath path, String key) {
324-
return (path == null || workArounds.haveDocLint());
327+
return (path == null || !haveDocLint());
325328
}
326329

327330
@Override
328331
public boolean showMessage(Element e, String key) {
329-
return (e == null || workArounds.haveDocLint());
332+
return (e == null || !haveDocLint());
330333
}
331334

332335
@Override

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/BaseConfiguration.java

+119-5
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,43 @@
2525

2626
package jdk.javadoc.internal.doclets.toolkit;
2727

28-
import java.io.*;
29-
import java.util.*;
3028

29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.util.ArrayList;
32+
import java.util.Collections;
33+
import java.util.HashMap;
34+
import java.util.HashSet;
35+
import java.util.LinkedHashSet;
36+
import java.util.List;
37+
import java.util.Locale;
38+
import java.util.Map;
39+
import java.util.Set;
40+
import java.util.SortedMap;
41+
import java.util.SortedSet;
42+
import java.util.TreeMap;
43+
import java.util.TreeSet;
44+
45+
import javax.lang.model.SourceVersion;
3146
import javax.lang.model.element.Element;
3247
import javax.lang.model.element.ModuleElement;
3348
import javax.lang.model.element.PackageElement;
3449
import javax.lang.model.element.TypeElement;
50+
import javax.lang.model.util.Elements;
3551
import javax.lang.model.util.SimpleElementVisitor14;
3652
import javax.tools.JavaFileManager;
3753
import javax.tools.JavaFileObject;
3854

55+
import com.sun.source.tree.CompilationUnitTree;
3956
import com.sun.source.util.DocTreePath;
57+
import com.sun.source.util.TreePath;
4058
import com.sun.tools.javac.util.DefinedBy;
4159
import com.sun.tools.javac.util.DefinedBy.Api;
4260
import jdk.javadoc.doclet.Doclet;
4361
import jdk.javadoc.doclet.DocletEnvironment;
4462
import jdk.javadoc.doclet.Reporter;
4563
import jdk.javadoc.doclet.StandardDoclet;
4664
import jdk.javadoc.doclet.Taglet;
47-
import jdk.javadoc.internal.doclets.formats.html.HtmlDoclet;
4865
import jdk.javadoc.internal.doclets.toolkit.builders.BuilderFactory;
4966
import jdk.javadoc.internal.doclets.toolkit.taglets.TagletManager;
5067
import jdk.javadoc.internal.doclets.toolkit.util.Comparators;
@@ -60,6 +77,7 @@
6077
import jdk.javadoc.internal.doclets.toolkit.util.Utils.Pair;
6178
import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberCache;
6279
import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable;
80+
import jdk.javadoc.internal.doclint.DocLint;
6381

6482
/**
6583
* Configure the output based on the options. Doclets should sub-class
@@ -201,7 +219,7 @@ public abstract class BaseConfiguration {
201219
* @apiNote The {@code doclet} parameter is used when
202220
* {@link Taglet#init(DocletEnvironment, Doclet) initializing tags}.
203221
* Some doclets (such as the {@link StandardDoclet}), may delegate to another
204-
* (such as the {@link HtmlDoclet}). In such cases, the primary doclet (i.e
222+
* (such as the {@code HtmlDoclet}). In such cases, the primary doclet (i.e
205223
* {@code StandardDoclet}) should be provided here, and not any internal
206224
* class like {@code HtmlDoclet}.
207225
*
@@ -367,7 +385,16 @@ protected boolean finishOptionSettings0() throws DocletException {
367385
group.checkPackageGroups(grp.first, grp.second);
368386
}
369387
});
370-
overviewElement = new OverviewElement(workArounds.getUnnamedPackage(), getOverviewPath());
388+
389+
PackageElement unnamedPackage;
390+
Elements elementUtils = utils.elementUtils;
391+
if (docEnv.getSourceVersion().compareTo(SourceVersion.RELEASE_9) >= 0) {
392+
ModuleElement unnamedModule = elementUtils.getModuleElement("");
393+
unnamedPackage = elementUtils.getPackageElement(unnamedModule, "");
394+
} else {
395+
unnamedPackage = elementUtils.getPackageElement("");
396+
}
397+
overviewElement = new OverviewElement(unnamedPackage, getOverviewPath());
371398
return true;
372399
}
373400

@@ -690,4 +717,91 @@ public boolean isJavaFXMode() {
690717
|| javafxModule.isUnnamed()
691718
|| javafxModule.getQualifiedName().contentEquals("javafx.base");
692719
}
720+
721+
722+
//<editor-fold desc="DocLint support">
723+
724+
private DocLint doclint;
725+
726+
Map<CompilationUnitTree, Boolean> shouldCheck = new HashMap<>();
727+
728+
public void runDocLint(TreePath path) {
729+
CompilationUnitTree unit = path.getCompilationUnit();
730+
if (doclint != null && shouldCheck.computeIfAbsent(unit, doclint::shouldCheck)) {
731+
doclint.scan(path);
732+
}
733+
}
734+
735+
/**
736+
* Initializes DocLint, if appropriate, depending on options derived
737+
* from the doclet command-line options, and the set of custom tags
738+
* that should be ignored by DocLint.
739+
*
740+
* DocLint is not enabled if the option {@code -Xmsgs:none} is given,
741+
* and it is not followed by any options to enable any groups.
742+
* Note that arguments for {@code -Xmsgs:} can be given individually
743+
* in separate {@code -Xmsgs:} options, or in a comma-separated list
744+
* for a single option. For example, the following are equivalent:
745+
* <ul>
746+
* <li>{@code -Xmsgs:all} {@code -Xmsgs:-html}
747+
* <li>{@code -Xmsgs:all,-html}
748+
* </ul>
749+
*
750+
* @param opts options for DocLint, derived from the corresponding doclet
751+
* command-line options
752+
* @param customTagNames the names of custom tags, to be ignored by doclint
753+
*/
754+
public void initDocLint(List<String> opts, Set<String> customTagNames) {
755+
List<String> doclintOpts = new ArrayList<>();
756+
757+
// basic analysis of -Xmsgs and -Xmsgs: options to see if doclint is enabled
758+
Set<String> groups = new HashSet<>();
759+
boolean seenXmsgs = false;
760+
for (String opt : opts) {
761+
if (opt.equals(DocLint.XMSGS_OPTION)) {
762+
groups.add("all");
763+
seenXmsgs = true;
764+
} else if (opt.startsWith(DocLint.XMSGS_CUSTOM_PREFIX)) {
765+
String[] args = opt.substring(DocLint.XMSGS_CUSTOM_PREFIX.length())
766+
.split(DocLint.SEPARATOR);
767+
for (String a : args) {
768+
if (a.equals("none")) {
769+
groups.clear();
770+
} else if (a.startsWith("-")) {
771+
groups.remove(a.substring(1));
772+
} else {
773+
groups.add(a);
774+
}
775+
}
776+
seenXmsgs = true;
777+
}
778+
doclintOpts.add(opt);
779+
}
780+
781+
if (seenXmsgs) {
782+
if (groups.isEmpty()) {
783+
// no groups enabled; do not init doclint
784+
return;
785+
}
786+
} else {
787+
// no -Xmsgs options of any kind, use default
788+
doclintOpts.add(DocLint.XMSGS_OPTION);
789+
}
790+
791+
if (!customTagNames.isEmpty()) {
792+
String customTags = String.join(DocLint.SEPARATOR, customTagNames);
793+
doclintOpts.add(DocLint.XCUSTOM_TAGS_PREFIX + customTags);
794+
}
795+
796+
doclintOpts.add(DocLint.XHTML_VERSION_PREFIX + "html5");
797+
798+
doclint = new DocLint();
799+
doclint.init(docEnv.getDocTrees(), docEnv.getElementUtils(), docEnv.getTypeUtils(),
800+
doclintOpts.toArray(new String[0]));
801+
}
802+
803+
public boolean haveDocLint() {
804+
return (doclint != null);
805+
}
806+
//</editor-fold>
693807
}

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/CommentUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,8 @@ public DocCommentInfo getHtmlCommentInfo(Element e) {
427427
}
428428
break;
429429
case PACKAGE:
430-
fo = configuration.workArounds.getJavaFileObject((PackageElement)e);
431-
pe = (PackageElement)e;
430+
pe = (PackageElement) e;
431+
fo = configuration.workArounds.getJavaFileObject(pe);
432432
break;
433433
default:
434434
return null;

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/Messages.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ public void warning(String key, Object... args) {
116116
* @param args optional arguments to be replaced in the message.
117117
*/
118118
public void warning(DocTreePath path, String key, Object... args) {
119-
if (configuration.showMessage(path, key))
119+
if (configuration.showMessage(path, key)) {
120120
report(WARNING, path, resources.getText(key, args));
121+
}
121122
}
122123

123124
/**

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/WorkArounds.java

+24-115
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import javax.lang.model.element.VariableElement;
4545
import javax.lang.model.type.TypeMirror;
4646
import javax.lang.model.util.Elements;
47+
import javax.lang.model.util.Types;
4748
import javax.tools.FileObject;
4849
import javax.tools.JavaFileManager.Location;
4950

@@ -94,110 +95,36 @@ public class WorkArounds {
9495
public final BaseConfiguration configuration;
9596
public final ToolEnvironment toolEnv;
9697
public final Utils utils;
97-
98-
private DocLint doclint;
98+
public final Elements elementUtils;
99+
public final Types typeUtils;
100+
public final com.sun.tools.javac.code.Types javacTypes;
99101

100102
public WorkArounds(BaseConfiguration configuration) {
101103
this.configuration = configuration;
102104
this.utils = this.configuration.utils;
103-
this.toolEnv = ((DocEnvImpl)this.configuration.docEnv).toolEnv;
104-
}
105-
106-
Map<CompilationUnitTree, Boolean> shouldCheck = new HashMap<>();
107-
// TODO: fix this up correctly
108-
public void runDocLint(TreePath path) {
109-
CompilationUnitTree unit = path.getCompilationUnit();
110-
if (doclint != null && shouldCheck.computeIfAbsent(unit, doclint::shouldCheck)) {
111-
doclint.scan(path);
112-
}
113-
}
114-
115-
/**
116-
* Initializes doclint, if appropriate, depending on options derived
117-
* from the doclet command-line options, and the set of custom tags
118-
* that should be ignored by doclint.
119-
*
120-
* DocLint is not enabled if the option {@code -Xmsgs:none} is given,
121-
* and it is not followed by any options to enable any groups.
122-
* Note that arguments for {@code -Xmsgs:} can be given individually
123-
* in separate {@code -Xmsgs:} options, or in a comma-separated list
124-
* for a single option. For example, the following are equivalent:
125-
* <ul>
126-
* <li>{@code -Xmsgs:all} {@code -Xmsgs:-html}
127-
* <li>{@code -Xmsgs:all,-html}
128-
* </ul>
129-
*
130-
* @param opts options for doclint, derived from the corresponding doclet
131-
* command-line options
132-
* @param customTagNames the names of custom tags, to be ignored by doclint
133-
*/
134-
public void initDocLint(List<String> opts, Set<String> customTagNames) {
135-
List<String> doclintOpts = new ArrayList<>();
136-
137-
// basic analysis of -Xmsgs and -Xmsgs: options to see if doclint is enabled
138-
Set<String> groups = new HashSet<>();
139-
boolean seenXmsgs = false;
140-
for (String opt : opts) {
141-
if (opt.equals(DocLint.XMSGS_OPTION)) {
142-
groups.add("all");
143-
seenXmsgs = true;
144-
} else if (opt.startsWith(DocLint.XMSGS_CUSTOM_PREFIX)) {
145-
String[] args = opt.substring(DocLint.XMSGS_CUSTOM_PREFIX.length())
146-
.split(DocLint.SEPARATOR);
147-
for (String a : args) {
148-
if (a.equals("none")) {
149-
groups.clear();
150-
} else if (a.startsWith("-")) {
151-
groups.remove(a.substring(1));
152-
} else {
153-
groups.add(a);
154-
}
155-
}
156-
seenXmsgs = true;
157-
}
158-
doclintOpts.add(opt);
159-
}
160-
161-
if (seenXmsgs) {
162-
if (groups.isEmpty()) {
163-
// no groups enabled; do not init doclint
164-
return;
165-
}
166-
} else {
167-
// no -Xmsgs options of any kind, use default
168-
doclintOpts.add(DocLint.XMSGS_OPTION);
169-
}
170-
171-
if (!customTagNames.isEmpty()) {
172-
String customTags = String.join(DocLint.SEPARATOR, customTagNames);
173-
doclintOpts.add(DocLint.XCUSTOM_TAGS_PREFIX + customTags);
174-
}
175-
176-
doclintOpts.add(DocLint.XHTML_VERSION_PREFIX + "html5");
177105

178-
JavacTask t = BasicJavacTask.instance(toolEnv.context);
179-
doclint = new DocLint();
180-
doclint.init(t, doclintOpts.toArray(new String[0]), false);
181-
}
106+
elementUtils = configuration.docEnv.getElementUtils();
107+
typeUtils = configuration.docEnv.getTypeUtils();
182108

183-
// TODO: fix this up correctly
184-
public boolean haveDocLint() {
185-
return (doclint == null);
109+
// Note: this one use of DocEnvImpl is what prevents us tunnelling extra
110+
// info from a doclet to its taglets via a doclet-specific subtype of
111+
// DocletEnvironment.
112+
toolEnv = ((DocEnvImpl)this.configuration.docEnv).toolEnv;
113+
javacTypes = toolEnv.getTypes();
186114
}
187115

188116
/*
189117
* TODO: This method exists because of a bug in javac which does not
190-
* handle "@deprecated tag in package-info.java", when this issue
191-
* is fixed this method and its uses must be jettisoned.
118+
* handle "@deprecated tag in package-info.java", when this issue
119+
* is fixed this method and its uses must be jettisoned.
192120
*/
193121
public boolean isDeprecated0(Element e) {
194122
if (!utils.getDeprecatedTrees(e).isEmpty()) {
195123
return true;
196124
}
197-
JavacTypes jctypes = ((DocEnvImpl)configuration.docEnv).toolEnv.typeutils;
198125
TypeMirror deprecatedType = utils.getDeprecatedType();
199126
for (AnnotationMirror anno : e.getAnnotationMirrors()) {
200-
if (jctypes.isSameType(anno.getAnnotationType().asElement().asType(), deprecatedType))
127+
if (typeUtils.isSameType(anno.getAnnotationType().asElement().asType(), deprecatedType))
201128
return true;
202129
}
203130
return false;
@@ -208,24 +135,11 @@ public boolean isSynthesized(AnnotationMirror aDesc) {
208135
return ((Attribute)aDesc).isSynthesized();
209136
}
210137

211-
// TODO: fix the caller
212-
public Object getConstValue(VariableElement ve) {
213-
return ((VarSymbol)ve).getConstValue();
214-
}
215-
216138
// TODO: DocTrees: Trees.getPath(Element e) is slow a factor 4-5 times.
217139
public Map<Element, TreePath> getElementToTreePath() {
218140
return toolEnv.elementToTreePath;
219141
}
220142

221-
// TODO: we need ElementUtils.getPackage to cope with input strings
222-
// to return the proper unnamedPackage for all supported releases.
223-
PackageElement getUnnamedPackage() {
224-
return (Feature.MODULES.allowedInSource(toolEnv.source))
225-
? toolEnv.syms.unnamedModule.unnamedPackage
226-
: toolEnv.syms.noModule.unnamedPackage;
227-
}
228-
229143
// TODO: implement in either jx.l.m API (preferred) or DocletEnvironment.
230144
FileObject getJavaFileObject(PackageElement packageElement) {
231145
return ((PackageSymbol)packageElement).sourcefile;
@@ -238,7 +152,7 @@ public TypeElement searchClass(TypeElement klass, String className) {
238152
// search by qualified name in current module first
239153
ModuleElement me = utils.containingModule(klass);
240154
if (me != null) {
241-
te = configuration.docEnv.getElementUtils().getTypeElement(me, className);
155+
te = elementUtils.getTypeElement(me, className);
242156
if (te != null) {
243157
return te;
244158
}
@@ -290,17 +204,12 @@ public TypeElement searchClass(TypeElement klass, String className) {
290204
}
291205

292206
// finally, search by qualified name in all modules
293-
te = configuration.docEnv.getElementUtils().getTypeElement(className);
294-
if (te != null) {
295-
return te;
296-
}
297-
298-
return null; // not found
207+
return elementUtils.getTypeElement(className);
299208
}
300209

301210
// TODO: need to re-implement this using j.l.m. correctly!, this has
302-
// implications on testInterface, the note here is that javac's supertype
303-
// does the right thing returning Parameters in scope.
211+
// implications on testInterface, the note here is that javac's supertype
212+
// does the right thing returning Parameters in scope.
304213
/**
305214
* Return the type containing the method that this method overrides.
306215
* It may be a <code>TypeElement</code> or a <code>TypeParameterElement</code>.
@@ -311,14 +220,14 @@ public TypeMirror overriddenType(ExecutableElement method) {
311220
if (utils.isStatic(method)) {
312221
return null;
313222
}
314-
MethodSymbol sym = (MethodSymbol)method;
223+
MethodSymbol sym = (MethodSymbol) method;
315224
ClassSymbol origin = (ClassSymbol) sym.owner;
316-
for (com.sun.tools.javac.code.Type t = toolEnv.getTypes().supertype(origin.type);
225+
for (com.sun.tools.javac.code.Type t = javacTypes.supertype(origin.type);
317226
t.hasTag(TypeTag.CLASS);
318-
t = toolEnv.getTypes().supertype(t)) {
227+
t = javacTypes.supertype(t)) {
319228
ClassSymbol c = (ClassSymbol) t.tsym;
320229
for (com.sun.tools.javac.code.Symbol sym2 : c.members().getSymbolsByName(sym.name)) {
321-
if (sym.overrides(sym2, origin, toolEnv.getTypes(), true)) {
230+
if (sym.overrides(sym2, origin, javacTypes, true)) {
322231
// Ignore those methods that may be a simple override
323232
// and allow the real API method to be found.
324233
if (sym2.type.hasTag(TypeTag.METHOD) &&
@@ -353,10 +262,10 @@ public boolean overrides(ExecutableElement e1, ExecutableElement e2, TypeElement
353262
!rider.isStatic() &&
354263

355264
// Symbol.overrides assumes the following
356-
ridee.isMemberOf(origin, toolEnv.getTypes()) &&
265+
ridee.isMemberOf(origin, javacTypes) &&
357266

358267
// check access, signatures and check return types
359-
rider.overrides(ridee, origin, toolEnv.getTypes(), true);
268+
rider.overrides(ridee, origin, javacTypes, true);
360269
}
361270

362271
// TODO: jx.l.m ?

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Utils.java

+66-80
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@
107107
import com.sun.tools.javac.model.JavacTypes;
108108
import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
109109
import jdk.javadoc.internal.doclets.toolkit.BaseOptions;
110+
import jdk.javadoc.internal.doclets.toolkit.CommentUtils;
110111
import jdk.javadoc.internal.doclets.toolkit.CommentUtils.DocCommentInfo;
111112
import jdk.javadoc.internal.doclets.toolkit.Resources;
112-
import jdk.javadoc.internal.doclets.toolkit.WorkArounds;
113113
import jdk.javadoc.internal.doclets.toolkit.taglets.BaseTaglet;
114114
import jdk.javadoc.internal.doclets.toolkit.taglets.Taglet;
115115
import jdk.javadoc.internal.tool.DocEnvImpl;
@@ -2271,60 +2271,56 @@ public TypeElement getEnclosingTypeElement(Element e) {
22712271
public String constantValueExpression(VariableElement ve) {
22722272
if (cve == null)
22732273
cve = new ConstantValueExpression();
2274-
return cve.constantValueExpression(configuration.workArounds, ve);
2275-
}
2276-
2277-
private static class ConstantValueExpression {
2278-
public String constantValueExpression(WorkArounds workArounds, VariableElement ve) {
2279-
return new TypeKindVisitor9<String, Object>() {
2280-
/* TODO: we need to fix this correctly.
2281-
* we have a discrepancy here, note the use of getConstValue
2282-
* vs. getConstantValue, at some point we need to use
2283-
* getConstantValue.
2284-
* In the legacy world byte and char primitives appear as Integer values,
2285-
* thus a byte value of 127 will appear as 127, but in the new world,
2286-
* a byte value appears as Byte thus 0x7f will be printed, similarly
2287-
* chars will be translated to \n, \r etc. however, in the new world,
2288-
* they will be printed as decimal values. The new world is correct,
2289-
* and we should fix this by using getConstantValue and the visitor to
2290-
* address this in the future.
2291-
*/
2292-
@Override
2293-
public String visitPrimitiveAsBoolean(PrimitiveType t, Object val) {
2294-
return (int)val == 0 ? "false" : "true";
2295-
}
2274+
return cve.visit(ve.asType(), ve.getConstantValue());
2275+
}
22962276

2297-
@Override
2298-
public String visitPrimitiveAsDouble(PrimitiveType t, Object val) {
2299-
return sourceForm(((Double)val), 'd');
2300-
}
2277+
// We could also use Elements.getConstantValueExpression, which provides
2278+
// similar functionality, but which also includes casts to provide valid
2279+
// compilable constants: e.g. (byte) 0x7f
2280+
private static class ConstantValueExpression extends TypeKindVisitor9<String, Object> {
2281+
@Override
2282+
public String visitPrimitiveAsBoolean(PrimitiveType t, Object val) {
2283+
return ((boolean) val) ? "true" : "false";
2284+
}
23012285

2302-
@Override
2303-
public String visitPrimitiveAsFloat(PrimitiveType t, Object val) {
2304-
return sourceForm(((Float)val).doubleValue(), 'f');
2305-
}
2286+
@Override
2287+
public String visitPrimitiveAsByte(PrimitiveType t, Object val) {
2288+
return "0x" + Integer.toString(((Byte) val) & 0xff, 16);
2289+
}
23062290

2307-
@Override
2308-
public String visitPrimitiveAsLong(PrimitiveType t, Object val) {
2309-
return val + "L";
2310-
}
2291+
@Override
2292+
public String visitPrimitiveAsChar(PrimitiveType t, Object val) {
2293+
StringBuilder buf = new StringBuilder(8);
2294+
buf.append('\'');
2295+
sourceChar((char) val, buf);
2296+
buf.append('\'');
2297+
return buf.toString();
2298+
}
23112299

2312-
@Override
2313-
protected String defaultAction(TypeMirror e, Object val) {
2314-
if (val == null)
2315-
return null;
2316-
else if (val instanceof Character)
2317-
return sourceForm(((Character)val));
2318-
else if (val instanceof Byte)
2319-
return sourceForm(((Byte)val));
2320-
else if (val instanceof String)
2321-
return sourceForm((String)val);
2322-
return val.toString(); // covers int, short
2323-
}
2324-
}.visit(ve.asType(), workArounds.getConstValue(ve));
2300+
@Override
2301+
public String visitPrimitiveAsDouble(PrimitiveType t, Object val) {
2302+
return sourceForm(((Double) val), 'd');
2303+
}
2304+
2305+
@Override
2306+
public String visitPrimitiveAsFloat(PrimitiveType t, Object val) {
2307+
return sourceForm(((Float) val).doubleValue(), 'f');
2308+
}
2309+
2310+
@Override
2311+
public String visitPrimitiveAsLong(PrimitiveType t, Object val) {
2312+
return val + "L";
2313+
}
2314+
2315+
@Override
2316+
protected String defaultAction(TypeMirror e, Object val) {
2317+
if (val == null)
2318+
return null;
2319+
else if (val instanceof String)
2320+
return sourceForm((String) val);
2321+
return val.toString(); // covers int, short
23252322
}
23262323

2327-
// where
23282324
private String sourceForm(double v, char suffix) {
23292325
if (Double.isNaN(v))
23302326
return "0" + suffix + "/0" + suffix;
@@ -2335,22 +2331,10 @@ private String sourceForm(double v, char suffix) {
23352331
return v + (suffix == 'f' || suffix == 'F' ? "" + suffix : "");
23362332
}
23372333

2338-
private String sourceForm(char c) {
2339-
StringBuilder buf = new StringBuilder(8);
2340-
buf.append('\'');
2341-
sourceChar(c, buf);
2342-
buf.append('\'');
2343-
return buf.toString();
2344-
}
2345-
2346-
private String sourceForm(byte c) {
2347-
return "0x" + Integer.toString(c & 0xff, 16);
2348-
}
2349-
23502334
private String sourceForm(String s) {
23512335
StringBuilder buf = new StringBuilder(s.length() + 5);
23522336
buf.append('\"');
2353-
for (int i=0; i<s.length(); i++) {
2337+
for (int i = 0; i < s.length(); i++) {
23542338
char c = s.charAt(i);
23552339
sourceChar(c, buf);
23562340
}
@@ -2360,31 +2344,33 @@ private String sourceForm(String s) {
23602344

23612345
private void sourceChar(char c, StringBuilder buf) {
23622346
switch (c) {
2363-
case '\b': buf.append("\\b"); return;
2364-
case '\t': buf.append("\\t"); return;
2365-
case '\n': buf.append("\\n"); return;
2366-
case '\f': buf.append("\\f"); return;
2367-
case '\r': buf.append("\\r"); return;
2368-
case '\"': buf.append("\\\""); return;
2369-
case '\'': buf.append("\\\'"); return;
2370-
case '\\': buf.append("\\\\"); return;
2371-
default:
2372-
if (isPrintableAscii(c)) {
2373-
buf.append(c); return;
2347+
case '\b' -> buf.append("\\b");
2348+
case '\t' -> buf.append("\\t");
2349+
case '\n' -> buf.append("\\n");
2350+
case '\f' -> buf.append("\\f");
2351+
case '\r' -> buf.append("\\r");
2352+
case '\"' -> buf.append("\\\"");
2353+
case '\'' -> buf.append("\\\'");
2354+
case '\\' -> buf.append("\\\\");
2355+
default -> {
2356+
if (isPrintableAscii(c)) {
2357+
buf.append(c);
2358+
return;
2359+
}
2360+
unicodeEscape(c, buf);
23742361
}
2375-
unicodeEscape(c, buf);
2376-
return;
23772362
}
23782363
}
23792364

23802365
private void unicodeEscape(char c, StringBuilder buf) {
23812366
final String chars = "0123456789abcdef";
23822367
buf.append("\\u");
2383-
buf.append(chars.charAt(15 & (c>>12)));
2384-
buf.append(chars.charAt(15 & (c>>8)));
2385-
buf.append(chars.charAt(15 & (c>>4)));
2386-
buf.append(chars.charAt(15 & (c>>0)));
2368+
buf.append(chars.charAt(15 & (c >> 12)));
2369+
buf.append(chars.charAt(15 & (c >> 8)));
2370+
buf.append(chars.charAt(15 & (c >> 4)));
2371+
buf.append(chars.charAt(15 & (c >> 0)));
23872372
}
2373+
23882374
private boolean isPrintableAscii(char c) {
23892375
return c >= ' ' && c <= '~';
23902376
}
@@ -2722,7 +2708,7 @@ public DocCommentTree getDocCommentTree0(Element element) {
27222708
}
27232709
}
27242710
// run doclint even if docCommentTree is null, to trigger checks for missing comments
2725-
configuration.workArounds.runDocLint(path);
2711+
configuration.runDocLint(path);
27262712
}
27272713
dcTreeCache.put(element, info);
27282714
}

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclint/DocLint.java

+35-21
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.util.Queue;
3535

3636
import javax.lang.model.element.Name;
37+
import javax.lang.model.util.Elements;
38+
import javax.lang.model.util.Types;
3739
import javax.tools.StandardLocation;
3840

3941
import com.sun.source.doctree.DocCommentTree;
@@ -46,8 +48,8 @@
4648
import com.sun.source.tree.MethodTree;
4749
import com.sun.source.tree.Tree;
4850
import com.sun.source.tree.VariableTree;
51+
import com.sun.source.util.DocTrees;
4952
import com.sun.source.util.JavacTask;
50-
import com.sun.source.util.Plugin;
5153
import com.sun.source.util.TaskEvent;
5254
import com.sun.source.util.TaskListener;
5355
import com.sun.source.util.TreePath;
@@ -283,27 +285,8 @@ public void init(JavacTask task, String... args) {
283285

284286
public void init(JavacTask task, String[] args, boolean addTaskListener) {
285287
env = new Env();
286-
for (String arg : args) {
287-
if (arg.equals(XMSGS_OPTION)) {
288-
env.messages.setOptions(null);
289-
} else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) {
290-
env.messages.setOptions(arg.substring(arg.indexOf(":") + 1));
291-
} else if (arg.startsWith(XCUSTOM_TAGS_PREFIX)) {
292-
env.setCustomTags(arg.substring(arg.indexOf(":") + 1));
293-
} else if (arg.startsWith(XHTML_VERSION_PREFIX)) {
294-
String argsVersion = arg.substring(arg.indexOf(":") + 1);
295-
HtmlVersion htmlVersion = HtmlVersion.getHtmlVersion(argsVersion);
296-
if (htmlVersion != null) {
297-
env.setHtmlVersion(htmlVersion);
298-
} else {
299-
throw new IllegalArgumentException(argsVersion);
300-
}
301-
} else if (arg.startsWith(XCHECK_PACKAGE)) {
302-
env.setCheckPackages(arg.substring(arg.indexOf(":") + 1));
303-
} else
304-
throw new IllegalArgumentException(arg);
305-
}
306288
env.init(task);
289+
processArgs(env, args);
307290

308291
checker = new Checker(env);
309292

@@ -346,6 +329,37 @@ public void finished(TaskEvent e) {
346329
}
347330
}
348331

332+
public void init(DocTrees trees, Elements elements, Types types, String... args) {
333+
env = new Env();
334+
env.init(trees, elements, types);
335+
processArgs(env, args);
336+
337+
checker = new Checker(env);
338+
}
339+
340+
private void processArgs(Env env, String... args) {
341+
for (String arg : args) {
342+
if (arg.equals(XMSGS_OPTION)) {
343+
env.messages.setOptions(null);
344+
} else if (arg.startsWith(XMSGS_CUSTOM_PREFIX)) {
345+
env.messages.setOptions(arg.substring(arg.indexOf(":") + 1));
346+
} else if (arg.startsWith(XCUSTOM_TAGS_PREFIX)) {
347+
env.setCustomTags(arg.substring(arg.indexOf(":") + 1));
348+
} else if (arg.startsWith(XHTML_VERSION_PREFIX)) {
349+
String argsVersion = arg.substring(arg.indexOf(":") + 1);
350+
HtmlVersion htmlVersion = HtmlVersion.getHtmlVersion(argsVersion);
351+
if (htmlVersion != null) {
352+
env.setHtmlVersion(htmlVersion);
353+
} else {
354+
throw new IllegalArgumentException(argsVersion);
355+
}
356+
} else if (arg.startsWith(XCHECK_PACKAGE)) {
357+
env.setCheckPackages(arg.substring(arg.indexOf(":") + 1));
358+
} else
359+
throw new IllegalArgumentException(arg);
360+
}
361+
}
362+
349363
public void scan(TreePath p) {
350364
DocCommentTree dc = env.trees.getDocCommentTree(p);
351365
checker.scan(dc, p);

‎test/langtools/jdk/javadoc/doclet/constantValues/TestConstantValuesDriver.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ public void test() {
5757
"""
5858
<code id="TestConstantValues.BYTE_MAX_VALUE">public&nbsp;static&nbsp;final&nbsp;byte</code></td>
5959
<th class="col-second" scope="row"><code><a href="TestConstantValues.html#BYTE_MAX_VALUE">BYTE_MAX_VALUE</a></code></th>
60-
<td class="col-last"><code>127</code></td>""",
60+
<td class="col-last"><code>0x7f</code></td>""",
6161
"""
6262
<code id="TestConstantValues.BYTE_MIN_VALUE">public&nbsp;static&nbsp;final&nbsp;byte</code></td>
6363
<th class="col-second" scope="row"><code><a href="TestConstantValues.html#BYTE_MIN_VALUE">BYTE_MIN_VALUE</a></code></th>
64-
<td class="col-last"><code>-127</code></td>""",
64+
<td class="col-last"><code>0x81</code></td>""",
6565
"""
6666
<code id="TestConstantValues.CHAR_MAX_VALUE">public&nbsp;static&nbsp;final&nbsp;char</code></td>
6767
<th class="col-second" scope="row"><code><a href="TestConstantValues.html#CHAR_MAX_VALUE">CHAR_MAX_VALUE</a></code></th>
68-
<td class="col-last"><code>65535</code></td>""",
68+
<td class="col-last"><code>'\\uffff'</code></td>""",
6969
"""
7070
<code id="TestConstantValues.DOUBLE_MAX_VALUE">public&nbsp;static&nbsp;final&nbsp;double</code></td>""",
7171
"""

0 commit comments

Comments
 (0)
Please sign in to comment.