diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java index c55c2db98def9..1da7f13ddf61d 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java @@ -4208,16 +4208,16 @@ List moduleDirectiveList() { while (true) { switch (token.kind) { case IDENTIFIER: - if (token.name() == names.transitive && !isTransitive) { + if (token.name() == names.transitive) { Token t1 = S.token(1); if (t1.kind == SEMI || t1.kind == DOT) { break loop; } + if (isTransitive) { + log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.RepeatedModifier); + } isTransitive = true; break; - } else if (token.name() == names.transitive && isTransitive) { - log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.RepeatedModifier); - break; } else { break loop; } diff --git a/test/langtools/tools/javac/modules/RequiresTransitiveTest.java b/test/langtools/tools/javac/modules/RequiresTransitiveTest.java index 6ff0dabec1ff9..846d1857bda39 100644 --- a/test/langtools/tools/javac/modules/RequiresTransitiveTest.java +++ b/test/langtools/tools/javac/modules/RequiresTransitiveTest.java @@ -23,6 +23,7 @@ /* * @test + * @bug 8345248 * @summary tests for "requires transitive" * @library /tools/lib * @modules @@ -34,6 +35,8 @@ import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; +import java.util.Objects; import toolbox.JavacTask; import toolbox.Task; @@ -256,4 +259,104 @@ public void testRepeatedModifiers(Path base) throws Exception { throw new Exception("expected output not found: " + e); } } + + @Test //JDK-8345248: + public void testTransitiveModuleName(Path base) throws Exception { + Path lib = base.resolve("lib"); + Path libSrc = lib.resolve("src"); + Path transitive = libSrc.resolve("transitive"); + tb.writeJavaFiles(transitive, + """ + module transitive { + } + """ + ); + Path transitiveA = libSrc.resolve("transitive.a"); + tb.writeJavaFiles(transitiveA, + """ + module transitive.a { + } + """ + ); + + Path libClasses = lib.resolve("classes"); + Files.createDirectories(libClasses); + + new JavacTask(tb, Task.Mode.CMDLINE) + .options("--module-source-path", libSrc.toString()) + .files(findJavaFiles(libSrc)) + .outdir(libClasses) + .run() + .writeAll(); + + Path src = base.resolve("src"); + Path classes = base.resolve("classes"); + + Files.createDirectories(classes); + + tb.writeJavaFiles(src, + """ + module m { + requires transitive; + requires transitive.a; + } + """ + ); + + new JavacTask(tb, Task.Mode.CMDLINE) + .options("--module-path", libClasses.toString()) + .sourcepath(src) + .files(findJavaFiles(src)) + .outdir(classes) + .run() + .writeAll(); + + tb.writeJavaFiles(src, + """ + module m { + requires transitive transitive; + requires transitive transitive.a; + } + """ + ); + + new JavacTask(tb, Task.Mode.CMDLINE) + .options("--module-path", libClasses.toString()) + .sourcepath(src) + .files(findJavaFiles(src)) + .outdir(classes) + .run() + .writeAll(); + + tb.writeJavaFiles(src, + """ + module m { + requires transitive transitive transitive; + requires transitive transitive transitive.a; + } + """ + ); + + List log = new JavacTask(tb, Task.Mode.CMDLINE) + .options("--module-path", libClasses.toString(), + "-XDrawDiagnostics") + .sourcepath(src) + .files(findJavaFiles(src)) + .outdir(classes) + .run() + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + + List expected = List.of( + "module-info.java:2:25: compiler.err.repeated.modifier", + "module-info.java:3:25: compiler.err.repeated.modifier", + "2 errors" + ); + + if (!Objects.equals(expected, log)) { + throw new Exception("expected: " + expected + + ", but got: " + log); + } + } }