Skip to content

JDK-8283714: REDO - Unexpected TypeElement in ANALYZE TaskEvent #8101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 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
Expand Down Expand Up @@ -1305,7 +1305,7 @@ public Env<AttrContext> attribute(Env<AttrContext> env) {
log.printVerbose("checking.attribution", env.enclClass.sym);

if (!taskListener.isEmpty()) {
TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
TaskEvent e = newAnalyzeTaskEvent(env);
taskListener.started(e);
}

Expand Down Expand Up @@ -1390,12 +1390,30 @@ protected void flow(Env<AttrContext> env, Queue<Env<AttrContext>> results) {
}
finally {
if (!taskListener.isEmpty()) {
TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
TaskEvent e = newAnalyzeTaskEvent(env);
taskListener.finished(e);
}
}
}

private TaskEvent newAnalyzeTaskEvent(Env<AttrContext> env) {
JCCompilationUnit toplevel = env.toplevel;
ClassSymbol sym;
if (env.enclClass.sym == syms.predefClass) {
if (TreeInfo.isModuleInfo(toplevel)) {
sym = toplevel.modle.module_info;
} else if (TreeInfo.isPackageInfo(toplevel)) {
sym = toplevel.packge.package_info;
} else {
throw new IllegalStateException("unknown env.toplevel");
}
} else {
sym = env.enclClass.sym;
}

return new TaskEvent(TaskEvent.Kind.ANALYZE, toplevel, sym);
}

/**
* Prepare attributed parse trees, in conjunction with their attribution contexts,
* for source or code generation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 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
Expand Down Expand Up @@ -445,18 +445,18 @@ public String getDocComment(Element e) {

@DefinedBy(Api.LANGUAGE_MODEL)
public PackageElement getPackageOf(Element e) {
if (e.getKind() == ElementKind.MODULE)
return null;
else
return cast(Symbol.class, e).packge();
Symbol sym = cast(Symbol.class, e);
return (sym.kind == MDL || sym.owner.kind == MDL) ? null : sym.packge();
}

@DefinedBy(Api.LANGUAGE_MODEL)
public ModuleElement getModuleOf(Element e) {
Symbol sym = cast(Symbol.class, e);
if (modules.getDefaultModule() == syms.noModule)
return null;
return (sym.kind == MDL) ? ((ModuleElement) e) : sym.packge().modle;
return (sym.kind == MDL) ? ((ModuleElement) e)
: (sym.owner.kind == MDL) ? (ModuleElement) sym.owner
: sym.packge().modle;
}

@DefinedBy(Api.LANGUAGE_MODEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
import java.util.stream.Collectors;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

import com.sun.source.doctree.AttributeTree;
Expand Down Expand Up @@ -162,7 +164,10 @@ Element getElement(ReferenceTree rtree) {
return null;
}
DocTrees doctrees = configuration.docEnv.getDocTrees();
return doctrees.getElement(docTreePath);
// Workaround for JDK-8284193
// DocTrees.getElement(DocTreePath) returns javac-internal Symbols
var e = doctrees.getElement(docTreePath);
return e == null || e.getKind() == ElementKind.CLASS && e.asType().getKind() != TypeKind.DECLARED ? null : e;
}

public TypeMirror getType(ReferenceTree rtree) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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 8284030
* @summary LinkFactory should not attempt to link to primitive types
* @library /tools/lib ../../lib
* @modules jdk.javadoc/jdk.javadoc.internal.tool
* @build toolbox.ToolBox javadoc.tester.*
* @run main TestLinkTagletPrimitive
*/

import javadoc.tester.JavadocTester;
import toolbox.ToolBox;

import java.io.IOException;
import java.nio.file.Path;

