错误注解的字段设置一个默认的空值 [英] Error setting a default null value for an annotation's field

查看:2263
本文介绍了错误注解的字段设置一个默认的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我得到一个错误属性值必须是常量。不是的无效恒???

  @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
公共@interface SomeInterface {
    类&LT ;?扩展美孚>巴()默认为空; //这不会编译
}


解决方案

我不知道为什么,但是的JLS 是很清楚的:

 讨论 请注意,null不是任何元素类型的合法元素值。

和一个缺省元素的定义是:

 默认值:
         默认ElementValue

不幸的是我一直在寻找新的语言功能(枚举现在注解)必须非常无益的编译器错误消息,当您不符合语言规范。

编辑:小googleing找到的<一个href=\"http://groups.csail.mit.edu/pag/jsr308/specification/java-annotation-design.html#htoc48\">following在JSR-308,在那里他们认为允许空值在这种情况下:


  

我们注意到一些可能反对该提案。


  
  

该提案没有做任何事情,可能这是不可能的了。


  
  

编程定义的特殊价值提供了比空更好的文档,这可能意味着无,未初始化,空本身,等等。


  
  

该提案更容易出错。这是很容易忘记检查对空不是忘记检查明确的价值。


  
  

该提案可能使标准的成语更详细。目前,只有注释的用户需要检查是否有其特殊的价值。该提案,许多处理注释工具则要检查字段的值是否为空,以免他们抛出一个空指针异常。


我觉得只有最后两点是有关为什么不这样做摆在首位。最后一点无疑带来了一个好点 - 注释处理器从来没有被关注,他们将得到一个注释值空。我倾向于认为,随着越来越多的注解处理器和其他类似的框架code的工作是必须做这种检查,使开发商code更清晰,而不是周围的其他方式,但它肯定将使它很难证明改变它。

Why am I getting an error "Attribute value must be constant". Isn't null constant???

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SomeInterface {
    Class<? extends Foo> bar() default null;// this doesn't compile
}

解决方案

I don't know why, but the JLS is very clear:

 Discussion

 Note that null is not a legal element value for any element type.

And the definition of a default element is:

     DefaultValue:
         default ElementValue

Unfortunately I keep finding that the new language features (Enums and now Annotations) have very unhelpful compiler error messages when you don't meet the language spec.

EDIT: A little googleing found the following in the JSR-308, where they argue for allowing nulls in this situation:

We note some possible objections to the proposal.

The proposal doesn't make anything possible that was not possible before.

The programmer-defined special value provides better documentation than null, which might mean "none", "uninitialized", null itself, etc.

The proposal is more error-prone. It's much easier to forget checking against null than to forget checking for an explicit value.

The proposal may make the standard idiom more verbose. Currently only the users of an annotation need to check for its special values. With the proposal, many tools that process annotations will have to check whether a field's value is null lest they throw a null pointer exception.

I think only the last two points are relevant to "why not do it in the first place." The last point certainly brings up a good point - an annotation processor never has to be concerned that they will get a null on an annotation value. I tend to see that as more the job of annotation processors and other such framework code to have to do that kind of check to make the developers code clearer rather than the other way around, but it would certainly make it hard to justify changing it.

这篇关于错误注解的字段设置一个默认的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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