8267329: Modernize Javadoc code to use instanceof with pattern matching

Reviewed-by: prappo, jjg
This commit is contained in:
Ian Graves 2021-05-24 21:57:30 +00:00
parent 209769b5ad
commit ebc9357d58
24 changed files with 88 additions and 99 deletions

View File

@ -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 class JavadocTool implements DocumentationTool {
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);

View File

@ -470,9 +470,9 @@ public class ClassWriterImpl extends SubWriterHolderWriter implements ClassWrite
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(

View File

@ -407,10 +407,10 @@ public class Contents {
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();

View File

@ -354,8 +354,7 @@ public class HtmlConfiguration extends BaseConfiguration {
@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;

View File

@ -1123,8 +1123,8 @@ public class HtmlDocletWriter {
// 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 @@ public class HtmlDocletWriter {
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()));

View File

@ -175,9 +175,9 @@ public class SourceToHTMLConverter {
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);
}
}
}

View File

@ -155,8 +155,7 @@ public class SystemPropertiesWriter extends HtmlDocletWriter {
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

View File

@ -318,12 +318,12 @@ public class TagletWriterImpl extends TagletWriter {
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 class TagletWriterImpl extends TagletWriter {
@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);

View File

@ -52,8 +52,8 @@ public class ContentBuilder extends Content {
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 class ContentBuilder extends Content {
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());
}

View File

@ -174,8 +174,8 @@ public class HtmlTree extends Content {
*/
@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)

View File

@ -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 @@ public class CommentUtils {
}
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 class CommentUtils {
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();
}

View File

@ -316,8 +316,8 @@ public abstract class MemberSummaryBuilder extends AbstractMemberBuilder {
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 @@ public abstract class MemberSummaryBuilder extends AbstractMemberBuilder {
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

View File

@ -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 class TagletManager {
* @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 @@ public class TagletManager {
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 @@ public class TagletManager {
+ 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"));
});
}

View File

@ -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 abstract class TagletWriter {
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 abstract class TagletWriter {
continue;
}
if (taglet instanceof SimpleTaglet && !((SimpleTaglet) taglet).enabled) {
if (taglet instanceof SimpleTaglet st && !st.enabled) {
// taglet has been disabled
continue;
}

View File

@ -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 class ThrowsTaglet extends BaseTaglet
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);

View File

@ -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 @@ public class DocPath {
@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

View File

@ -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 @@ public class Extern {
*/
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 @@ public class Extern {
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

View File

@ -1737,11 +1737,11 @@ public class Utils {
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 class Utils {
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 class Utils {
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;
}

View File

@ -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
@ -591,14 +591,14 @@ public class Checker extends DocTreePathScanner<Void, Void> {
}
void warnIfEmpty(TagStackItem tsi, DocTree endTree) {
if (tsi.tag != null && tsi.tree instanceof StartElementTree) {
if (tsi.tag != null && tsi.tree instanceof StartElementTree startTree) {
if (tsi.tag.flags.contains(HtmlTag.Flag.EXPECT_CONTENT)
&& !tsi.flags.contains(Flag.HAS_TEXT)
&& !tsi.flags.contains(Flag.HAS_ELEMENT)
&& !tsi.flags.contains(Flag.HAS_INLINE_TAG)
&& !(tsi.tag.elemKind == ElemKind.HTML4)) {
DocTree tree = (endTree != null) ? endTree : tsi.tree;
Name treeName = ((StartElementTree) tsi.tree).getName();
DocTree tree = (endTree != null) ? endTree : startTree;
Name treeName = startTree.getName();
env.messages.warning(HTML, tree, "dc.tag.empty", treeName);
}
}

View File

@ -1159,8 +1159,7 @@ public class ElementsTable {
@Override
public boolean equals(Object obj) {
if (obj instanceof ModulePackage) {
ModulePackage that = (ModulePackage)obj;
if (obj instanceof ModulePackage that) {
return this.toString().equals(that.toString());
}
return false;

View File

@ -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
@ -158,8 +158,8 @@ public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler {
ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<>();
try {
StandardJavaFileManager fm = toolEnv.fileManager instanceof StandardJavaFileManager
? (StandardJavaFileManager) toolEnv.fileManager
StandardJavaFileManager fm = toolEnv.fileManager instanceof StandardJavaFileManager sfm
? sfm
: null;
Set<String> packageNames = new LinkedHashSet<>();
// Normally, the args should be a series of package names or file names.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
* 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
@ -66,9 +66,9 @@ public class Messager extends Log implements Reporter {
/** Get the current messager, which is also the compiler log. */
public static Messager instance0(Context context) {
Log instance = context.get(logKey);
if (instance == null || !(instance instanceof Messager))
if (!(instance instanceof Messager m))
throw new InternalError("no messager instance!");
return (Messager)instance;
return m;
}
public static void preRegister(Context context,

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* 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
@ -140,8 +140,8 @@ public class Start {
this.locale = Locale.getDefault();
Log log = context.get(Log.logKey);
if (log instanceof Messager) {
messager = (Messager) log;
if (log instanceof Messager m){
messager = m;
} else {
PrintWriter out = context.get(Log.errKey);
messager = (out == null)
@ -366,8 +366,8 @@ public class Start {
if (fileManager == null) {
JavacFileManager.preRegister(context);
fileManager = context.get(JavaFileManager.class);
if (fileManager instanceof BaseFileManager) {
((BaseFileManager) fileManager).autoClose = true;
if (fileManager instanceof BaseFileManager bfm) {
bfm.autoClose = true;
}
}
@ -433,9 +433,8 @@ public class Start {
reportInternalError(ee);
result = ABNORMAL;
} finally {
if (fileManager != null
&& fileManager instanceof BaseFileManager
&& ((BaseFileManager) fileManager).autoClose) {
if (fileManager instanceof BaseFileManager bfm
&& bfm.autoClose) {
try {
fileManager.close();
} catch (IOException ignore) {}
@ -518,8 +517,8 @@ public class Start {
return CMDERR;
}
if (fileManager instanceof BaseFileManager) {
((BaseFileManager) fileManager).handleOptions(options.fileManagerOptions());
if (fileManager instanceof BaseFileManager bfm) {
bfm.handleOptions(options.fileManagerOptions());
}
String mr = com.sun.tools.javac.main.Option.MULTIRELEASE.primaryName;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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
@ -148,8 +148,8 @@ public class ToolEnvironment {
chk = Check.instance(context);
types = com.sun.tools.javac.code.Types.instance(context);
fileManager = context.get(JavaFileManager.class);
if (fileManager instanceof JavacFileManager) {
((JavacFileManager)fileManager).setSymbolFileEnabled(false);
if (fileManager instanceof JavacFileManager jfm) {
jfm.setSymbolFileEnabled(false);
}
docTrees = JavacTrees.instance(context);
source = Source.instance(context);