8284037: Snippet-files subdirectory not automatically detected when in unnamed package

Reviewed-by: prappo
This commit is contained in:
Jonathan Gibbons 2022-05-25 17:41:58 +00:00
parent c6743489d2
commit f786e2a22f
2 changed files with 103 additions and 12 deletions

View File

@ -38,7 +38,6 @@ import javax.lang.model.element.PackageElement;
import javax.tools.Diagnostic;
import javax.tools.DocumentationTool.Location;
import javax.tools.FileObject;
import javax.tools.JavaFileManager;
import com.sun.source.doctree.AttributeTree;
import com.sun.source.doctree.DocTree;
@ -215,14 +214,14 @@ public class SnippetTaglet extends BaseTaglet {
// we didn't create JavaFileManager, so we won't close it; even if an error occurs
var fileManager = writer.configuration().getFileManager();
// first, look in local snippet-files subdirectory
Utils utils = writer.configuration().utils;
PackageElement pkg = getPackageElement(holder, utils);
JavaFileManager.Location l = utils.getLocationForPackage(pkg);
String relativeName = "snippet-files/" + v;
String packageName = packageName(pkg, utils);
try {
fileObject = fileManager.getFileForInput(l, packageName, relativeName);
// first, look in local snippet-files subdirectory
var utils = writer.configuration().utils;
var pkg = getPackageElement(holder, utils);
var pkgLocation = utils.getLocationForPackage(pkg);
var pkgName = pkg.getQualifiedName().toString(); // note: empty string for unnamed package
var relativeName = "snippet-files/" + v;
fileObject = fileManager.getFileForInput(pkgLocation, pkgName, relativeName);
// if not found in local snippet-files directory, look on snippet path
if (fileObject == null && fileManager.hasLocation(Location.SNIPPET_PATH)) {
@ -409,10 +408,6 @@ public class SnippetTaglet extends BaseTaglet {
return writer.invalidTagOutput(resources.getText("doclet.tag.invalid", "snippet"), details);
}
private String packageName(PackageElement pkg, Utils utils) {
return utils.getPackageName(pkg);
}
private static PackageElement getPackageElement(Element e, Utils utils) {
if (e instanceof DocletElement de) {
return de.getPackageElement();

View File

@ -0,0 +1,96 @@
/*
* Copyright (c) 2022, 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.
*/
/*
* @test
* @bug 8284037
* @summary Snippet-files subdirectory not automatically detected when in unnamed package
* @library /tools/lib ../../lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.javadoc/jdk.javadoc.internal.tool
* @build javadoc.tester.* toolbox.ToolBox toolbox.ModuleBuilder builder.ClassBuilder
* @run main TestSnippetUnnamedPackage
*/
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class TestSnippetUnnamedPackage extends SnippetTester {
public static void main(String... args) throws Exception {
new TestSnippetUnnamedPackage().runTests();
}
@Test
public void testNoSourcePath(Path base) throws IOException {
test(base, false);
}
@Test
public void testSourcePath(Path base) throws IOException {
test(base, true);
}
void test(Path base, boolean useSourcePath) throws IOException {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"""
/**
* Comment.
* Before.
* {@snippet class=S}
* After.
*/
public class C {
private C() { }
}
""");
tb.writeFile(src.resolve("snippet-files").resolve("S.java"),
"public class S { }");
var args = new ArrayList<String>();
args.addAll(List.of("-d", base.resolve("out").toString()));
if (useSourcePath) {
args.addAll(List.of("--source-path", src.toString()));
}
args.add(src.resolve("C.java").toString());
javadoc(args.toArray(String[]::new));
checkExit(useSourcePath ? Exit.OK : Exit.ERROR);
checkOutput(Output.OUT, !useSourcePath,
"C.java:4: error: file not found on source path or snippet path: S.java");
checkOutput("C.html", useSourcePath,
"""
Before.
\s
<div class="snippet-container"><button class="snippet-copy" onclick="copySnippet(this)"><span data-copied="Copied!">Copy</span><img src="copy.svg" alt="Copy"></button>
<pre class="snippet"><code class="language-java">public class S { }</code></pre>
</div>
After.""");
}
}