注释类型参数的属性 [英] Annotation attributes with type parameters

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

问题描述

当你定义一个Java接口,它可以声明类型参数的方法,例如像这样的:

When you define a Java interface, it's possible to declare a method with type parameters, for example like this:

public interface ExampleInterface {
    <E extends Enum<E>> Class<E> options();
}

同样的事情不会注释中工作。此,例如,是非法的:

The same thing does not work in an annotation. This, for example, is illegal:

public @interface ExampleAnnotation {
    <E extends Enum<E>> Class<E> options();
}

我能得到什么,我后,使用原始类型枚举

public @interface ExampleAnnotation {
    @SuppressWarnings("rawtypes")
    Class<? extends Enum> options();
}

到底是什么,为什么它是不可能申报注释的原因与类型参数属性?

What exactly is the reason why it is not possible to declare annotation attributes with type parameters?

推荐答案

我认为这是可能的,但需要大量增加的语言规范,这是没有道理的。

I think it is possible, but it requires lots of additions to language spec, which is not justified.

首先,你枚举例如,你可以使用类和LT ;?扩展Enum&LT;&GT;&GT;选项​​

First, for you enum example, you could use Class<? extends Enum<?>> options.

有在另一个问题类和LT ;?扩展Enum&GT;选项​​:因为 Enum.class 类&LT;枚举&GT; 这是一个类和LT ;?扩展Enum&GT; ,是合法的选项= Enum.class

There is another problem in Class<? extends Enum> options: since Enum.class is a Class<Enum> which is a Class<? extends Enum>, it's legal to options=Enum.class

这不能用类和LT发生;?扩展Enum&LT;&GT;&GT;选项​​,因为枚举不是一个子类型枚举&LT;&GT; ,相当意外实际上在凌乱的原始类型的治疗方法。

That can't happen with Class<? extends Enum<?>> options, because Enum is not a subtype of Enum<?>, a rather accidental fact in the messy raw type treatments.

回到一般问题。由于有限的属性类型中,是唯一一个与类型参数,通配符通常是前pressive够了,你的关心是不是非常值得寻址。

Back to the general problem. Since among limited attribute types, Class is the only one with a type parameter, and wildcard usually is expressive enough, your concern isn't very much worth addressing.

让我们进一步推广问题,假设有更多的属性类型,而通配符功能不够强大在很多情况下。例如,假设地图是允许的,例如

Let's generalize the problem even further, suppose there are more attribute types, and wildcard isn't powerful enough in many cases. For example, let's say Map is allowed, e.g.

Map<String,Integer> options();

options={"a":1, "b":2} // suppose we have "map literal"

假设我们希望有一个attrbite类型为地图&LT; X,X&GT; 的任何类型的 X 。这不能用通配符pssed前$ P $ - 地图&LT;?,&GT; 表示,而地图&LT; X,Y&GT; 任何 X,Y

Suppose we want an attrbite type to be Map<x,x> for any type x. That can't be expressed with wildcards - Map<?,?> means rather Map<x,y> for any x,y.

一种方法是允许的类型参数的类型:&LT; X&GT;地图&LT; X,X&GT; 。这实际上是一般非常有用。但它是类型系统的重大变化。

One approach is to allow type parameters for a type: <X>Map<X,X>. This is actually quite useful in general. But it's a major change to type system.

另一种方法是reinter $ P $角型参数用于在注释类型的方法。

Another approach is to reinterpret type parameters for methods in an annotation type.

<X> Map<X,X> options();

options={ "a":"a", "b":"b" }  // infer X=String

这不的方法类型参数,推理规则,继承规则等等。我们需要改变/添加了很多东西,使其工作目前了解到在所有工作。

this doesn't work at all in the current understanding of method type parameters, inference rules, inheritance rules etc. We need to change/add a lot of things to make it work.

在这两种方法中,这是一个问题,如何提供 X 来注释处理器。我们必须创造一些额外的机制来进行与实例类型的参数。

In either approaches, it's a problem how to deliver X to annotation processors. We'll have to invent some additional mechanism to carry type arguments with instances.

这篇关于注释类型参数的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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