Skip to content

Commit

Permalink
8265684: implement Sealed Classes as a standard feature in Java, java…
Browse files Browse the repository at this point in the history
…doc changes

Reviewed-by: jjg
  • Loading branch information
Vicente Romero committed May 20, 2021
1 parent 0fa9223 commit 7dcb9fd
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
Expand Down Expand Up @@ -84,8 +85,7 @@ public class ClassWriterImpl extends SubWriterHolderWriter implements ClassWrite
"java.lang.constant.ConstantDesc",
"java.io.Serializable");

private static final Set<String> previewModifiers
= Set.of("sealed", "non-sealed");
private static final Set<String> previewModifiers = Collections.emptySet();

protected final TypeElement typeElement;

Expand Down Expand Up @@ -196,7 +196,7 @@ protected TypeElement getCurrentPageElement() {
return typeElement;
}

@Override @SuppressWarnings("preview")
@Override
public void addClassSignature(String modifiers, Content classInfoTree) {
ContentBuilder mods = new ContentBuilder();
String sep = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1686,7 +1686,6 @@ private boolean shouldNotRedirectRelativeLinks() {
*
* @return the text, with all the relative links redirected to work.
*/
@SuppressWarnings("preview")
private String redirectRelativeLinks(Element element, TextTree tt) {
String text = tt.getBody();
if (element == null || utils.isOverviewElement(element) || shouldNotRedirectRelativeLinks()) {
Expand Down Expand Up @@ -2232,7 +2231,6 @@ public void addPreviewInfo(Element forWhat, Content target) {
}
}

@SuppressWarnings("preview")
private List<Content> getPreviewNotes(TypeElement el) {
String className = el.getSimpleName().toString();
List<Content> result = new ArrayList<>();
Expand All @@ -2257,9 +2255,6 @@ private List<Content> getPreviewNotes(TypeElement el) {
}
previewLanguageFeatures.addAll(utils.previewLanguageFeaturesUsed(el));
if (!previewLanguageFeatures.isEmpty()) {
if (previewLanguageFeatures.contains(DeclarationPreviewLanguageFeatures.SEALED_PERMITS)) {
previewLanguageFeatures.remove(DeclarationPreviewLanguageFeatures.SEALED);
}
for (DeclarationPreviewLanguageFeatures feature : previewLanguageFeatures) {
String featureDisplayName =
resources.getText("doclet.Declared_Using_Preview." + feature.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public TypeSignature setModifiers(Content modifiers) {
return this;
}

@SuppressWarnings("preview")
public Content toContent() {
Content content = new ContentBuilder();
Content annotationInfo = classWriter.getAnnotationInfo(typeElement, true);
Expand Down Expand Up @@ -185,10 +184,6 @@ public Content toContent() {
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 {
Expand All @@ -209,7 +204,6 @@ public Content toContent() {
return HtmlTree.DIV(HtmlStyle.typeSignature, content);
}

@SuppressWarnings("preview")
private Content getRecordComponents() {
Content content = new ContentBuilder();
content.add("(");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ public Content getParamHeader(ParamTaglet.ParamKind kind) {
}

@Override
@SuppressWarnings("preview")
public Content paramTagOutput(Element element, ParamTree paramTag, String paramName) {
ContentBuilder body = new ContentBuilder();
CommentHelper ch = utils.getCommentHelper(element);
Expand Down Expand Up @@ -461,7 +460,6 @@ protected TypeElement getCurrentPageElement() {
return htmlWriter.getCurrentPageElement();
}

@SuppressWarnings("preview")
private Content createAnchorAndSearchIndex(Element element, String tagText, String desc, DocTree tree) {
Content result = null;
if (context.isFirstSentence && context.inSummary || context.inTags.contains(DocTree.Kind.INDEX)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,6 @@ public boolean isGeneratedDoc(TypeElement te) {
* Splits the elements in a collection to its individual
* collection.
*/
@SuppressWarnings("preview")
private static class Splitter {

final Set<ModuleElement> mset = new LinkedHashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ public void setRecordConstructorTree(ExecutableElement ee) {
* Generates the description for the standard {@code equals} method for a record.
* @param ee the {@code equals} method
*/
@SuppressWarnings("preview")
public void setRecordEqualsTree(ExecutableElement ee) {
List<DocTree> fullBody = new ArrayList<>();
add(fullBody, "doclet.record_equals_doc.fullbody.head");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@ private void setEnumDocumentation(TypeElement elem) {
* @param elem the record element
*/

@SuppressWarnings("preview")
private void setRecordDocumentation(TypeElement elem) {
CommentUtils cmtUtils = configuration.cmtUtils;
Set<Name> componentNames = elem.getRecordComponents().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ public void inherit(DocFinder.Input input, DocFinder.Output output) {
}

@Override
@SuppressWarnings("preview")
public Content getAllBlockTagOutput(Element holder, TagletWriter writer) {
Utils utils = writer.configuration().utils;
if (utils.isExecutableElement(holder)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ void seenTag(String name) {
* @param trees the trees containing the comments
* @param inlineTrees true if the trees are inline and false otherwise
*/
@SuppressWarnings("preview")
public void checkTags(Element element, Iterable<? extends DocTree> trees, boolean inlineTrees) {
if (trees == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,6 @@ private void addAll(Map<TypeElement, List<TypeElement>> map, TypeElement te, Col
private <T extends Element> void mapTypeParameters(final Map<TypeElement, List<T>> map,
Element element, final T holder) {

@SuppressWarnings("preview")
SimpleElementVisitor14<Void, Void> elementVisitor
= new SimpleElementVisitor14<Void, Void>() {

Expand Down Expand Up @@ -561,7 +560,6 @@ public Void visitDeclared(DeclaredType t, Void p) {
* @param e whose type parameters are being checked.
* @param holder owning the type parameters.
*/
@SuppressWarnings("preview")
private <T extends Element> void mapAnnotations(final Map<TypeElement, List<T>> map,
Element e, final T holder) {
new SimpleElementVisitor14<Void, Void>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ private int getKindIndex(Element e) {
};
}

@SuppressWarnings("preview")
boolean hasParameters(Element e) {
return new SimpleElementVisitor14<Boolean, Void>() {
@Override
Expand All @@ -533,7 +532,6 @@ protected Boolean defaultAction(Element e, Void p) {
* @return a negative integer, zero, or a positive integer as the first argument is less
* than, equal to, or greater than the second.
*/
@SuppressWarnings("preview")
private String getFullyQualifiedName(Element e) {
return new SimpleElementVisitor14<String, Void>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ protected Category getCategory(DocTree docTree) {
};
}

@SuppressWarnings("preview")
protected Category getCategory(Element element) {
return new SimpleElementVisitor14<Category, Void>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.text.RuleBasedCollator;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
Expand All @@ -54,6 +55,7 @@
import java.util.TreeSet;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import javax.lang.model.AnnotatedConstruct;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
Expand Down Expand Up @@ -488,7 +490,6 @@ public boolean definesSerializableFields(TypeElement aclass) {
return configuration.workArounds.definesSerializableFields( aclass);
}

@SuppressWarnings("preview")
public String modifiersToString(Element e, boolean trailingSpace) {
SortedSet<Modifier> modifiers = new TreeSet<>(e.getModifiers());
modifiers.remove(NATIVE);
Expand Down Expand Up @@ -2988,37 +2989,19 @@ public String toString() {
* @param e the Element to check.
* @return the set of preview language features used to declare the given element
*/
@SuppressWarnings("preview")
public Set<DeclarationPreviewLanguageFeatures> previewLanguageFeaturesUsed(Element e) {
Set<DeclarationPreviewLanguageFeatures> result = new HashSet<>();

if ((e.getKind().isClass() || e.getKind().isInterface()) &&
e.getModifiers().contains(Modifier.SEALED)) {
List<? extends TypeMirror> permits = ((TypeElement) e).getPermittedSubclasses();
boolean hasLinkablePermits = permits.stream()
.anyMatch(t -> isLinkable(asTypeElement(t)));
if (hasLinkablePermits) {
result.add(DeclarationPreviewLanguageFeatures.SEALED_PERMITS);
} else {
result.add(DeclarationPreviewLanguageFeatures.SEALED);
}
}

return result;
return new HashSet<>();
}

public enum DeclarationPreviewLanguageFeatures {

SEALED(List.of("sealed")),
SEALED_PERMITS(List.of("sealed", "permits"));
NONE(List.of(""));
public final List<String> features;

DeclarationPreviewLanguageFeatures(List<String> features) {
this.features = features;
}
}

@SuppressWarnings("preview")
public PreviewSummary declaredUsingPreviewAPIs(Element el) {
List<TypeElement> usedInDeclaration = new ArrayList<>();
usedInDeclaration.addAll(annotations2Classes(el));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,6 @@ class LocalMemberTable {
}
}

@SuppressWarnings("preview")
String getMemberKey(Element e) {
return new SimpleElementVisitor14<String, Void>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,6 @@ private boolean isTypeElementSelected(TypeElement te) {
return (xclasses || toolEnv.getFileKind(te) == SOURCE) && isSelected(te);
}

@SuppressWarnings("preview")
SimpleElementVisitor14<Boolean, Void> visibleElementVisitor = null;
/**
* Returns true if the element is selected, by applying
Expand All @@ -989,7 +988,6 @@ private boolean isTypeElementSelected(TypeElement te) {
* @param e the element to be checked
* @return true if the element is visible
*/
@SuppressWarnings("preview")
public boolean isSelected(Element e) {
if (toolEnv.isSynthetic((Symbol) e)) {
return false;
Expand Down Expand Up @@ -1029,7 +1027,6 @@ public Boolean visitUnknown(Element e, Void p) {
return visibleElementVisitor.visit(e);
}

@SuppressWarnings("preview")
private class IncludedVisitor extends SimpleElementVisitor14<Boolean, Void> {
private final Set<Element> includedCache;

Expand Down
27 changes: 4 additions & 23 deletions test/langtools/jdk/javadoc/doclet/testPreview/TestPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,16 @@ public void testUserJavadoc() {
"m/pkg");
checkExit(Exit.OK);

ResourceBundle bundle = ResourceBundle.getBundle("jdk.javadoc.internal.doclets.formats.html.resources.standard", ModuleLayer.boot().findModule("jdk.javadoc").get());

{
String zero = MessageFormat.format(bundle.getString("doclet.PreviewLeadingNote"), "<code>TestPreviewDeclaration</code>");
String one = MessageFormat.format(bundle.getString("doclet.Declared_Using_Preview"), "<code>TestPreviewDeclaration</code>", "<em>Sealed Classes</em>", "<code>sealed</code>");
String two = MessageFormat.format(bundle.getString("doclet.PreviewTrailingNote1"), "<code>TestPreviewDeclaration</code>");
String three = MessageFormat.format(bundle.getString("doclet.PreviewTrailingNote2"), new Object[0]);
String expectedTemplate = """
<div class="preview-block" id="preview-pkg.TestPreviewDeclaration"><span class="preview-label">{0}</span>
<ul class="preview-comment">
<li>{1}</li>
</ul>
<div class="preview-comment">{2}</div>
<div class="preview-comment">{3}</div>
</div>""";
String expected = MessageFormat.format(expectedTemplate, zero, one, two, three);
checkOutput("m/pkg/TestPreviewDeclaration.html", true, expected);
}

checkOutput("m/pkg/TestPreviewDeclarationUse.html", true,
"<code><a href=\"TestPreviewDeclaration.html\" title=\"interface in pkg\">TestPreviewDeclaration</a><sup><a href=\"TestPreviewDeclaration.html#preview-pkg.TestPreviewDeclaration\">PREVIEW</a></sup></code>");
"<code><a href=\"TestPreviewDeclaration.html\" title=\"interface in pkg\">TestPreviewDeclaration</a></code>");
checkOutput("m/pkg/TestPreviewAPIUse.html", true,
"<a href=\"" + doc + "java.base/preview/Core.html\" title=\"class or interface in preview\" class=\"external-link\">Core</a><sup><a href=\"" + doc + "java.base/preview/Core.html#preview-preview.Core\" title=\"class or interface in preview\" class=\"external-link\">PREVIEW</a>");
checkOutput("m/pkg/DocAnnotation.html", true,
"<div class=\"preview-block\" id=\"preview-pkg.DocAnnotation\"><span class=\"preview-label\">");
"<span class=\"modifiers\">public @interface </span><span class=\"element-name type-name-label\">DocAnnotation</span>");
checkOutput("m/pkg/DocAnnotationUse1.html", true,
"<div class=\"preview-block\" id=\"preview-pkg.DocAnnotationUse1\"><span class=\"preview-label\">");
"<div class=\"inheritance\">pkg.DocAnnotationUse1</div>");
checkOutput("m/pkg/DocAnnotationUse2.html", true,
"<div class=\"preview-block\" id=\"preview-pkg.DocAnnotationUse2\"><span class=\"preview-label\">");
"<div class=\"inheritance\">pkg.DocAnnotationUse2</div>");
}

@Test
Expand Down
Loading

1 comment on commit 7dcb9fd

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.