标记注释与标记接口 [英] Marker Annotation vs Marker Interface

查看:48
本文介绍了标记注释与标记接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 Marker 接口时,我偶然发现了以下网站:第 37 项:使用标记接口定义类型
在这里,根据 Joshua Bloch 的说法,Marker 接口相对于 Marker 注释有两个优点.

<块引用>

  1. 标记接口定义了一个由实例实现的类型被标记的班级;标记注释没有.这种存在type 允许您在编译时捕获您无法捕获的错误如果您使用了标记注释,则捕获直到运行时.

  2. 标记接口相对于标记注释的另一个优点是他们可以更精确地定位.如果注释类型是用目标 ElementType.TYPE 声明,它可以应用于任何类或接口.假设您有一个适用的标记仅适用于特定接口的实现.如果你定义它作为一个标记界面,你可以让它将唯一的界面扩展到它适用,保证所有标记的类型也是它适用的唯一接口的子类型.

好的,第一点被理解,但我不确定我是否正确理解第二点:

<块引用>

如果一个注解类型是用目标ElementType.TYPE声明的,它可以应用于任何类或接口.

同样,如果我有一个标记接口,那么它也可以应用于任何类或接口.标记注释和标记接口不是说同样的事情吗?如何更精确地定位标记界面?

第二点还提到:

<块引用>

你可以让[标记接口]扩展它适用的唯一接口,保证所有标记的类型也是它适用的唯一接口的子类型.

您不能通过使用 @Inherited 元注释通过注释来实现这一点吗?

解决方案

如何更精确地定位标记界面?

您说得对,两者都可以应用于任何类型.通过更精确地定位",作者意味着您可以添加额外的限制来标记界面可以应用于哪些特定类型.不可能对注释添加相同的精确限制:如果注释仅限于 ElementType.TYPE,则它始终可以应用于所有类型.

第二点的另一部分详细介绍了如何添加这些限制.如果你有一个标记接口,你可以让它像这样扩展另一个接口(作者称之为唯一接口):

interface Marker extends Foo { }

标记现在只能应用于实现 Foo 的类型.

<块引用>

您不能通过使用 @Inherited 元注释通过注释来实现这一点吗?

不,@Inherited 元注释仅意味着被注释类的任何子类型都将被视为它也具有相同的注释.它对注释可以应用于哪些类型没有任何限制.

While reading about Marker interfaces I stumbled upon the following site : Item 37: Use marker interfaces to define types
Here, according to Joshua Bloch there are two advantages of Marker interfaces over the Marker annotations.

  1. Marker interfaces define a type that is implemented by instances of the marked class; marker annotations do not. The existence of this type allows you to catch errors at compile time that you couldn’t catch until runtime if you used a marker annotation.

  2. Another advantage of marker interfaces over marker annotations is that they can be targeted more precisely. If an annotation type is declared with target ElementType.TYPE, it can be applied to any class or interface. Suppose you have a marker that is applicable only to implementations of a particular interface. If you define it as a marker interface, you can have it extend the sole interface to which it is applicable, guaranteeing that all marked types are also subtypes of the sole interface to which it is applicable.

OK, First point is understood but I'm not sure if I understand the 2nd point correctly:

If an annotation type is declared with target ElementType.TYPE, it can be applied to any class or interface.

Likewise, if I have a marker interface then that too can be applied to any class or interface. Isn't it saying the same thing about marker annotations and marker interfaces? How can a marker interface be targeted more precisely?

The 2nd point also mentions that:

you can have [the Marker Interface] extend the sole interface to which it is applicable, guaranteeing that all marked types are also subtypes of the sole interface to which it is applicable.

Can't you also achieve this with annotations, by using the @Inherited meta-annotation?

解决方案

How can a marker interface be targeted more precisely?

You are correct that both could be applied to any type. By "targeted more precisely" the author means that you can add additional restrictions to which specific types a marker interface can be applied to. It is not possible to add the same precise restrictions to annotations: If an annotation is restricted to ElementType.TYPE, then it can always be applied to all types.

The other part of the 2nd point goes into details how you can add those restrictions. If you have a marker interface, you can let it extend another interface (which the author calls the sole interface) like this:

interface Marker extends Foo { }

The marker can now only be applied to types which implement Foo.

Can't you also achieve this with annotations, by using the @Inherited meta-annotation?

No, the @Inherited meta-annotation only means that any subtype of an annotated class will be treated as if it also had the same annotation. It does not impose any restrictions to which types the annotation can be applied to.

这篇关于标记注释与标记接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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