Skip to content

Commit

Permalink
fix preview bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nizarbenalla committed Nov 14, 2024
1 parent db56266 commit 8e44121
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2531,9 +2531,13 @@ public void addPreviewInfo(Element forWhat, Content target) {
ul.add(HtmlTree.LI(note));
}
previewDiv.add(ul);
String previewUseNote =
utils.isNonPreviewExtendingPreview(forWhat)
? "doclet.NonPreviewPreview"
: "doclet.PreviewTrailingNote1";
Content note1 =
contents.getContent("doclet.PreviewTrailingNote1",
nameCode);
contents.getContent(previewUseNote,
nameCode);
previewDiv.add(HtmlTree.DIV(HtmlStyles.previewComment, note1));
Content note2 =
contents.getContent("doclet.PreviewTrailingNote2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ Programs can only use <code>requires transitive java.base</code> when \
preview features are enabled.<br>\
Preview features may be removed in a future release, or upgraded \
to permanent features of the Java Platform.<br>
doclet.NonPreviewPreview=Programs can use {0} without enabling preview features. \
Programs can only reference the preview API by name when preview features are enabled.
doclet.RestrictedMethod=restricted method
doclet.RestrictedLeadingNote={0} is a {1} of the Java platform.
doclet.RestrictedTrailingNote1=Programs can only use {0} when access to restricted methods is enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2710,6 +2710,27 @@ private boolean hasNoPreviewAnnotation(Element el) {
.anyMatch(am -> "jdk.internal.javac.NoPreview".equals(getQualifiedTypeName(am.getAnnotationType())));
}

public boolean isNonPreviewExtendingPreview(Element el) {
if (!configuration.workArounds.isPreviewAPI(el) && (el.getKind().isDeclaredType())) {
Element enclosingElement = el.getEnclosingElement();
while (enclosingElement != null) {
if (configuration.workArounds.isPreviewAPI(enclosingElement)) {
return false; // inner class of preview feature
}
enclosingElement = enclosingElement.getEnclosingElement(); // deal with nested inner classes
}

TypeElement typeElement = (TypeElement) el;
for (TypeMirror supertype : typeElement.getInterfaces()) {
Element superElement = ((DeclaredType) supertype).asElement();
if (configuration.workArounds.isPreviewAPI(superElement)) {
return true; // non preview extending a preview interface
}
}
}
return false; // Either it's a preview API or has no preview parent
}

private PreviewFlagProvider previewFlagProvider = new PreviewFlagProvider() {
@Override
public boolean isPreview(Element el) {
Expand All @@ -2723,7 +2744,8 @@ public boolean isPreview(Element el) {
|| !previewAPIs.previewAPI.isEmpty()
|| !previewAPIs.reflectivePreviewAPI.isEmpty()
|| !previewAPIs.declaredUsingPreviewFeature.isEmpty())
&& !hasNoPreviewAnnotation(el);
&& !hasNoPreviewAnnotation(el)
&& !isNonPreviewExtendingPreview(el);
}
};

Expand Down
43 changes: 42 additions & 1 deletion test/langtools/jdk/javadoc/doclet/testPreview/TestPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/*
* @test
* @bug 8250768 8261976 8277300 8282452 8287597 8325325 8325874 8297879
* 8331947 8281533
* 8331947 8281533 8343239
* @summary test generated docs for items declared using preview
* @library ../../lib
* @modules jdk.javadoc/jdk.javadoc.internal.tool
Expand Down Expand Up @@ -176,6 +176,47 @@ public void testPreviewAPIJavadoc() {
checkOutput("preview-list.html", false, "supportMethod");
}

// 8343239 pre-existing permanent API that is later retrofitted
// to extend a @PreviewFeature interface should not be flagged as a preview feature
@Test
public void nonPreviewExtendsPreview() {
javadoc("-d", "out-non-preview-extends-preview",
"--add-exports", "java.base/jdk.internal.javac=api2",
"--source-path", Paths.get(testSrc, "api2").toAbsolutePath().toString(),
"--show-packages=all",
"api2/nonpreviewextendspreview");
checkExit(Exit.OK);

checkOutput("api2/nonpreviewextendspreview/NonPreviewExtendsPreview.html", true,
"""
<div class="horizontal-scroll">
<div class="type-signature"><span class="modifiers">public interface </span><span class="element-name type-name-label">NonPreviewExtendsPreview</span><span class="extends-implements">
extends <a href="CoreInterface.html" title="interface in nonpreviewextendspreview">CoreInterface</a><sup><a href="CoreInterface.html#preview-previewextendsnonpreview.CoreInterface">PREVIEW</a></sup></span></div>
<div class="preview-block" id="preview-nonpreviewextendspreview.NonPreviewExtendsPreview"><span class="preview-label"><code>NonPreviewExtendsPreview</code> relies on preview features of the Java platform:</span>
<ul class="preview-comment">
<li><code>NonPreviewExtendsPreview</code> refers to one or more preview APIs: <a href="CoreInterface.html" title="interface in previewextendsnonpreview"><code>CoreInterface</code></a>.</li>
</ul>
<div class="preview-comment">Programs can use <code>NonPreviewExtendsPreview</code> without enabling preview features. Programs can only reference the preview API by name when preview features are enabled.</div>
<div class="preview-comment">Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.</div>
</div>
""");
checkOutput("api2/nonpreviewextendspreview/NonPreviewExtendsPreview.html", false,
"""
<div class="preview-comment">Programs can only use <code>NonPreviewExtendsPreview</code> when preview features are enabled.</div>
""");
checkOutput("api2/nonpreviewextendspreview/CoreInterface.html", true,
"""
<div class="horizontal-scroll">
<div class="type-signature"><span class="modifiers">public interface </span><span class="element-name type-name-label">CoreInterface</span></div>
<div class="preview-block" id="preview-nonpreviewextendspreview.CoreInterface"><span class="preview-label"><code>CoreInterface</code> is a preview API of the Java platform.</span>
<div class="preview-comment">Programs can only use <code>CoreInterface</code> when preview features are enabled.</div>
<div class="preview-comment">Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.</div>
</div>
<div class="block">Preview feature</div>
</div>
""");
}

@Test
public void test8277300() {
javadoc("-d", "out-8277300",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024, 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.
*
* 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 nonpreviewextendspreview;

import jdk.internal.javac.PreviewFeature;

/**
* Preview feature
*/
@PreviewFeature(feature= PreviewFeature.Feature.TEST)
public interface CoreInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024, 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.
*
* 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 nonpreviewextendspreview;

/**
* Non preview feature
*/
public interface NonPreviewExtendsPreview extends CoreInterface {
default int getNumber() {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2024, 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.
*
* 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.
*/

/**
* Non preview package.
*/
package api2.nonpreviewextendspreview;

0 comments on commit 8e44121

Please sign in to comment.