8303784: no-@Target annotations should be applicable to type parameter declarations

Reviewed-by: vromero
This commit is contained in:
Liam Miller-Cushon 2023-05-02 18:19:03 +00:00
parent b76f320e76
commit 8c106b0c8e
4 changed files with 49 additions and 1 deletions

View File

@ -226,6 +226,7 @@ public enum Source {
SWITCH_MULTIPLE_CASE_LABELS(JDK14, Fragments.FeatureMultipleCaseLabels, DiagKind.PLURAL),
SWITCH_RULE(JDK14, Fragments.FeatureSwitchRules, DiagKind.PLURAL),
SWITCH_EXPRESSION(JDK14, Fragments.FeatureSwitchExpressions, DiagKind.PLURAL),
NO_TARGET_ANNOTATION_APPLICABILITY(JDK14),
TEXT_BLOCKS(JDK15, Fragments.FeatureTextBlocks, DiagKind.PLURAL),
PATTERN_MATCHING_IN_INSTANCEOF(JDK16, Fragments.FeaturePatternMatchingInstanceof, DiagKind.NORMAL),
REIFIABLE_TYPES_INSTANCEOF(JDK16, Fragments.FeatureReifiableTypesInstanceof, DiagKind.PLURAL),

View File

@ -3513,7 +3513,7 @@ public class Check {
protected boolean isTypeAnnotation(JCAnnotation a, boolean isTypeParameter) {
List<Attribute> targets = typeAnnotations.annotationTargets(a.annotationType.type.tsym);
return (targets == null) ?
false :
(Feature.NO_TARGET_ANNOTATION_APPLICABILITY.allowedInSource(source) && isTypeParameter) :
targets.stream()
.anyMatch(attr -> isTypeAnnotation(attr, isTypeParameter));
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2023, Alphabet LLC. 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 8303784
* @summary no-@Target annotations should be applicable to type parameter declarations
* @compile/fail/ref=NoTargetOnTypeParameterDeclaration.out --release 13 -XDrawDiagnostics NoTargetOnTypeParameterDeclaration.java
* @compile --release 14 NoTargetOnTypeParameterDeclaration.java
* @compile NoTargetOnTypeParameterDeclaration.java
*/
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Retention;
class NoTargetOnTypeParameterDeclaration {
@Retention(RetentionPolicy.RUNTIME)
@interface A {}
class B<@A X> {
<@A Y> void f() {}
}
}

View File

@ -0,0 +1,3 @@
NoTargetOnTypeParameterDeclaration.java:41:11: compiler.err.annotation.type.not.applicable.to.type: NoTargetOnTypeParameterDeclaration.A
NoTargetOnTypeParameterDeclaration.java:42:6: compiler.err.annotation.type.not.applicable.to.type: NoTargetOnTypeParameterDeclaration.A
2 errors