In Java, I use the 'null analysis' using annotations. I don't use Eclipse's default annotations and I have defined my owns.
在Java中,我使用注释使用'null analysis'。我没有使用Eclipse的默认注释,我已经定义了自己的所有权。
For each package, I used to add a global NonNullByDefault
annotation in the package-info.java
file.
对于每个包,我曾经在package-info.java文件中添加了一个全局的NonNullByDefault批注。
With Java version <= Java7, the code below has no problem, everything works fine.
使用Java版本<= Java7,下面的代码没有问题,一切正常。
@my.package.annotations.NonNullByDefault
package my.foo.package;
But when I set Eclipse to be Java8 compliant (the only JDK installed on my system is Java8), then an error occurs:
但是当我将Eclipse设置为Java8兼容时(我系统上安装的唯一JDK是Java8),则会发生错误:
A default nullness annotation has not been specified for the package my.foo.package - Java Problem
尚未为包my.foo.package指定默认的nullness注释 - Java问题
My annotation is declared in the following way:
我的注释以下列方式声明:
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.PACKAGE, ElementType.TYPE,
ElementType.METHOD, ElementType.CONSTRUCTOR })
public @interface NonNullByDefault {
// marker annotation
}
What's new in Java8 about that? Can someone explain me the good way to use my annotations?
Java8中有什么新内容?有人能解释我使用我的注释的好方法吗?
2 个解决方案
#1
JDT's own annotations have significantly changed between versions 1.x and 2.x of org.eclipse.jdt.annotation. To fully leverage the greater precision of type annotations in Java 8, @NonNullByDefault
now has an attribute of type DefaultLocation[]
, specifying where exactly the default should apply.
JDT自己的注释在org.eclipse.jdt.annotation的版本1.x和2.x之间发生了显着变化。为了充分利用Java 8中类型注释的更高精度,@ NonNullByDefault现在具有DefaultLocation []类型的属性,指定默认应该应用的确切位置。
Rebuilding your own annotations with the same level of details and making JDT understand your own annotations will be cumbersome.
使用相同级别的细节重建您自己的注释并使JDT理解您自己的注释将是麻烦的。
Still, an attribute-less @NonNullByDefault
should be understood in the "default" way (see the default value of org.eclipse.jdt.annotation.NonNullByDefault.value
). Apparently there's a bug in the evaluation of such custom annotations in Java 8 projects.
但是,应该以“默认”方式理解无属性@NonNullByDefault(请参阅org.eclipse.jdt.annotation.NonNullByDefault.value的默认值)。显然,在Java 8项目中评估此类自定义注释存在一个错误。
#2
Can you please try the following:
你可以尝试以下方法:
- Put a file name
package-info.java
into each of your Java packages -
Enter the following in the file:
在文件中输入以下内容:
@NonNullByDefault package your.package; import org.eclipse.jdt.annotation.NonNullByDefault;
@NonNullByDefault包your.package; import org.eclipse.jdt.annotation.NonNullByDefault;
将文件名package-info.java放入每个Java包中
This will specify that you don't want to accept null anywhere for any argument and you will also not be putting loads of @NonNull
in the code. You may get a warning like A default nullness annotation has not been specified for the package my.foo.package
if you create a new package but forget to copy the file.
这将指定您不希望任何参数的任何地方都接受null,并且您也不会在代码中加载@NonNull。您可能会收到一条警告,如果您创建了一个新包但忘记复制该文件,则尚未为包my.foo.package指定默认的nullness注释。
#1
JDT's own annotations have significantly changed between versions 1.x and 2.x of org.eclipse.jdt.annotation. To fully leverage the greater precision of type annotations in Java 8, @NonNullByDefault
now has an attribute of type DefaultLocation[]
, specifying where exactly the default should apply.
JDT自己的注释在org.eclipse.jdt.annotation的版本1.x和2.x之间发生了显着变化。为了充分利用Java 8中类型注释的更高精度,@ NonNullByDefault现在具有DefaultLocation []类型的属性,指定默认应该应用的确切位置。
Rebuilding your own annotations with the same level of details and making JDT understand your own annotations will be cumbersome.
使用相同级别的细节重建您自己的注释并使JDT理解您自己的注释将是麻烦的。
Still, an attribute-less @NonNullByDefault
should be understood in the "default" way (see the default value of org.eclipse.jdt.annotation.NonNullByDefault.value
). Apparently there's a bug in the evaluation of such custom annotations in Java 8 projects.
但是,应该以“默认”方式理解无属性@NonNullByDefault(请参阅org.eclipse.jdt.annotation.NonNullByDefault.value的默认值)。显然,在Java 8项目中评估此类自定义注释存在一个错误。
#2
Can you please try the following:
你可以尝试以下方法:
- Put a file name
package-info.java
into each of your Java packages -
Enter the following in the file:
在文件中输入以下内容:
@NonNullByDefault package your.package; import org.eclipse.jdt.annotation.NonNullByDefault;
@NonNullByDefault包your.package; import org.eclipse.jdt.annotation.NonNullByDefault;
将文件名package-info.java放入每个Java包中
This will specify that you don't want to accept null anywhere for any argument and you will also not be putting loads of @NonNull
in the code. You may get a warning like A default nullness annotation has not been specified for the package my.foo.package
if you create a new package but forget to copy the file.
这将指定您不希望任何参数的任何地方都接受null,并且您也不会在代码中加载@NonNull。您可能会收到一条警告,如果您创建了一个新包但忘记复制该文件,则尚未为包my.foo.package指定默认的nullness注释。