Wed, 13 Mar 2013 22:03:09 +0100
8006547: Repeating annotations: No Target on container annotation with all targets on base annotation gives compiler error
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java Tue Mar 12 17:39:34 2013 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java Wed Mar 13 22:03:09 2013 +0100 1.3 @@ -2779,25 +2779,17 @@ 1.4 } 1.5 1.6 private void validateTarget(Symbol container, Symbol contained, DiagnosticPosition pos) { 1.7 - Attribute.Array containedTarget = getAttributeTargetAttribute(contained); 1.8 - 1.9 - // If contained has no Target, we are done 1.10 - if (containedTarget == null) { 1.11 - return; 1.12 - } 1.13 - 1.14 - // If contained has Target m1, container must have a Target 1.15 - // annotation, m2, and m2 must be a subset of m1. (This is 1.16 - // trivially true if contained has no target as per above). 1.17 - 1.18 - // contained has target, but container has not, error 1.19 + // The set of targets the container is applicable to must be a subset 1.20 + // (with respect to annotation target semantics) of the set of targets 1.21 + // the contained is applicable to. The target sets may be implicit or 1.22 + // explicit. 1.23 + 1.24 + Set<Name> containerTargets; 1.25 Attribute.Array containerTarget = getAttributeTargetAttribute(container); 1.26 if (containerTarget == null) { 1.27 - log.error(pos, "invalid.repeatable.annotation.incompatible.target", container, contained); 1.28 - return; 1.29 - } 1.30 - 1.31 - Set<Name> containerTargets = new HashSet<Name>(); 1.32 + containerTargets = getDefaultTargetSet(); 1.33 + } else { 1.34 + containerTargets = new HashSet<Name>(); 1.35 for (Attribute app : containerTarget.values) { 1.36 if (!(app instanceof Attribute.Enum)) { 1.37 continue; // recovery 1.38 @@ -2805,8 +2797,14 @@ 1.39 Attribute.Enum e = (Attribute.Enum)app; 1.40 containerTargets.add(e.value.name); 1.41 } 1.42 - 1.43 - Set<Name> containedTargets = new HashSet<Name>(); 1.44 + } 1.45 + 1.46 + Set<Name> containedTargets; 1.47 + Attribute.Array containedTarget = getAttributeTargetAttribute(contained); 1.48 + if (containedTarget == null) { 1.49 + containedTargets = getDefaultTargetSet(); 1.50 + } else { 1.51 + containedTargets = new HashSet<Name>(); 1.52 for (Attribute app : containedTarget.values) { 1.53 if (!(app instanceof Attribute.Enum)) { 1.54 continue; // recovery 1.55 @@ -2814,20 +2812,42 @@ 1.56 Attribute.Enum e = (Attribute.Enum)app; 1.57 containedTargets.add(e.value.name); 1.58 } 1.59 - 1.60 - if (!isTargetSubset(containedTargets, containerTargets)) { 1.61 + } 1.62 + 1.63 + if (!isTargetSubsetOf(containerTargets, containedTargets)) { 1.64 log.error(pos, "invalid.repeatable.annotation.incompatible.target", container, contained); 1.65 } 1.66 } 1.67 1.68 - /** Checks that t is a subset of s, with respect to ElementType 1.69 + /* get a set of names for the default target */ 1.70 + private Set<Name> getDefaultTargetSet() { 1.71 + if (defaultTargets == null) { 1.72 + Set<Name> targets = new HashSet<Name>(); 1.73 + targets.add(names.ANNOTATION_TYPE); 1.74 + targets.add(names.CONSTRUCTOR); 1.75 + targets.add(names.FIELD); 1.76 + targets.add(names.LOCAL_VARIABLE); 1.77 + targets.add(names.METHOD); 1.78 + targets.add(names.PACKAGE); 1.79 + targets.add(names.PARAMETER); 1.80 + targets.add(names.TYPE); 1.81 + 1.82 + defaultTargets = java.util.Collections.unmodifiableSet(targets); 1.83 + } 1.84 + 1.85 + return defaultTargets; 1.86 + } 1.87 + private Set<Name> defaultTargets; 1.88 + 1.89 + 1.90 + /** Checks that s is a subset of t, with respect to ElementType 1.91 * semantics, specifically {ANNOTATION_TYPE} is a subset of {TYPE} 1.92 */ 1.93 - private boolean isTargetSubset(Set<Name> s, Set<Name> t) { 1.94 - // Check that all elements in t are present in s 1.95 - for (Name n2 : t) { 1.96 + private boolean isTargetSubsetOf(Set<Name> s, Set<Name> t) { 1.97 + // Check that all elements in s are present in t 1.98 + for (Name n2 : s) { 1.99 boolean currentElementOk = false; 1.100 - for (Name n1 : s) { 1.101 + for (Name n1 : t) { 1.102 if (n1 == n2) { 1.103 currentElementOk = true; 1.104 break;
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/DefaultTarget.java Wed Mar 13 22:03:09 2013 +0100 2.3 @@ -0,0 +1,47 @@ 2.4 +/* 2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +import java.lang.annotation.*; 2.28 + 2.29 +/** 2.30 + * @test 2.31 + * @bug 8006547 2.32 + * @compile DefaultTarget.java 2.33 + */ 2.34 + 2.35 +@Target({ 2.36 + ElementType.CONSTRUCTOR, 2.37 + ElementType.PARAMETER, 2.38 + ElementType.TYPE, 2.39 + ElementType.METHOD, 2.40 + ElementType.LOCAL_VARIABLE, 2.41 + ElementType.PACKAGE, 2.42 + ElementType.ANNOTATION_TYPE, 2.43 + ElementType.FIELD, 2.44 +}) 2.45 +@interface Container { 2.46 + DefaultTarget[] value(); 2.47 +} 2.48 + 2.49 +@Repeatable(Container.class) 2.50 +public @interface DefaultTarget {}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/DefaultTargetTypeParameter.java Wed Mar 13 22:03:09 2013 +0100 3.3 @@ -0,0 +1,40 @@ 3.4 +/* 3.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +import java.lang.annotation.*; 3.28 + 3.29 +/** 3.30 + * @test 3.31 + * @bug 8006547 3.32 + * @compile/fail/ref=DefaultTargetTypeParameter.out -XDrawDiagnostics DefaultTargetTypeParameter.java 3.33 + */ 3.34 + 3.35 +@Target({ 3.36 + ElementType.TYPE_PARAMETER, 3.37 +}) 3.38 +@interface Container { 3.39 + DefaultTargetTypeParameter[] value(); 3.40 +} 3.41 + 3.42 +@Repeatable(Container.class) 3.43 +public @interface DefaultTargetTypeParameter {}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/DefaultTargetTypeParameter.out Wed Mar 13 22:03:09 2013 +0100 4.3 @@ -0,0 +1,2 @@ 4.4 +DefaultTargetTypeParameter.java:39:1: compiler.err.invalid.repeatable.annotation.incompatible.target: Container, DefaultTargetTypeParameter 4.5 +1 error
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/DefaultTargetTypeUse.java Wed Mar 13 22:03:09 2013 +0100 5.3 @@ -0,0 +1,40 @@ 5.4 +/* 5.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. 5.11 + * 5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 + * version 2 for more details (a copy is included in the LICENSE file that 5.16 + * accompanied this code). 5.17 + * 5.18 + * You should have received a copy of the GNU General Public License version 5.19 + * 2 along with this work; if not, write to the Free Software Foundation, 5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 + * 5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.23 + * or visit www.oracle.com if you need additional information or have any 5.24 + * questions. 5.25 + */ 5.26 + 5.27 +import java.lang.annotation.*; 5.28 + 5.29 +/** 5.30 + * @test 5.31 + * @bug 8006547 5.32 + * @compile/fail/ref=DefaultTargetTypeUse.out -XDrawDiagnostics DefaultTargetTypeUse.java 5.33 + */ 5.34 + 5.35 +@Target({ 5.36 + ElementType.TYPE_USE, 5.37 +}) 5.38 +@interface Container { 5.39 + DefaultTargetTypeUse[] value(); 5.40 +} 5.41 + 5.42 +@Repeatable(Container.class) 5.43 +public @interface DefaultTargetTypeUse {}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/DefaultTargetTypeUse.out Wed Mar 13 22:03:09 2013 +0100 6.3 @@ -0,0 +1,2 @@ 6.4 +DefaultTargetTypeUse.java:39:1: compiler.err.invalid.repeatable.annotation.incompatible.target: Container, DefaultTargetTypeUse 6.5 +1 error
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/NoTargetOnContainer.java Wed Mar 13 22:03:09 2013 +0100 7.3 @@ -0,0 +1,49 @@ 7.4 +/* 7.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 + * 7.8 + * This code is free software; you can redistribute it and/or modify it 7.9 + * under the terms of the GNU General Public License version 2 only, as 7.10 + * published by the Free Software Foundation. 7.11 + * 7.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 7.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.15 + * version 2 for more details (a copy is included in the LICENSE file that 7.16 + * accompanied this code). 7.17 + * 7.18 + * You should have received a copy of the GNU General Public License version 7.19 + * 2 along with this work; if not, write to the Free Software Foundation, 7.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.21 + * 7.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.23 + * or visit www.oracle.com if you need additional information or have any 7.24 + * questions. 7.25 + */ 7.26 + 7.27 +import java.lang.annotation.*; 7.28 + 7.29 +/** 7.30 + * @test 7.31 + * @bug 8006547 7.32 + * @compile NoTargetOnContainer.java 7.33 + */ 7.34 + 7.35 +@interface FooContainer { 7.36 + Foo[] value(); 7.37 +} 7.38 + 7.39 +@Target({ 7.40 + ElementType.CONSTRUCTOR, 7.41 + ElementType.PARAMETER, 7.42 + ElementType.TYPE, 7.43 + ElementType.METHOD, 7.44 + ElementType.LOCAL_VARIABLE, 7.45 + ElementType.PACKAGE, 7.46 + ElementType.ANNOTATION_TYPE, 7.47 + ElementType.FIELD, 7.48 +}) 7.49 +@Repeatable(FooContainer.class) 7.50 +@interface Foo {} 7.51 + 7.52 +class NoTargetOnContainer {}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/NoTargetOnContainer2.java Wed Mar 13 22:03:09 2013 +0100 8.3 @@ -0,0 +1,50 @@ 8.4 +/* 8.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.7 + * 8.8 + * This code is free software; you can redistribute it and/or modify it 8.9 + * under the terms of the GNU General Public License version 2 only, as 8.10 + * published by the Free Software Foundation. 8.11 + * 8.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 8.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 8.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 8.15 + * version 2 for more details (a copy is included in the LICENSE file that 8.16 + * accompanied this code). 8.17 + * 8.18 + * You should have received a copy of the GNU General Public License version 8.19 + * 2 along with this work; if not, write to the Free Software Foundation, 8.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 8.21 + * 8.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 8.23 + * or visit www.oracle.com if you need additional information or have any 8.24 + * questions. 8.25 + */ 8.26 + 8.27 +import java.lang.annotation.*; 8.28 + 8.29 +/** 8.30 + * @test 8.31 + * @bug 8006547 8.32 + * @compile NoTargetOnContainer2.java 8.33 + */ 8.34 + 8.35 +@interface FooContainer { 8.36 + Foo[] value(); 8.37 +} 8.38 + 8.39 +@Target({ 8.40 + ElementType.CONSTRUCTOR, 8.41 + ElementType.PARAMETER, 8.42 + ElementType.TYPE, 8.43 + ElementType.METHOD, 8.44 + ElementType.LOCAL_VARIABLE, 8.45 + ElementType.PACKAGE, 8.46 + ElementType.ANNOTATION_TYPE, 8.47 + ElementType.FIELD, 8.48 + ElementType.TYPE_USE, 8.49 + ElementType.TYPE_PARAMETER}) 8.50 +@Repeatable(FooContainer.class) 8.51 +@interface Foo {} 8.52 + 8.53 +class NoTargetOnContainer2 {}