From 03c4595173d564df97aa4f2b3156adcd4be379fa Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Thu, 7 Dec 2023 09:09:19 +0000 Subject: [PATCH] 8321164: javac with annotation processor throws AssertionError: Filling jrt:/... during JarFileObject[/...] Reviewed-by: vromero, mcimadamore --- .../com/sun/tools/javac/code/Types.java | 26 ++-- .../ReadingMethodWithTypeAnno.java | 118 ++++++++++++++++++ 2 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 test/langtools/tools/javac/annotations/ReadingMethodWithTypeAnno.java diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java index 7783c7fb4f4..d6ff37b3e53 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java @@ -2407,20 +2407,26 @@ public class Types { } // where private TypeMapping erasure = new StructuralTypeMapping() { + @SuppressWarnings("fallthrough") private Type combineMetadata(final Type s, final Type t) { if (t.getMetadata().nonEmpty()) { - switch (s.getKind()) { - case OTHER: - case UNION: - case INTERSECTION: - case PACKAGE: - case EXECUTABLE: - case NONE: - case VOID: - case ERROR: + switch (s.getTag()) { + case CLASS: + if (s instanceof UnionClassType || + s instanceof IntersectionClassType) { + return s; + } + //fall-through + case BYTE, CHAR, SHORT, LONG, FLOAT, INT, DOUBLE, BOOLEAN, + ARRAY, MODULE, TYPEVAR, WILDCARD, BOT: + return s.dropMetadata(Annotations.class); + case VOID, METHOD, PACKAGE, FORALL, DEFERRED, + NONE, ERROR, UNKNOWN, UNDETVAR, UNINITIALIZED_THIS, + UNINITIALIZED_OBJECT: return s; - default: return s.dropMetadata(Annotations.class); + default: + throw new AssertionError(s.getTag().name()); } } else { return s; diff --git a/test/langtools/tools/javac/annotations/ReadingMethodWithTypeAnno.java b/test/langtools/tools/javac/annotations/ReadingMethodWithTypeAnno.java new file mode 100644 index 00000000000..d7436156554 --- /dev/null +++ b/test/langtools/tools/javac/annotations/ReadingMethodWithTypeAnno.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2023, 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 8321164 + * @summary Indirectly verify that types.erasure does not complete, called from + * ClassReader.isSameBinaryType, which must not complete. + * @library /tools/lib + * @modules jdk.compiler/com.sun.tools.javac.api + * jdk.compiler/com.sun.tools.javac.main + * @build toolbox.JavacTask toolbox.TestRunner toolbox.ToolBox + * @run main ReadingMethodWithTypeAnno + */ + +import com.sun.source.util.TaskEvent; +import com.sun.source.util.TaskListener; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import toolbox.JavacTask; +import toolbox.Task.Expect; +import toolbox.Task.OutputKind; +import toolbox.TestRunner; +import toolbox.ToolBox; + +public class ReadingMethodWithTypeAnno extends TestRunner { + public static void main(String... args) throws Exception { + ReadingMethodWithTypeAnno r = new ReadingMethodWithTypeAnno(); + r.runTests(m -> new Object[] { Paths.get(m.getName()) }); + } + + private final ToolBox tb = new ToolBox(); + + public ReadingMethodWithTypeAnno() throws IOException { + super(System.err); + } + + @Test + public void test_DeclNone_UseNone(Path base) throws IOException { + Path libSrc = base.resolve("lib-src"); + Path libClasses = Files.createDirectories(base.resolve("lib-classes")); + + tb.writeJavaFiles(libSrc, + """ + public class Lib { + public void test(java.lang.@Ann String s) { + new Object() {}; + } + } + """, + """ + import java.lang.annotation.ElementType; + import java.lang.annotation.Target; + @Target(ElementType.TYPE_USE) + public @interface Ann {} + """); + + new JavacTask(tb) + .outdir(libClasses) + .files(tb.findJavaFiles(libSrc)) + .run(Expect.SUCCESS) + .writeAll() + .getOutput(OutputKind.DIRECT); + + Path src = base.resolve("src"); + Path classes = Files.createDirectories(base.resolve("classes")); + + tb.writeJavaFiles(src, + """ + public class Test { + } + """); + + new JavacTask(tb) + .outdir(classes) + .classpath(libClasses) + .files(tb.findJavaFiles(src)) + .callback(task -> { + task.addTaskListener(new TaskListener() { + @Override + public void finished(TaskEvent e) { + if (e.getKind() == TaskEvent.Kind.ENTER) { + task.getElements().getTypeElement("Lib"); + task.getElements().getTypeElement("Lib$1"); + } + } + }); + }) + .run(Expect.SUCCESS) + .writeAll() + .getOutput(OutputKind.DIRECT); + } + +} +