Skip to content

Commit

Permalink
Address a few nullness mismatches that our checker newly detects.
Browse files Browse the repository at this point in the history
We had concluded way back in cl/63478563 that the `Platform` one is harmless in practice, and the `RegularImmutableMap` one is definitely harmless. Still, they're good to address.

RELNOTES=n/a
PiperOrigin-RevId: 625103048
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Apr 15, 2024
1 parent b33044e commit eab5117
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
12 changes: 11 additions & 1 deletion android/guava/src/com/google/common/base/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ static CharMatcher precomputeCharMatcher(CharMatcher matcher) {

static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value);
return ref == null ? Optional.<T>absent() : Optional.of(enumClass.cast(ref.get()));
/*
* We use `fromNullable` instead of `of` because `WeakReference.get()` has a nullable return
* type.
*
* In practice, we are very unlikely to see `null`: The `WeakReference` to the enum constant
* won't be cleared as long as the enum constant is referenced somewhere, and the enum constant
* is referenced somewhere for as long as the enum class is loaded. *Maybe in theory* the enum
* class could be unloaded after the above call to `getEnumConstants` but before we call
* `get()`, but that is vanishingly unlikely.
*/
return ref == null ? Optional.absent() : Optional.fromNullable(enumClass.cast(ref.get()));
}

static String formatCompact4Digits(double value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public UnmodifiableIterator<Entry<K, V>> iterator() {
}

@Override
int copyIntoArray(Object[] dst, int offset) {
int copyIntoArray(@Nullable Object[] dst, int offset) {
return asList().copyIntoArray(dst, offset);
}

Expand Down Expand Up @@ -537,7 +537,7 @@ public UnmodifiableIterator<K> iterator() {
}

@Override
int copyIntoArray(Object[] dst, int offset) {
int copyIntoArray(@Nullable Object[] dst, int offset) {
return asList().copyIntoArray(dst, offset);
}

Expand Down
12 changes: 11 additions & 1 deletion guava/src/com/google/common/base/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ static CharMatcher precomputeCharMatcher(CharMatcher matcher) {

static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value);
return ref == null ? Optional.<T>absent() : Optional.of(enumClass.cast(ref.get()));
/*
* We use `fromNullable` instead of `of` because `WeakReference.get()` has a nullable return
* type.
*
* In practice, we are very unlikely to see `null`: The `WeakReference` to the enum constant
* won't be cleared as long as the enum constant is referenced somewhere, and the enum constant
* is referenced somewhere for as long as the enum class is loaded. *Maybe in theory* the enum
* class could be unloaded after the above call to `getEnumConstants` but before we call
* `get()`, but that is vanishingly unlikely.
*/
return ref == null ? Optional.absent() : Optional.fromNullable(enumClass.cast(ref.get()));
}

static String formatCompact4Digits(double value) {
Expand Down

0 comments on commit eab5117

Please sign in to comment.