Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 cb7e311
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 22 deletions.
43 changes: 25 additions & 18 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -3428,8 +3417,26 @@ Attribute.Array getAttributeTargetAttribute(TypeSymbol 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;
}

Expand Down
41 changes: 41 additions & 0 deletions test/langtools/tools/javac/annotations/8261088/T8261088.java
@@ -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 {}
Expand Up @@ -25,7 +25,7 @@

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

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

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

Expand All @@ -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 {}
Expand Down

1 comment on commit cb7e311

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.