简体   繁体   English

替换为字段和方法的javax.annotation的FindBugs DefaultAnnotation

[英]alternative to FindBugs DefaultAnnotation for javax.annotation for fields and methods

I currently use 我目前正在使用

@DefaultAnnotation(NonNull.class)
package jobs;

import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;

however the annotation @edu.umd.cs.findbugs.annotations.DefaultAnnotation is deprecated: http://findbugs.sourceforge.net/api/edu/umd/cs/findbugs/annotations/DefaultAnnotation.html 但是不推荐使用注释@ edu.umd.cs.findbugs.annotations.DefaultAnnotation: http ://findbugs.sourceforge.net/api/edu/umd/cs/findbugs/annotations/DefaultAnnotation.html

They propose to use javax.annotation.ParametersAreNonnullByDefault However, DefaultAnnotation not only targets parameters, but also fields and methods. 他们建议使用javax.annotation.ParametersAreNonnullByDefault但是,DefaultAnnotation不仅定位参数,还定位字段和方法。

So, what is the javax.annotation alternative for setting fields and methods to Nonnull by default? 那么,默认情况下将字段和方法设置为Nonnull的javax.annotation替代方法是什么?

As far as I know there is none. 据我所知,没有。 Wanting the same thing, I copied the source for ParametersAreNonnullByDefault into my own FieldsAreNonnullByDefault and MethodsAreNonnullByDefault and changed the @TypeQualifierDefault values to match ( FIELD and METHOD respective). 希望同样的事情,我复制的ParametersAreNonnullByDefault到我自己FieldsAreNonnullByDefaultMethodsAreNonnullByDefault ,改变了@TypeQualifierDefault值匹配( FIELDMETHOD各自的)。 FindBugs picks up these new annotations perfectly. FindBugs可以完美地获取这些新注释。

Here's a sample for FieldsAreNonnullByDefault : 这是FieldsAreNonnullByDefault的示例:

package com.sample;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.annotation.meta.TypeQualifierDefault;

/**
 * This annotation can be applied to a package or class to indicate that the 
 * classes' fields in that element are nonnull by default unless there is
 * <ul>
 *   <li>an explicit nullness annotation
 *   <li>a default field annotation applied to a more tightly nested element.
 * </ul>
 */
@Documented
@Nonnull
@TypeQualifierDefault(ElementType.FIELD)  // <-- METHOD for return values
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldsAreNonnullByDefault {
}

So from the pure code point of view, there is not really any major difference. 所以从纯代码的角度来看,实际上没有任何重大差异。

The difference occurs once you start distributing your code afterwards. 一旦您之后开始分发代码,就会出现差异。

As long as you do not ship your code with the annotations jar and a JRE, you should be OK. 只要您不使用注释jar JRE发送代码,就应该没问题。

If you are distributing a JRE then as you already know you need to comply with the Oracle Java Binary License . 如果您正在分发JRE,那么您已经知道需要遵守Oracle Java二进制许可证

You may want to refresh yourself with the terms of that license, specifically: 您可能希望使用该许可证的条款刷新自己,特别是:

F. JAVA TECHNOLOGY RESTRICTIONS. F. JAVA技术限制。 You may not create, modify, or change the behavior of, or authorize your licensees to create, modify, or change the behavior of, classes, interfaces, or subpackages that are in any way identified as "java", "javax", "sun", “oracle” or similar convention as specified by Oracle in any naming convention designation. 您不得创建,修改或更改被许可方的行为,或授权其创建,修改或更改以任何方式标识为“java”,“javax”,“javax”的类,接口或子包的行为。 sun“,”oracle“或Oracle在任何命名约定中指定的类似约定。

So if you are distributing a JRE and the same distribution includes a jar file that defines classes in a javax subpackage, unless the classes comply with a specification released and published by a JSR, you are not complying with the terms of the Oracle Java Binary License. 因此,如果您正在分发JRE并且相同的发行版包含一个定义javax子包中的类的jar文件, 除非这些类符合JSR发布和发布的规范, 否则您不遵守Oracle Java二进制许可证的条款。

It is at this point that you then should take a look at the JSR 305 official page . 在这一点上,您应该看一下JSR 305官方页面

At this point in time that JSR has not published anything : 此时JSR尚未发布任何内容

JSR  - 截至2016年3月 - 被列为休眠状态

So you will need to ensure that you do not distribute the "jsr305.jar" file beside a JRE in your Windows installer, OS-X installer, Docker image, etc. 因此,您需要确保不在Windows安装程序,OS-X安装程序,Docker镜像等中的JRE旁边分发“jsr305.jar”文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM