Skip to content

Commit

Permalink
8341966: Broken annotated module may lead to an exception in javac
Browse files Browse the repository at this point in the history
  • Loading branch information
lahodaj committed Oct 11, 2024
1 parent 6133866 commit c93ad26
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
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 @@ -2192,7 +2192,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
97 changes: 95 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,96 @@ 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().transformClass(cm1, new ClassTransform() {
@Override
public void accept(ClassBuilder builder, ClassElement 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

0 comments on commit c93ad26

Please sign in to comment.