8254023: A module declaration is not allowed to be a target of an annotation that lacks an @Target meta-annotation

Reviewed-by: jfranck, vromero
This commit is contained in:
Guoxiong Li 2020-12-17 08:06:35 +00:00 committed by Joel Borggrén-Franck
parent ce0ab2dd84
commit 41f312eb64
6 changed files with 180 additions and 3 deletions

View file

@ -119,7 +119,7 @@ public class Check {
names = Names.instance(context); names = Names.instance(context);
dfltTargetMeta = new Name[] { names.PACKAGE, names.TYPE, dfltTargetMeta = new Name[] { names.PACKAGE, names.TYPE,
names.FIELD, names.RECORD_COMPONENT, names.METHOD, names.CONSTRUCTOR, 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); log = Log.instance(context);
rs = Resolve.instance(context); rs = Resolve.instance(context);
syms = Symtab.instance(context); syms = Symtab.instance(context);

View file

@ -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
*/

View file

@ -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 { }

View file

@ -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 { }

View file

@ -23,7 +23,7 @@
/** /**
* @test * @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. * @summary Verify that annotation processing works.
* @library /tools/lib * @library /tools/lib
* @modules * @modules
@ -517,6 +517,66 @@ public class AnnotationProcessing extends ModuleTestBase {
} }
@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 @Test
public void testGenerateInMultiModeAPI(Path base) throws Exception { public void testGenerateInMultiModeAPI(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src"); Path moduleSrc = base.resolve("module-src");

View file

@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 8159602 8170549 8171255 8171322 * @bug 8159602 8170549 8171255 8171322 8254023
* @summary Test annotations on module declaration. * @summary Test annotations on module declaration.
* @library /tools/lib * @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api * @modules jdk.compiler/com.sun.tools.javac.api
@ -51,6 +51,7 @@ import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ModuleElement; import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
import com.sun.tools.classfile.Annotation;
import com.sun.tools.classfile.Attribute; import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.ClassFile; import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute; import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
@ -410,6 +411,42 @@ public class AnnotationsOnModules extends ModuleTestBase {
} }
@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 @Test
public void testModuleInfoAnnotationsInAPI(Path base) throws Exception { public void testModuleInfoAnnotationsInAPI(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src"); Path moduleSrc = base.resolve("module-src");