Java 8、类型注解和 JSR 308 [英] Java 8, Type Annotations and JSR 308

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

问题描述

我已经安装了最新的 JDK 8 (b116),但我注意到我不能使用类型注释.例如阅读 Java 教程,如果我写:

I've installed the last JDK 8 (b116) but I noticed that I can't use type annotations. For example reading the Java tutorial if I write:

String str = null;
String myString = (@NonNull String) str;

TEST st = new @Interned TEST();

编译器给我以下错误:

annotation type not applicable to this kind of declaration

现在可以使用了.在使用类型注解之前,我们必须对注解进行注解使用 @Target(ElementType.TYPE_USE).看看下面的评论!

Now it works. Before using a type annotations we must annotate the annotation with @Target(ElementType.TYPE_USE). Look at the comments below!

我也不明白像:NonNullInterned 等注释是否会插入到 JDK 中,或者如果我们必须下载 Checker Framework

I also don't understand if the annotations like: NonNull, Interned, etc. will be inserted in the JDK or if we have to download the Checker Framework

推荐答案

您已经自己回答了问题的第一部分.

You have answered the first part of your Question yourself.

第二部分:

我也不明白像:NonNullInterned 等注解是否会被插入到 JDK 中,或者我们是否必须下载 Checker Framework.

I also don't understand if the annotations like: NonNull, Interned, etc. will be inserted in the JDK or if we have to download the Checker Framework.

注解只是一种Java类/接口.它们必须在源代码中定义并编译.

Annotations are just a kind of Java class / interface. They have to be defined in source code and compiled.

理想情况下,您应该从规范位置获得确定性源代码和/或字节码文件.但是,如果您要复制注释源代码的主要部分(包名称、注释名称、字段名称和类型)并对其进行编译,则 JVM 的其余部分将一无所获.

Ideally you should the definitive source code and / or bytecode files, obtained from the canonical place. However, if you were to reproduce the salient parts of the annotations' source code (the package name, annotation name, field names and type) and compile it, the rest of the JVM would be none the wiser.

但是当您谈论诸如 @NonNull@Interned 之类的特定注释时,您需要意识到在不同的包中可能存在这些注释的多个版本.这可能会导致问题(对于注释处理软件),直到标准/事实上的标准版本出现.我不知道是否可以将 Checkers 框架称为事实上的标准……但.

But when you talk about specific annotations like @NonNull and @Interned, you need to realize that there could exist multiple versions of these in different packages. This could cause issues (for annotation processing software) until standard / defacto standard versions emerge. I don't know if the Checkers Framework could be called a defacto standard ... yet.

您询问是否将检查器注释添加到 Java 8 库中.我个人对此表示怀疑,因为这些注释的包名是不可接受的.不过等等看...

You asked if the checkers annotations would be added to the Java 8 library. I personally doubt it, because the package name for those annotations would be unacceptable. But wait and see ...

这篇关于Java 8、类型注解和 JSR 308的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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