gpt4 book ai didi

java - 重复注解需要公共(public)容器吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:19:18 24 4
gpt4 key购买 nike

我注意到在使用重复注释时 Eclipse 的编译器和 javac 之间存在差异。重复注解和它的容器在同一个包中,但前者被声明为公共(public)的,而后者仍然是包私有(private)的。 Eclipse 的安排没有问题,即使在另一个包中引用了重复注释。另一方面,javac 拒绝编译,说

value() in [container] is defined in an inaccessible class or interface

我的问题是,哪一个是正确的?我在 JLS 中找不到关于此的任何规则。这是否意味着它可以解释?还是某个编译器中存在错误?

最佳答案

没有关于可访问性的声明,但规范清楚地表明,可重复注释可能被限制为仅在某些位置可重复,因为 包含注解已被声明。

JLS §9.6.3

  1. T is applicable to at least the same kinds of program element as TC (§9.6.4.1). Specifically, if the kinds of program element where T is applicable are denoted by the set m1, and the kinds of program element where TC is applicable are denoted by the set m2, then each kind in m2 must occur in m1, …

    This clause implements the policy that an annotation type may be repeatable on only some of the kinds of program element where it is applicable

这也是backed with an example :

Example 9.6.3-2. Restricting Where Annotations May Repeat

An annotation whose type declaration indicates a target of java.lang.annotation.ElementType.TYPE can appear in at least as many locations as an annotation whose type declaration indicates a target of java.lang.annotation.ElementType.ANNOTATION_TYPE. For example, given the following declarations of repeatable and containing annotation types:

@Target(ElementType.TYPE)
@Repeatable(FooContainer.class)
@interface Foo {}

@Target(ElementType.ANNOTATION_TYPE)
@Interface FooContainer {
Foo[] value();
}

@Foo can appear on any type declaration while @FooContainer can appear on only annotation type declarations. Therefore, the following annotation type declaration is legal:

@Foo @Foo
@interface X {}

while the following interface declaration is illegal:

@Foo @Foo
interface X {}

虽然这是在讨论由 @Target 而不是可访问性修饰符施加的限制,但它描述了一种也可以应用于后者的“精神”。其意图显然是,只有在指定容器注释的属性允许的情况下,才可以重复可重复注释,否则仍然可以使用该注释,但只能出现一次。

可重复注解的属性并不能覆盖容器注解类型的属性。这与所有其他地方的行为一致;即使在源代码中没有引用不可访问的注释类型,编译重复的注释也会在类文件中创建一个引用,并且类文件可能不包含对不可访问类的符号引用,并且不应该仅针对容器注释。

关于java - 重复注解需要公共(public)容器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30793047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com