Summary
Repeatable annotations without an @Target
annotation should be allowed to have a container annotation with an @Target
that explicitly includes module declarations.
Problem
Repeatable annotations are required to be applicable to at least the same kinds of program elements as their containing annotation type (JLS 9.6.3), and annotations without an @Target
are applicable to module declarations (JLS 9.6.4.1).
Repeatable annotations with a @Target
that explicitly lists all default targets except for MODULE
and whose container annotation do not have an @Target
are currently erroneously accepted by javac, and should be rejected:
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Target;
@interface TC {
T[] value() default {};
}
@Target({
ElementType.CONSTRUCTOR,
ElementType.PARAMETER,
ElementType.TYPE,
ElementType.METHOD,
ElementType.LOCAL_VARIABLE,
ElementType.PACKAGE,
ElementType.ANNOTATION_TYPE,
ElementType.FIELD,
ElementType.RECORD_COMPONENT,
})
@Repeatable(TC.class) // error: containing annotation type (TC) is applicable to more targets than repeatable annotation type (T)
@interface T {}
Repeatable annotations without an @Target
whose container annotations explicitly targets modules are currently erroneously rejected, and should be accepted by javac:
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 {}
Solution
The solution is to update javac to include module declarations in the set of default targets of repeatable annotations.
See also discussion in https://github.com/openjdk/jdk/pull/2303.
Specification
No changes to the specification are required, this behaviour is already specified by JLS 9.6.3 and 9.6.4.1.
The change to javac to reconcile it with the specification is in https://github.com/openjdk/jdk/pull/2412.
- csr of
-
JDK-8261088 Repeatable annotations without @Target cannot have containers that target module declarations
-
- Resolved
-
- relates to
-
JDK-8270916 Update java.lang.annotation.Target for changes in JLS 9.6.4.1
-
- Resolved
-