Skip to content

Commit

Permalink
8254023: A module declaration is not allowed to be a target of an ann…
Browse files Browse the repository at this point in the history
…otation that lacks an @target meta-annotation

Reviewed-by: jfranck, vromero
  • Loading branch information
lgxbslgx authored and Joel Borggrén-Franck committed Dec 17, 2020
1 parent ce0ab2d commit 41f312e
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected Check(Context context) {
names = Names.instance(context);
dfltTargetMeta = new Name[] { names.PACKAGE, names.TYPE,
names.FIELD, names.RECORD_COMPONENT, names.METHOD, names.CONSTRUCTOR,
names.ANNOTATION_TYPE, names.LOCAL_VARIABLE, names.PARAMETER};
names.ANNOTATION_TYPE, names.LOCAL_VARIABLE, names.PARAMETER, names.MODULE };
log = Log.instance(context);
rs = Resolve.instance(context);
syms = Symtab.instance(context);
Expand Down
29 changes: 29 additions & 0 deletions test/langtools/tools/javac/annotations/8254023/T8254023.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2020, 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 8254023
* @summary A module declaration is not allowed to be a target of an annotation that lacks an (at)Target meta-annotation
* @compile module-info.java test/A.java
*/
25 changes: 25 additions & 0 deletions test/langtools/tools/javac/annotations/8254023/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2020, 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.A
module test { }
26 changes: 26 additions & 0 deletions test/langtools/tools/javac/annotations/8254023/test/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2020, 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.
*/

package test;

public @interface A { }
62 changes: 61 additions & 1 deletion test/langtools/tools/javac/modules/AnnotationProcessing.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* @test
* @bug 8133884 8162711 8133896 8172158 8172262 8173636 8175119 8189747 8236842
* @bug 8133884 8162711 8133896 8172158 8172262 8173636 8175119 8189747 8236842 8254023
* @summary Verify that annotation processing works.
* @library /tools/lib
* @modules
Expand Down Expand Up @@ -517,6 +517,66 @@ public SourceVersion getSupportedSourceVersion() {

}

@Test
public void testAnnotationsWithoutTargetInModuleInfo(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1");

tb.writeJavaFiles(m1,
"@test.A module m1x { exports test; }",
"package test; public @interface A { }",
"package test; public @interface B { }");

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

List<String> expectedLog = List.of("Note: m1x/test.A AP Invoked",
"Note: m1x/test.A AP Invoked");

List<String> actualLog = new JavacTask(tb)
.options("-processor", AnnotationsWithoutTargetInModuleInfo.class.getName()
+ "," + AnnotationsWithoutTargetNotInModuleInfo.class.getName())
.outdir(classes)
.files(findJavaFiles(m1))
.run()
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);

tb.checkEqual(expectedLog, actualLog);
}

@SupportedAnnotationTypes("m1x/test.A")
public static final class AnnotationsWithoutTargetInModuleInfo extends AbstractProcessor {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
processingEnv.getMessager().printMessage(Kind.NOTE, "m1x/test.A AP Invoked");
return false;
}

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
}

}

@SupportedAnnotationTypes("m1x/test.B")
public static final class AnnotationsWithoutTargetNotInModuleInfo extends AbstractProcessor {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
processingEnv.getMessager().printMessage(Kind.NOTE, "m1x/test.B AP Invoked");
return false;
}

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
}

}

@Test
public void testGenerateInMultiModeAPI(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Expand Down
39 changes: 38 additions & 1 deletion test/langtools/tools/javac/modules/AnnotationsOnModules.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 8159602 8170549 8171255 8171322
* @bug 8159602 8170549 8171255 8171322 8254023
* @summary Test annotations on module declaration.
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
Expand Down Expand Up @@ -51,6 +51,7 @@
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.TypeElement;

import com.sun.tools.classfile.Annotation;
import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
Expand Down Expand Up @@ -410,6 +411,42 @@ public void testAnnotationWithImportAmbiguity(Path base) throws Exception {

}

@Test
public void testAnnotationWithoutTarget(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");

tb.writeJavaFiles(m1,
"@test.A module m1x { exports test; }",
"package test; public @interface A { }");

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

new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString())
.outdir(classes)
.files(findJavaFiles(m1))
.run()
.writeAll();

ClassFile cf = ClassFile.read(classes.resolve("m1x").resolve("module-info.class"));
var invisibleAnnotations = (RuntimeInvisibleAnnotations_attribute) cf.attributes.map.get(Attribute.RuntimeInvisibleAnnotations);

if (invisibleAnnotations == null) {
throw new AssertionError("Annotations not found!");
}
int length = invisibleAnnotations.annotations.length;
if (length != 1) {
throw new AssertionError("Incorrect number of annotations: " + length);
}
Annotation annotation = invisibleAnnotations.annotations[0];
String annotationName = cf.constant_pool.getUTF8Value(annotation.type_index).toString();
if (!"Ltest/A;".equals(annotationName)) {
throw new AssertionError("Incorrect annotation name: " + annotationName);
}
}

@Test
public void testModuleInfoAnnotationsInAPI(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Expand Down

0 comments on commit 41f312e

Please sign in to comment.