public class TestLinkTagletPrimitive extends JavadocTester {

public static void main(String... args) throws Exception {
TestLinkTagletPrimitive tester = new TestLinkTagletPrimitive();
tester.runTests();
}

ToolBox tb = new ToolBox();

@Test
public void testSimple(Path base) throws IOException {
Path src = base.resolve("src");

tb.writeJavaFiles(src, """
/**
* Comment.
* Byte: {@link byte}
* Void: {@link void}
*/
public class C {\s
private C() { }
}
""");

javadoc("-Xdoclint:none",
"-d", base.resolve("api").toString(),
"-sourcepath", src.toString(),
src.resolve("C.java").toString());
checkExit(Exit.OK);

checkOutput(Output.OUT, true,
"C.java:3: warning: Tag @link: reference not found: byte",
"C.java:4: warning: Tag @link: reference not found: void");

checkOutput("C.html", true,
Copy link
Member

Choose a reason for hiding this comment

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

I presume this run and the run below generate warnings. Should we check for them?

"""
<div class="block">Comment.
Byte:\s
<details class="invalid-tag">
<summary>invalid @link</summary>
<pre><code>byte</code></pre>
</details>

Void:\s
<details class="invalid-tag">
<summary>invalid @link</summary>
<pre><code>void</code></pre>
</details>
</div>
""");
}
@Test
public void testArray(Path base) throws IOException {
Path src = base.resolve("src");

tb.writeJavaFiles(src, """
/**
* Comment.
* Byte[]: {@link byte[]}
*/
public class C {\s
private C() { }
}
""");

javadoc("-Xdoclint:none",
"-d", base.resolve("api").toString(),
"-sourcepath", src.toString(),
src.resolve("C.java").toString());
checkExit(Exit.OK);

checkOutput(Output.OUT, true,
"C.java:3: warning: Tag @link: reference not found: byte[]");

checkOutput("C.html", true,
"""
<div class="block">Comment.
Byte[]:\s
<details class="invalid-tag">
<summary>invalid @link</summary>
<pre><code>byte[]</code></pre>
</details>
</div>
""");
}
}
168 changes: 168 additions & 0 deletions test/langtools/tools/javac/api/taskListeners/TestTypeElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* 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 8283661 8283714
* @summary Unexpected TypeElement in ANALYZE TaskEvent
* @modules jdk.compiler
* @run main TestTypeElement
*/

import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;

import com.sun.source.util.JavacTask;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskListener;

public class TestTypeElement {
public static void main(String... args) throws Exception {
new TestTypeElement().run();
}

private PrintStream log;
private Elements elements;
private int errors;

void run() throws Exception {
log = System.err;

List<JavaFileObject> files = List.of(
createFileObject("module-info.java", "module m { }"),
createFileObject("p/package-info.java", "/** Comment. */ package p;"),
createFileObject("p/C.java", "package p; public class C { }")
);

JavaCompiler c = ToolProvider.getSystemJavaCompiler();
JavacTask t = (JavacTask) c.getTask(null, null, null, List.of("-d", "classes"), null, files);
t.addTaskListener(new TaskListener() {
@Override
public void started(TaskEvent e) {
log.println("started: " + e);
checkTypeElement(e);
}
@Override
public void finished(TaskEvent e) {
log.println("finished: " + e);
checkTypeElement(e);
}
});
elements = t.getElements();
t.call();

if (errors > 0) {
log.println(errors + " errors occurred");
throw new Exception(errors + " errors occurred");
}
}

private void checkTypeElement(TaskEvent e) {
TypeElement te = e.getTypeElement();

if (te != null) {
showTypeElement(e.getTypeElement());
}

switch (e.getKind()) {
case COMPILATION, PARSE, ENTER -> {
checkEqual(te, null);
}

case ANALYZE, GENERATE -> {
if (te == null) {
error("type element is null");
return;
}


switch (te.getQualifiedName().toString()) {
case "m.module-info" -> {
checkEqual(elements.getModuleOf(te), elements.getModuleElement("m"));
checkEqual(elements.getPackageOf(te), null);
}
case "p.package-info", "p.C" -> {
checkEqual(elements.getModuleOf(te), elements.getModuleElement("m"));
checkEqual(elements.getPackageOf(te), elements.getPackageElement("p"));
}
}
}
}
}

private void showTypeElement(TypeElement e) {
log.println("type element: " + e);

try {
log.println(" module element: " + elements.getModuleOf(e));
} catch (Throwable t) {
log.println(" module element: " + t);
}

try {
log.println(" package element: " + elements.getPackageOf(e));
} catch (Throwable t) {
log.println(" package element: " + t);
}
}

private <T> void checkEqual(T found, T expected) {
if (found != expected) {
error("mismatch");
log.println(" found: " + found);
log.println("expected: " + expected);
}
}

private void error(String message) {
log.println("Error: " + message);
errors++;
}

private JavaFileObject createFileObject(String name, String body) {
return createFileObject(name, JavaFileObject.Kind.SOURCE, body);
}

private JavaFileObject createFileObject(String name, JavaFileObject.Kind kind, String body) {
try {
return new SimpleJavaFileObject(new URI("myfo:///" + name), kind) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return body;
}
};
} catch (URISyntaxException e) {
throw new IllegalArgumentException(name, e);
}
}
}