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.

java 默认为空的注解,错误注解的字段设置一个默认的空值相关推荐

  1. MySQL数据库建表时对于某个字段设置了默认值,但是使用Spring Data Jpa的save后默认值没有生效

    问题描述:MySQL数据库建表时对于某个字段设置了默认值,但是使用Spring Data Jpa的save后默认值没有生效.MySQL数据库中构建的用户表表结构如下所示: DROP TABLE IF ...

  2. 获取的字段值是空值或者为null,而你自己的需求就是想要获取的字段为一个 * 默认的值

    一.业务阐述 在开发中查询的数据库结果集,既要连接数据库.执行数据库操作.关闭数据库,还要把结果集的记录人为的设置到自己封装的DAO中等一系列的重复代码. 本文主要是想解决:用户只需要得到数据库连接, ...

  3. mysql的text的默认值,mysql给text设置一个默认值(干货)

    这周有个需求里面原先是用redis的,但是后面经同事介绍说明,决定还是用mysql,当时想在表里面加一个text字段,但是考虑到程序其他地方会不会有对该表的插入操作,所以不经思索的就说给text字段加 ...

  4. mysql字段设置中文默认值

    mysql字段设置中文默认值 (1064 - You have an error in your SQL syntax; check the manual that corresponds to yo ...

  5. java null转空_Java对象为空时,将null转换为 保存值为空的属性

    Java对象为空时,不显示该对象属性,或者将null转换为"" 第一种方法: @JsonInclude(JsonInclude.Include.NON_NULL) private ...

  6. 单击事件开启线程时,再次单击就会出现 java.lang.IllegalThreadStateException: Thread already started. 错误

    第一种解决方案:单击事件开启线程时,再次单击就会出现 java.lang.IllegalThreadStateException: Thread already started. 错误 解决办法: 创 ...

  7. java设置首页跳转_SpringBoot设置首页(默认页)跳转功能的实现方案

    先给大家介绍下SpringBoot设置首页(默认页)跳转功能 最近springboot开发需要设置个默认页面,就相当于我访问http://www.back.order.baidu.com要直接跳转到登 ...

  8. mysql 默认值 now_mysql设置datetime默认值为now

    mysql设置datetime默认值为now http://blog.csdn.net/scogeek/article/details/50378480 在mysql中timestamp数据类型是一个 ...

  9. httpsUrlConnection 如何设置的默认sslcontext和 hostnameverifier?

    https请求接口的时候java是什么时候默认添加ssl 上下文的? HttpURLConnection 子类HttpsURLConnection 子类HttpsURLConnectionImpl 1 ...

  10. PowerDesigner中如何给字符串字段设置默认值 .

    参考:http://www.cnblogs.com/navy235/archive/2011/10/18/2216443.html 在PowerDesigner12.5中,给varchar或nvarc ...

最新文章

  1. C#2.0实例程序STEP BY STEP--实例二:数据类型
  2. 1264: [AHOI2006]基因匹配Match
  3. 明尼苏达大学研究团队道歉,Linux内核维护人员:拒绝
  4. mysql 索引 normal_mysql索引类型 normal, unique, full text
  5. ajax制作表格,Ajax实现表格实时编辑(示例代码)
  6. Visual C++ 时尚编程百例016(字体)
  7. JNDI学习总结(4)——Tomcat下使用Druid配置JNDI数据源
  8. mysql 锁监视器_MySQL锁监视器
  9. 顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感
  10. 微信小程序商城项目实战(第三篇:商品列表)
  11. 共焦显微镜技术原理、参数及其应用
  12. img在div中居中
  13. electron开发客户端注意事项(兼开源个人知识管理工具“想学吗”)
  14. wps2016向程序发送命令_老司机帮您向程序发送命令时出现错误 【操作步骤】 的设置办法...
  15. MindManager教你做一份完美的笔记
  16. 抢红包: 红包过期失效案例
  17. jsp+ssm计算机毕业设计亿上汽车在线销售管理系统【附源码】
  18. 音乐电台“淘漉音乐”
  19. ACL-BioNLP 2020 | 耶鲁大学实践成果:生物医药知识图谱嵌入模型基准测试
  20. C# 关联启动外部程序,监听程序是否运行

热门文章

  1. 66岁比尔盖茨离婚!27年前与下属恋爱修成正果,现在“无法共同成长”,分割8000亿财产...
  2. CentOS7离线安装Nginx(详细安装过程)
  3. 【考试记录】20181003
  4. window 下tomcat 内存设置,bat启动方式的
  5. 调整数组顺序使奇数位于偶数前面(剑指offer)
  6. media type
  7. hdu 4781 Beautiful Soup 构造
  8. VC++如何计算一段代码的执行时间
  9. Pytorch——循环神经网络层、损失函数、优化器
  10. ArcGIS 制作3D规划图纸