简体   繁体   English

Android Studio 是否有 NonNullByDefault 注释

[英]Is there a NonNullByDefault annotation for Android Studio

I am really missing @NonNullByDefault from eclipse in Android Studio.我真的很想念 Android Studio 中的 Eclipse 中的 @NonNullByDefault。 Is there a way to get this or something similar?有没有办法得到这个或类似的东西? I only saw one using javax's @TypeQualifierDefault , but this seems not to be available in the android project.我只看到一个使用 javax 的@TypeQualifierDefault ,但这在 android 项目中似乎不可用。

I have not found a @NonNullByDefault functionality.我还没有找到 @NonNullByDefault 功能。 BUT you can at least let the inspector handle all unannotated members & parameters as if they were annotated as @Nullable.但是您至少可以让检查员处理所有未注释的成员和参数,就好像它们被注释为 @Nullable 一样。 Which will at least help to get 100% null-analyzation coverage.这至少有助于获得 100% 的空分析覆盖率。 Basically a @NullByDefault.基本上是@NullByDefault。

检查器设置

I wrote a gradle script to handle project-wide @NonNullByDefault annotation for Android Studio.我编写了一个 gradle 脚本来处理 Android Studio 的项目范围的@NonNullByDefault注释。

It goes through all the subpackages to check whether package-info.java files are generated inside each of them.它遍历所有子包以检查每个子包中是否生成了package-info.java文件。

The script is run before each assembleDebug task so that you can force @ParametersAreNonnullByDefault annotation on all java classes in your project.该脚本在每个 assembleDebug 任务之前运行,以便您可以在项目中的所有 java 类上强制使用 @ParametersAreNonnullByDefault 注释

NonNullByDefault for all java classes inside a project 项目中所有 Java 类的 NonNullByDefault

You can actually just add the eclipse annotation package to your project and use it.您实际上可以将 eclipse 注释包添加到您的项目中并使用它。

Simply add只需添加

dependencies {
    implementation group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.annotation', version: '2.2.600'
}

To your gradle and you can use the @NonNullByDefault annotations it provides.对于您的 gradle,您可以使用它提供的 @NonNullByDefault 注释。

As usual you can either set it per class像往常一样,您可以为每个班级设置它

@NonNullByDefault
public class MyClass {
    String test = "This is a non null string"
    @Nullable test2 = "This is a nullable string"

    public MyClass(String nonNullParam, @Nullable String nullableParam) {
        Object nonNullVar = nonNullParam;
        @Nullable Object nullableVar = nullableParam;
    }
}

Or you can set it for an entire package by creating a package-info.java file and in it或者您可以通过创建一个 package-info.java 文件并在其中为整个包设置它

@NonNullByDefault   
package my.package;

import org.eclipse.jdt.annotation.NonNullByDefault;

You still need to activate the setting in https://stackoverflow.com/a/35942944/2075537 to have complete coverage in case you forget it on a package您仍然需要激活https://stackoverflow.com/a/35942944/2075537 中的设置才能获得完整的覆盖范围,以防您在包裹上忘记它

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

相关问题 IDEA中是否有@NonNullByDefault注释? - Is there a @NonNullByDefault annotation in IDEA? 我是否可以将@NonNullByDefault与没有空注释约束的继承类混合使用? - Can I not mix @NonNullByDefault with inherited classes that have no null annotation constraint? Android Studio Annotation AbstractProcessor未找到 - Android Studio Annotation AbstractProcessor Not Found 使用@NonNullByDefault和数组时,如何删除“空注是多余的”警告? (Eclipse,NullAnnotation) - How to remove 'nullness annotation is redundant' warning when using @NonNullByDefault and arrays? (Eclipse, NullAnnotation) 用于Android Studio 3.0中自定义注释处理器的NoClassDefFoundError - NoClassDefFoundError for custom annotation processor in Android Studio 3.0 在 Android Studio 2.2 中启用 Annotation Processors 选项 - enable Annotation Processors option in Android Studio 2.2 Android Studio中的多模块注释处理 - Multi-module annotation processing in Android Studio Android Studio 3.1无法识别jar批注处理 - Android studio 3.1 not recognizing jar annotation processing 在Android Studio中使用@NonNull注释的正确方法 - Right way to use the @NonNull annotation in Android Studio 如何在包上使用NonNullByDefault - How to use NonNullByDefault on a package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM