Skip to content
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

8341966: Broken annotated module may lead to an exception in javac #192

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 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
Expand Down Expand Up @@ -2174,7 +2174,7 @@ public void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy) {

Type resolvePossibleProxyType(Type t) {
if (t instanceof ProxyType proxyType) {
Assert.check(requestingOwner.owner.kind == MDL);
Assert.check(requestingOwner.owner instanceof ModuleSymbol);
ModuleSymbol prevCurrentModule = currentModule;
currentModule = (ModuleSymbol) requestingOwner.owner;
try {
Expand Down
94 changes: 92 additions & 2 deletions test/langtools/tools/javac/modules/AnnotationsOnModules.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 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
Expand All @@ -23,7 +23,7 @@

/*
* @test
* @bug 8159602 8170549 8171255 8171322 8254023
* @bug 8159602 8170549 8171255 8171322 8254023 8341966
* @summary Test annotations on module declaration.
* @library /tools/lib
* @enablePreview
Expand All @@ -35,8 +35,10 @@
*/

import java.io.File;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand All @@ -55,6 +57,7 @@
import java.lang.classfile.*;
import java.lang.classfile.ClassFile;
import java.lang.classfile.attribute.*;
import java.lang.reflect.AccessFlag;
import toolbox.JavacTask;
import toolbox.Task;
import toolbox.Task.OutputKind;
Expand Down Expand Up @@ -726,6 +729,93 @@ public TestCase(String extraDecl, String decl, String use, String expectedAnnota
}
}

@Test
public void testBrokenModuleInfoClassWithAnnotation(Path base) throws Exception {
Path lib = base.resolve("lib");
tb.writeJavaFiles(lib,
"""
@Deprecated
module m{}
""");

Path libClasses = base.resolve("lib-classes");
Files.createDirectories(libClasses);

new JavacTask(tb)
.options("--release", "21")
.outdir(libClasses)
.files(findJavaFiles(lib))
.run()
.writeAll();

Path modifiedModuleInfo = libClasses.resolve("module-info.class");
ClassModel cm1 = ClassFile.of().parse(modifiedModuleInfo);
byte[] newBytes = ClassFile.of().transform(cm1, (builder, element) -> {
if (element instanceof ModuleAttribute attr) {
List<ModuleRequireInfo> requires = new ArrayList<>();

for (ModuleRequireInfo mri : attr.requires()) {
if (mri.requires().name().equalsString("java.base")) {
requires.add(ModuleRequireInfo.of(mri.requires(),
List.of(AccessFlag.TRANSITIVE),
mri.requiresVersion()
.orElse(null)));
} else {
requires.add(mri);
}
}

builder.accept(ModuleAttribute.of(attr.moduleName(),
attr.moduleFlagsMask(),
attr.moduleVersion()
.orElseGet(() -> null),
requires,
attr.exports(),
attr.opens(),
attr.uses(),
attr.provides()));
} else {
builder.accept(element);
}
});

try (OutputStream out = Files.newOutputStream(modifiedModuleInfo)) {
out.write(newBytes);
}

Path src = base.resolve("src");
Path classes = base.resolve("classes");

tb.writeJavaFiles(src,
"""
public class C {}
""");

Files.createDirectories(classes);

List<String> actualErrors =
new JavacTask(tb)
.options("--module-path", libClasses.toString(),
"--add-modules", "m",
"-XDshould-stop.at=FLOW",
"-XDdev",
"-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(OutputKind.DIRECT);
List<String> expectedErrors = List.of(
"- compiler.err.cant.access: m.module-info, (compiler.misc.bad.class.file.header: module-info.class, (compiler.misc.bad.requires.flag: ACC_TRANSITIVE (0x0020))",
"1 error"
);

if (!expectedErrors.equals(actualErrors)) {
throw new AssertionError("Unexpected errors, expected: " + expectedErrors +
", but got: " + actualErrors);
}
}

private static final String OPT_EXPECTED_ANNOTATIONS = "expectedAnnotations";

@SupportedAnnotationTypes("*")
Expand Down