Skip to content
Permalink
Browse files
8267329: Modernize Javadoc code to use instanceof with pattern matching
Reviewed-by: prappo, jjg
  • Loading branch information
Ian Graves committed May 24, 2021
1 parent 209769b commit ebc9357d58957702abbd003d21082badc630876d
Showing with 88 additions and 99 deletions.
  1. +5 −5 src/jdk.javadoc/share/classes/jdk/javadoc/internal/api/JavadocTool.java
  2. +2 −2 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/ClassWriterImpl.java
  3. +4 −4 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/Contents.java
  4. +1 −2 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlConfiguration.java
  5. +7 −7 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java
  6. +2 −2 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/SourceToHTMLConverter.java
  7. +1 −2 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/SystemPropertiesWriter.java
  8. +3 −4 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/TagletWriterImpl.java
  9. +4 −4 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/markup/ContentBuilder.java
  10. +2 −2 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/markup/HtmlTree.java
  11. +3 −4 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/CommentUtils.java
  12. +4 −4 ...jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/builders/MemberSummaryBuilder.java
  13. +4 −6 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/taglets/TagletManager.java
  14. +3 −4 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/taglets/TagletWriter.java
  15. +3 −3 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/taglets/ThrowsTaglet.java
  16. +2 −2 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/DocPath.java
  17. +7 −9 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Extern.java
  18. +8 −8 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Utils.java
  19. +4 −4 src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclint/Checker.java
  20. +1 −2 src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/ElementsTable.java
  21. +3 −3 src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/JavadocTool.java
  22. +3 −3 src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Messager.java
  23. +9 −10 src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java
  24. +3 −3 src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/ToolEnvironment.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 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
