重复注释是否需要公共容器? [英] Do repeating annotations need a public container?

查看:84
本文介绍了重复注释是否需要公共容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到使用重复注释时Eclipse的编译器和javac之间存在差异.重复注释及其容器位于同一程序包中,但前者被声明为公共程序,而后者则保持程序包私有. Eclipse的布局没有问题,即使在另一个包中引用了重复的注释.另一方面,javac拒绝编译,说

I noticed a discrepancy between Eclipse's compiler and javac while using repeating annotations. The repeating annotation and its container were in the same package, but the former was declared public, while the latter remained package-private. Eclipse had no problem with the arrangement, even though the repeating annotation was referenced in another package. javac, on the other hand, refused to compile, saying

[container]中的value()是在无法访问的类或接口中定义的

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

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

My question is, which one is correct? I couldn't find any rule about this in the JLS. Does that mean it's open to interpretation? Or is there a bug in one of the compilers?

推荐答案

没有关于可访问性的声明,但规范明确指出,可重复注释可能被限制为可重复的由于声明了 注释的方式,因此仅在某些位置.

There is no statement regarding accessibility but the specification makes it clear that it is possible that a repeatable annotation may be restricted to be repeatable at certain locations only, due to the way how the containing annotation has been declared.

JLS§9.6. 3

...

  1. T至少适用于与TC相同类型的程序元素(第9.6.4.1节).具体地说,如果用集合m1表示适用T的程序元素的种类,用集合m2表示适用TC的程序元素的种类,那么m2中的每种种类都必须出现在m1中,…

  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, …

此子句实施的政策是,注释类型仅可在某些适用的程序元素上可重复使用

这也是带有示例支持:

示例9.6.3-2.限制注释可以重复的地方

其类型声明指示目标java.lang.annotation.ElementType.TYPE的注释可以至少出现在其类型声明指示目标java.lang.annotation.ElementType.ANNOTATION_TYPE的注释中.例如,给定以下可重复且包含注释类型的声明:

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可以出现在任何类型声明中,而@FooContainer只能出现在注释类型声明中.因此,以下注释类型声明是合法的:

@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施加的限制而不是可访问性修饰符,但它描述了一种精神",也可以应用于后者.显然,其意图似乎是,只有在指定的容器批注的属性允许的情况下,才可以重复可重复批注,否则仍然可以使用该批注,但只能出现一次.

While this is discussing restrictions imposed by @Target rather than accessibility modifiers, it describes a "spirit" that can be applied to the latter as well. The intention clearly seems to be that a repeatable annotation can only be repeated if the properties of the specified container annotation allow it, otherwise the annotation still may be used, but only with a single occurrence.

这并不是说可重复注释的属性能够覆盖容器注释类型的属性.这符合所有其他地方的行为;即使源代码中没有对无法访问的注释类型的引用,编译重复的注释也会在类文件中创建引用,并且类文件可能不包含对无法访问的类的符号引用,并且不应仅存在异常容器注释.

It’s not like the properties of a repeatable annotation were capable of overriding the properties of the container annotation type. This is in line with the behavior at all other places; even if there is no reference to the inaccessible annotation type in the source code, compiling the repeated annotation would create a reference in the class file and a class file may not contain symbolic references to inaccessible classes and there shouldn’t be an exception only for container annotations.

这篇关于重复注释是否需要公共容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