8261088: Repeatable annotations without @Target cannot have containers that target module declarations

Backport-of: 60c11fef00
(cherry picked from commit cb7e311dc6)
This commit is contained in:
Joe Darcy
2021-08-17 22:49:33 +00:00
committed by Vitaly Provodin
parent 9a7c6bea31
commit f0c0b86f90
4 changed files with 72 additions and 22 deletions

View File

@@ -119,9 +119,6 @@ public class Check {
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 @@ public class Check {
deferredLintHandler = DeferredLintHandler.instance(context);
allowModules = Feature.MODULES.allowedInSource(source);
allowRecords = Feature.RECORDS.allowedInSource(source);
allowSealed = Feature.SEALED_CLASSES.allowedInSource(source);
}
@@ -194,6 +192,10 @@ public class Check {
*/
private DeferredLintHandler deferredLintHandler;
/** Are modules allowed
*/
private final boolean allowModules;
/** Are records allowed
*/
private final boolean allowRecords;
@@ -3215,20 +3217,7 @@ public class Check {
/* 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 @@ public class Check {
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;
}

View File

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

View File

@@ -25,7 +25,7 @@ import java.lang.annotation.*;
/**
* @test
* @bug 8006547
* @bug 8006547 8261088
* @compile NoTargetOnContainer.java
*/
@@ -42,7 +42,8 @@ import java.lang.annotation.*;
ElementType.PACKAGE,
ElementType.ANNOTATION_TYPE,
ElementType.FIELD,
ElementType.RECORD_COMPONENT
ElementType.RECORD_COMPONENT,
ElementType.MODULE,
})
@Repeatable(FooContainer.class)
@interface Foo {}

View File

@@ -25,7 +25,7 @@ import java.lang.annotation.*;
/**
* @test
* @bug 8006547
* @bug 8006547 8261088
* @compile NoTargetOnContainer2.java
*/
@@ -44,7 +44,8 @@ import java.lang.annotation.*;
ElementType.FIELD,
ElementType.TYPE_USE,
ElementType.TYPE_PARAMETER,
ElementType.RECORD_COMPONENT
ElementType.RECORD_COMPONENT,
ElementType.MODULE,
})
@Repeatable(FooContainer.class)
@interface Foo {}