From 8bde8ea5b389157c82c38818732e2d87dabce541 Mon Sep 17 00:00:00 2001 From: Elliot Sisteron Date: Fri, 5 May 2023 12:52:04 +0000 Subject: Fix some nullable warnings. The `@NonNullByDefault` annotation wasn't working as expected, using a fork from another team instead. Also added it to the notifications package where it was missing. Running null analysis on the code spawned a few warnings, I tried to fix most / all of them here. Some are benign (the checker has to understand that a certain variable cannot be null), but some were genuine issues. Bug: 267476663 Test: atest CtsSafetyCenterTestCases Change-Id: I992ed765d02c1a7b0a39997ea3718e1de09d4986 --- .../safetycenter/annotations/NonNullByDefault.java | 25 +++++++++++++++------- .../resources/SafetyCenterResourcesContext.java | 7 +++--- 2 files changed, 21 insertions(+), 11 deletions(-) (limited to 'SafetyCenter') diff --git a/SafetyCenter/Annotations/java/com/android/safetycenter/annotations/NonNullByDefault.java b/SafetyCenter/Annotations/java/com/android/safetycenter/annotations/NonNullByDefault.java index d19110652..34b6ce798 100644 --- a/SafetyCenter/Annotations/java/com/android/safetycenter/annotations/NonNullByDefault.java +++ b/SafetyCenter/Annotations/java/com/android/safetycenter/annotations/NonNullByDefault.java @@ -15,19 +15,28 @@ */ package com.android.safetycenter.annotations; -import androidx.annotation.NonNull; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PACKAGE; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.CLASS; -import java.lang.annotation.ElementType; import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import javax.annotation.Nonnull; import javax.annotation.meta.TypeQualifierDefault; /** - * Annotation to declare all type usages in the annotated instance as {@link NonNull}, unless - * explicitly marked with a nullable annotation. + * Specifies that all type uses are {@link Nonnull} within the annotated package, unless tagged with + * {@code @Nullable}. This helps IDEs flag all potential nullability issues without having to use + * {@code @NonNull} annotations. + * + *

This is similar to {@code @ParametersAreNonnullByDefault}, but is also applied more widely + * (e.g. to methods return types and fields). */ -@NonNull -@TypeQualifierDefault(ElementType.TYPE_USE) -@Retention(RetentionPolicy.CLASS) +@Retention(CLASS) +@Target(PACKAGE) +@TypeQualifierDefault({PARAMETER, FIELD, METHOD}) +@Nonnull // Android variant cannot be applied as a type qualifier. public @interface NonNullByDefault {} diff --git a/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java b/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java index bc56d18ee..5417e7b89 100644 --- a/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java +++ b/SafetyCenter/ResourcesLib/java/com/android/safetycenter/resources/SafetyCenterResourcesContext.java @@ -290,6 +290,7 @@ public class SafetyCenterResourcesContext extends ContextWrapper { /** Retrieve assets held in the Safety Center resources APK. */ @Override + @Nullable public AssetManager getAssets() { if (mAssetsFromApk == null) { Context resourcesApkContext = getResourcesApkContext(); @@ -302,6 +303,7 @@ public class SafetyCenterResourcesContext extends ContextWrapper { /** Retrieve resources held in the Safety Center resources APK. */ @Override + @Nullable public Resources getResources() { if (mResourcesFromApk == null) { Context resourcesApkContext = getResourcesApkContext(); @@ -314,6 +316,7 @@ public class SafetyCenterResourcesContext extends ContextWrapper { /** Retrieve theme held in the Safety Center resources APK. */ @Override + @Nullable public Resources.Theme getTheme() { if (mThemeFromApk == null) { Context resourcesApkContext = getResourcesApkContext(); @@ -366,9 +369,7 @@ public class SafetyCenterResourcesContext extends ContextWrapper { return null; } - /** - * Gets a color by resource name - */ + /** Gets a color by resource name */ @ColorInt @Nullable public Integer getColorByName(String name) { -- cgit v1.2.3