@@ -113,15 +113,15 @@ public DocumentationTask getTask(

if (out == null)
context.put(Log.errKey, new PrintWriter(System.err, true));
else if (out instanceof PrintWriter)
context.put(Log.errKey, ((PrintWriter) out));
else if (out instanceof PrintWriter pout)
context.put(Log.errKey, pout);
else
context.put(Log.errKey, new PrintWriter(out, true));

if (fileManager == null) {
fileManager = getStandardFileManager(diagnosticListener, null, null);
if (fileManager instanceof BaseFileManager) {
((BaseFileManager) fileManager).autoClose = true;
if (fileManager instanceof BaseFileManager bfm) {
bfm.autoClose = true;
}
}
fileManager = ccw.wrap(fileManager);
@@ -470,9 +470,9 @@ private Content getClassLinks(HtmlLinkInfo.Kind context, Collection<?> list) {
isFirst = false;
}
// TODO: should we simply split this method up to avoid instanceof ?
if (type instanceof TypeElement) {
if (type instanceof TypeElement te) {
Content link = getLink(
new HtmlLinkInfo(configuration, context, (TypeElement)(type)));
new HtmlLinkInfo(configuration, context, te));
content.add(HtmlTree.CODE(link));
} else {
Content link = getLink(
@@ -407,10 +407,10 @@ public Content getContent(String key, Object o0, Object o1, Object o2) {

if (o == null) {
c.add("{" + m.group(1) + "}");
} else if (o instanceof String) {
c.add((String) o);
} else if (o instanceof Content) {
c.add((Content) o);
} else if (o instanceof String str) {
c.add(str);
} else if (o instanceof Content con) {
c.add(con);
}

start = m.end();
@@ -354,8 +354,7 @@ public Locale getLocale() {
@Override
public JavaFileObject getOverviewPath() {
String overviewpath = options.overviewPath();
if (overviewpath != null && getFileManager() instanceof StandardJavaFileManager) {
StandardJavaFileManager fm = (StandardJavaFileManager) getFileManager();
if (overviewpath != null && getFileManager() instanceof StandardJavaFileManager fm) {
return fm.getJavaFileObjects(overviewpath).iterator().next();
}
return null;
@@ -1123,8 +1123,8 @@ public Content seeTagToContent(Element element, DocTree see, TagletWriterImpl.Co
// documented, this must be an inherited link. Redirect it.
// The current class either overrides the referenced member or
// inherits it automatically.
if (this instanceof ClassWriterImpl) {
containing = ((ClassWriterImpl) this).getTypeElement();
if (this instanceof ClassWriterImpl writer) {
containing = writer.getTypeElement();
} else if (!utils.isPublic(containing)) {
messages.warning(
ch.getDocTreePath(see), "doclet.see.class_or_package_not_accessible",
@@ -2128,11 +2128,11 @@ Script getMainBodyScript() {
List<DocPath> getLocalStylesheets(Element element) throws DocFileIOException {
List<DocPath> stylesheets = new ArrayList<>();
DocPath basePath = null;
if (element instanceof PackageElement) {
stylesheets.addAll(getModuleStylesheets((PackageElement)element));
basePath = docPaths.forPackage((PackageElement)element);
} else if (element instanceof ModuleElement) {
basePath = DocPaths.forModule((ModuleElement)element);
if (element instanceof PackageElement pkg) {
stylesheets.addAll(getModuleStylesheets(pkg));
basePath = docPaths.forPackage(pkg);
} else if (element instanceof ModuleElement mdle) {
basePath = DocPaths.forModule(mdle);
}
for (DocPath stylesheet : getStylesheets(element)) {
stylesheets.add(basePath.resolve(stylesheet.getPath()));
@@ -175,9 +175,9 @@ public void convertModule(ModuleElement mdl, DocPath outputdir)
return;
}
for (Element elem : mdl.getEnclosedElements()) {
if (elem instanceof PackageElement && configuration.docEnv.isIncluded(elem)
if (elem instanceof PackageElement pkg && configuration.docEnv.isIncluded(elem)
&& !(options.noDeprecated() && utils.isDeprecated(elem))) {
convertPackage((PackageElement) elem, outputdir);
convertPackage(pkg, outputdir);
}
}
}
@@ -155,8 +155,7 @@ private Content createLink(IndexItem i) {
if (element instanceof OverviewElement) {
return links.createLink(pathToRoot.resolve(i.getUrl()),
resources.getText("doclet.Overview"));
} else if (element instanceof DocletElement) {
DocletElement e = (DocletElement) element;
} else if (element instanceof DocletElement e) {
// Implementations of DocletElement do not override equals and
// hashCode; putting instances of DocletElement in a map is not
// incorrect, but might well be inefficient
@@ -318,12 +318,12 @@ public Content seeTagOutput(Element holder, List<? extends SeeTree> seeTags) {
links.add(htmlWriter.seeTagToContent(holder, dt, context.within(dt)));
}
if (utils.isVariableElement(holder) && ((VariableElement)holder).getConstantValue() != null &&
htmlWriter instanceof ClassWriterImpl) {
htmlWriter instanceof ClassWriterImpl writer) {
//Automatically add link to constant values page for constant fields.
DocPath constantsPath =
htmlWriter.pathToRoot.resolve(DocPaths.CONSTANT_VALUES);
String whichConstant =
((ClassWriterImpl) htmlWriter).getTypeElement().getQualifiedName() + "." +
writer.getTypeElement().getQualifiedName() + "." +
utils.getSimpleName(holder);
DocLink link = constantsPath.fragment(whichConstant);
links.add(htmlWriter.links.createLink(link,
@@ -503,8 +503,7 @@ public String visitVariable(VariableElement e, Void p) {

@Override
public String visitUnknown(Element e, Void p) {
if (e instanceof DocletElement) {
DocletElement de = (DocletElement) e;
if (e instanceof DocletElement de) {
return switch (de.getSubKind()) {
case OVERVIEW -> resources.getText("doclet.Overview");
case DOCFILE -> getHolderName(de);
@@ -52,8 +52,8 @@ public ContentBuilder(Content... contents) {
public ContentBuilder add(Content content) {
Objects.requireNonNull(content);
ensureMutableContents();
if (content instanceof ContentBuilder) {
contents.addAll(((ContentBuilder) content).contents);
if (content instanceof ContentBuilder cb) {
contents.addAll(cb.contents);
} else
contents.add(content);
return this;
@@ -65,8 +65,8 @@ public ContentBuilder add(CharSequence text) {
ensureMutableContents();
Content c = contents.isEmpty() ? null : contents.get(contents.size() - 1);
TextBuilder tb;
if (c != null && c instanceof TextBuilder) {
tb = (TextBuilder) c;
if (c instanceof TextBuilder tbi) {
tb = tbi;
} else {
contents.add(tb = new TextBuilder());
}
@@ -174,8 +174,8 @@ public HtmlTree addStyle(String style) {
*/
@Override
public HtmlTree add(Content content) {
if (content instanceof ContentBuilder) {
((ContentBuilder) content).contents.forEach(this::add);
if (content instanceof ContentBuilder cb) {
cb.contents.forEach(this::add);
}
else if (content == HtmlTree.EMPTY || content.isValid()) {
// quietly avoid adding empty or invalid nodes (except EMPTY)
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 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
@@ -368,7 +368,7 @@ private boolean hasParamForComponent(TypeElement elem, Name component) {
}

for (DocTree t : elemComment.getBlockTags()) {
if (t instanceof ParamTree && ((ParamTree) t).getName().getName() == component) {
if (t instanceof ParamTree pt && pt.getName().getName() == component) {
return true;
}
}
@@ -419,8 +419,7 @@ public DocCommentInfo getHtmlCommentInfo(Element e) {
PackageElement pe = null;
switch (e.getKind()) {
case OTHER:
if (e instanceof DocletElement) {
DocletElement de = (DocletElement)e;
if (e instanceof DocletElement de) {
fo = de.getFileObject();
pe = de.getPackageElement();
}
@@ -316,8 +316,8 @@ private void processProperty(Element member,
fullBody.addAll(cmtutils.makeFirstSentenceTree(text));
}
List<? extends DocTree> propertyTags = utils.getBlockTags(property,
t -> (t instanceof UnknownBlockTagTree)
&& ((UnknownBlockTagTree) t).getTagName().equals("propertyDescription"));
t -> (t instanceof UnknownBlockTagTree tree)
&& (tree.getTagName().equals("propertyDescription")));
if (propertyTags.isEmpty()) {
List<? extends DocTree> comment = utils.getFullBody(property);
blockTags.addAll(cmtutils.makePropertyDescriptionTree(comment));
@@ -331,8 +331,8 @@ private void processProperty(Element member,
blockTags.addAll(tags);

List<? extends DocTree> bTags = utils.getBlockTags(property,
t -> (t instanceof UnknownBlockTagTree)
&& ((UnknownBlockTagTree) t).getTagName().equals("defaultValue"));
t -> (t instanceof UnknownBlockTagTree tree)
&& (tree.getTagName().equals("defaultValue")));
blockTags.addAll(bTags);

//add @see tags
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 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
@@ -223,8 +223,7 @@ public TagletManager(BaseConfiguration configuration) {
* @throws IOException if an error occurs while setting the location
*/
public void initTagletPath(JavaFileManager fileManager) throws IOException {
if (fileManager instanceof StandardJavaFileManager) {
StandardJavaFileManager sfm = (StandardJavaFileManager)fileManager;
if (fileManager instanceof StandardJavaFileManager sfm) {
if (tagletPath != null) {
List<File> paths = new ArrayList<>();
for (String pathname : tagletPath.split(File.pathSeparator)) {
@@ -552,8 +551,7 @@ private void printTagMisuseWarn(CommentHelper ch, Taglet taglet, DocTree tag, St
case PACKAGE:
return blockTagletsByLocation.get(Location.PACKAGE);
case OTHER:
if (e instanceof DocletElement) {
DocletElement de = (DocletElement) e;
if (e instanceof DocletElement de) {
switch (de.getSubKind()) {
case DOCFILE:
return blockTagletsByLocation.get(Location.PACKAGE);
@@ -752,7 +750,7 @@ private void showTaglets(PrintStream out) {
+ format(t.inMethod(), "method") + " "
+ format(t.inField(), "field") + " "
+ format(t.isInlineTag(), "inline")+ " "
+ format((t instanceof SimpleTaglet) && !((SimpleTaglet) t).enabled, "disabled"));
+ format((t instanceof SimpleTaglet st) && !st.enabled, "disabled"));
});
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@@ -263,8 +263,7 @@ public Content getBlockTagOutput(TagletManager tagletManager,
continue;
}

if (element.getKind() == ElementKind.MODULE && taglet instanceof BaseTaglet) {
BaseTaglet t = (BaseTaglet) taglet;
if (element.getKind() == ElementKind.MODULE && taglet instanceof BaseTaglet t) {
switch (t.getTagKind()) {
// @uses and @provides are handled separately, so skip here.
// See ModuleWriterImpl.computeModulesData
@@ -280,7 +279,7 @@ public Content getBlockTagOutput(TagletManager tagletManager,
continue;
}

if (taglet instanceof SimpleTaglet && !((SimpleTaglet) taglet).enabled) {
if (taglet instanceof SimpleTaglet st && !st.enabled) {
// taglet has been disabled
continue;
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 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
@@ -74,8 +74,8 @@ public void inherit(DocFinder.Input input, DocFinder.Output output) {
Element exception;
CommentHelper ch = utils.getCommentHelper(input.element);
if (input.tagId == null) {
exception = input.docTreeInfo.docTree instanceof ThrowsTree
? ch.getException((ThrowsTree) input.docTreeInfo.docTree) : null;
exception = input.docTreeInfo.docTree instanceof ThrowsTree tt
? ch.getException(tt) : null;
input.tagId = exception == null
? ch.getExceptionName(input.docTreeInfo.docTree).getSignature()
: utils.getFullyQualifiedName(exception);
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 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
@@ -63,7 +63,7 @@ protected DocPath(String p) {

@Override
public boolean equals(Object other) {
return (other instanceof DocPath) && path.equals(((DocPath)other).path);
return (other instanceof DocPath dp) && path.equals(dp.path);
}

@Override
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 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
@@ -396,14 +396,13 @@ private boolean link(String url, String elemlisturl, Reporter reporter, boolean
*/
private Item findElementItem(Element element) {
Item item = null;
if (element instanceof ModuleElement) {
item = moduleItems.get(utils.getModuleName((ModuleElement)element));
if (element instanceof ModuleElement me) {
item = moduleItems.get(utils.getModuleName(me));
}
else if (element instanceof PackageElement) {
PackageElement packageElement = (PackageElement)element;
ModuleElement moduleElement = utils.containingModule(packageElement);
else if (element instanceof PackageElement pkg) {
ModuleElement moduleElement = utils.containingModule(pkg);
Map<String, Item> pkgMap = packageItems.get(utils.getModuleName(moduleElement));
item = (pkgMap != null) ? pkgMap.get(utils.getPackageName(packageElement)) : null;
item = (pkgMap != null) ? pkgMap.get(utils.getPackageName(pkg)) : null;
}
return item;
}
@@ -624,8 +623,7 @@ private InputStream open(URL url) throws IOException {
in = conn.getInputStream();
redir = false;

if (conn instanceof HttpURLConnection) {
HttpURLConnection http = (HttpURLConnection)conn;
if (conn instanceof HttpURLConnection http) {
int stat = http.getResponseCode();
// See:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
@@ -1737,11 +1737,11 @@ public int compare(String s1, String s2) {

private Collator createCollator(Locale locale) {
Collator baseCollator = Collator.getInstance(locale);
if (baseCollator instanceof RuleBasedCollator) {
if (baseCollator instanceof RuleBasedCollator rbc) {
// Extend collator to sort signatures with additional args and var-args in a well-defined order:
// () < (int) < (int, int) < (int...)
try {
return new RuleBasedCollator(((RuleBasedCollator) baseCollator).getRules()
return new RuleBasedCollator(rbc.getRules()
+ "& ')' < ',' < '.','['");
} catch (ParseException e) {
throw new RuntimeException(e);
@@ -2333,8 +2333,8 @@ public String visitPrimitiveAsLong(PrimitiveType t, Object val) {
protected String defaultAction(TypeMirror e, Object val) {
if (val == null)
return null;
else if (val instanceof String)
return sourceForm((String) val);
else if (val instanceof String s)
return sourceForm(s);
return val.toString(); // covers int, short
}

@@ -2632,10 +2632,10 @@ public void removeCommentHelper(Element element) {

public List<? extends DocTree> getBlockTags(Element element, Taglet taglet) {
return getBlockTags(element, t -> {
if (taglet instanceof BaseTaglet) {
return ((BaseTaglet) taglet).accepts(t);
} else if (t instanceof UnknownBlockTagTree) {
return ((UnknownBlockTagTree) t).getTagName().equals(taglet.getName());
if (taglet instanceof BaseTaglet bt) {
return bt.accepts(t);
} else if (t instanceof UnknownBlockTagTree tree) {
return tree.getTagName().equals(taglet.getName());
} else {
return false;
}
Loading

1 comment on commit ebc9357

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on ebc9357 May 24, 2021

Please sign in to comment.