Skip to content
Permalink
Browse files
8261088: Repeatable annotations without @target cannot have container…
…s that target module declarations

Backport-of: 60c11fef006124e6c2be6d958c78dc344bb777d5
  • Loading branch information
jddarcy committed Aug 17, 2021
1 parent e506cb2 commit cb7e311dc68b4d8c7eeba5697a9a48abb0a05cff
@@ -119,9 +119,6 @@ protected Check(Context context) {
context.put(checkKey, this);

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.MODULE };
log = Log.instance(context);
rs = Resolve.instance(context);
syms = Symtab.instance(context);
@@ -161,6 +158,7 @@ protected Check(Context context) {

deferredLintHandler = DeferredLintHandler.instance(context);

allowModules = Feature.MODULES.allowedInSource(source);
allowRecords = Feature.RECORDS.allowedInSource(source);
allowSealed = Feature.SEALED_CLASSES.allowedInSource(source);
}
@@ -194,6 +192,10 @@ protected Check(Context context) {
*/
private DeferredLintHandler deferredLintHandler;

/** Are modules allowed
*/
private final boolean allowModules;

/** Are records allowed
*/
private final boolean allowRecords;
@@ -3215,20 +3217,7 @@ private void validateTarget(TypeSymbol container, TypeSymbol contained, Diagnost
/* get a set of names for the default target */
private Set<Name> getDefaultTargetSet() {
if (defaultTargets == null) {
Set<Name> targets = new HashSet<>();
targets.add(names.ANNOTATION_TYPE);
targets.add(names.CONSTRUCTOR);
targets.add(names.FIELD);
if (allowRecords) {
targets.add(names.RECORD_COMPONENT);
}
targets.add(names.LOCAL_VARIABLE);
targets.add(names.METHOD);
targets.add(names.PACKAGE);
targets.add(names.PARAMETER);
targets.add(names.TYPE);

defaultTargets = java.util.Collections.unmodifiableSet(targets);
defaultTargets = Set.of(defaultTargetMetaInfo());
}

return defaultTargets;
@@ -3428,8 +3417,26 @@ boolean annotationApplicable(JCAnnotation a, Symbol s) {
return (atValue instanceof Attribute.Array attributeArray) ? attributeArray : null;
}

public final Name[] dfltTargetMeta;
private Name[] dfltTargetMeta;
private Name[] defaultTargetMetaInfo() {
if (dfltTargetMeta == null) {
ArrayList<Name> defaultTargets = new ArrayList<>();
defaultTargets.add(names.PACKAGE);
defaultTargets.add(names.TYPE);
defaultTargets.add(names.FIELD);
defaultTargets.add(names.METHOD);
defaultTargets.add(names.CONSTRUCTOR);
defaultTargets.add(names.ANNOTATION_TYPE);
defaultTargets.add(names.LOCAL_VARIABLE);
defaultTargets.add(names.PARAMETER);
if (allowRecords) {
defaultTargets.add(names.RECORD_COMPONENT);
}
if (allowModules) {
defaultTargets.add(names.MODULE);
}
dfltTargetMeta = defaultTargets.toArray(new Name[0]);
}
return dfltTargetMeta;
}

@@ -0,0 +1,41 @@
/*
* Copyright (c) 2021, 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 8261088
* @summary Repeatable annotations without Target cannot have containers that target module declarations
* @compile T8261088.java
*/

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Target;

@Target(ElementType.MODULE)
@interface TC {
T[] value() default {};
}

@Repeatable(TC.class)
@interface T {}
@@ -25,7 +25,7 @@

/**
* @test
* @bug 8006547
* @bug 8006547 8261088
* @compile NoTargetOnContainer.java
*/

@@ -42,7 +42,8 @@
ElementType.PACKAGE,
ElementType.ANNOTATION_TYPE,
ElementType.FIELD,
ElementType.RECORD_COMPONENT
ElementType.RECORD_COMPONENT,
ElementType.MODULE,
})
@Repeatable(FooContainer.class)
@interface Foo {}
@@ -25,7 +25,7 @@

/**
* @test
* @bug 8006547
* @bug 8006547 8261088
* @compile NoTargetOnContainer2.java
*/

@@ -44,7 +44,8 @@
ElementType.FIELD,
ElementType.TYPE_USE,
ElementType.TYPE_PARAMETER,
ElementType.RECORD_COMPONENT
ElementType.RECORD_COMPONENT,
ElementType.MODULE,
})
@Repeatable(FooContainer.class)
@interface Foo {}

1 comment on commit cb7e311

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on cb7e311 Aug 17, 2021

Please sign in to comment.