Skip to content

Commit

Permalink
Convert Finalizer nullness annotations to checker-qual and jsr305.
Browse files Browse the repository at this point in the history
This eliminates one of the remaining usages of checker-compat-qual.

RELNOTES=n/a
PiperOrigin-RevId: 399322816
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Sep 28, 2021
1 parent fd945ba commit 5d57f75
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
13 changes: 7 additions & 6 deletions android/guava/src/com/google/common/base/internal/Finalizer.java
Expand Up @@ -23,7 +23,7 @@
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
import javax.annotation.CheckForNull;

/**
* Thread that finalizes referents. All references should implement {@code
Expand All @@ -43,6 +43,7 @@
* collected, and this class can detect when the main class loader has been garbage collected and
* stop itself.
*/
// no @ElementTypesAreNonNullByDefault for the reasons discussed above
public class Finalizer implements Runnable {

private static final Logger logger = Logger.getLogger(Finalizer.class.getName());
Expand Down Expand Up @@ -116,10 +117,10 @@ public static void startFinalizer(
// By preference, we will use the Thread constructor that has an `inheritThreadLocals` parameter.
// But before Java 9, our only way not to inherit ThreadLocals is to zap them after the thread
// is created, by accessing a private field.
@NullableDecl
@CheckForNull
private static final Constructor<Thread> bigThreadConstructor = getBigThreadConstructor();

@NullableDecl
@CheckForNull
private static final Field inheritableThreadLocals =
(bigThreadConstructor == null) ? getInheritableThreadLocalsField() : null;

Expand Down Expand Up @@ -192,7 +193,7 @@ private boolean cleanUp(Reference<?> reference) {
}

/** Looks up FinalizableReference.finalizeReferent() method. */
@NullableDecl
@CheckForNull
private Method getFinalizeReferentMethod() {
Class<?> finalizableReferenceClass = finalizableReferenceClassReference.get();
if (finalizableReferenceClass == null) {
Expand All @@ -211,7 +212,7 @@ private Method getFinalizeReferentMethod() {
}
}

@NullableDecl
@CheckForNull
private static Field getInheritableThreadLocalsField() {
try {
Field inheritableThreadLocals = Thread.class.getDeclaredField("inheritableThreadLocals");
Expand All @@ -226,7 +227,7 @@ private static Field getInheritableThreadLocalsField() {
}
}

@NullableDecl
@CheckForNull
private static Constructor<Thread> getBigThreadConstructor() {
try {
return Thread.class.getConstructor(
Expand Down
19 changes: 12 additions & 7 deletions guava/src/com/google/common/base/internal/Finalizer.java
Expand Up @@ -23,7 +23,7 @@
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;
import javax.annotation.CheckForNull;

/**
* Thread that finalizes referents. All references should implement {@code
Expand All @@ -43,6 +43,7 @@
* collected, and this class can detect when the main class loader has been garbage collected and
* stop itself.
*/
// no @ElementTypesAreNonNullByDefault for the reasons discussed above
public class Finalizer implements Runnable {

private static final Logger logger = Logger.getLogger(Finalizer.class.getName());
Expand Down Expand Up @@ -116,10 +117,11 @@ public static void startFinalizer(
// By preference, we will use the Thread constructor that has an `inheritThreadLocals` parameter.
// But before Java 9, our only way not to inherit ThreadLocals is to zap them after the thread
// is created, by accessing a private field.
private static final @Nullable Constructor<Thread> bigThreadConstructor =
getBigThreadConstructor();
@CheckForNull
private static final Constructor<Thread> bigThreadConstructor = getBigThreadConstructor();

private static final @Nullable Field inheritableThreadLocals =
@CheckForNull
private static final Field inheritableThreadLocals =
(bigThreadConstructor == null) ? getInheritableThreadLocalsField() : null;

/** Constructs a new finalizer thread. */
Expand Down Expand Up @@ -191,7 +193,8 @@ private boolean cleanUp(Reference<?> reference) {
}

/** Looks up FinalizableReference.finalizeReferent() method. */
private @Nullable Method getFinalizeReferentMethod() {
@CheckForNull
private Method getFinalizeReferentMethod() {
Class<?> finalizableReferenceClass = finalizableReferenceClassReference.get();
if (finalizableReferenceClass == null) {
/*
Expand All @@ -209,7 +212,8 @@ private boolean cleanUp(Reference<?> reference) {
}
}

private static @Nullable Field getInheritableThreadLocalsField() {
@CheckForNull
private static Field getInheritableThreadLocalsField() {
try {
Field inheritableThreadLocals = Thread.class.getDeclaredField("inheritableThreadLocals");
inheritableThreadLocals.setAccessible(true);
Expand All @@ -223,7 +227,8 @@ private boolean cleanUp(Reference<?> reference) {
}
}

private static @Nullable Constructor<Thread> getBigThreadConstructor() {
@CheckForNull
private static Constructor<Thread> getBigThreadConstructor() {
try {
return Thread.class.getConstructor(
ThreadGroup.class, Runnable.class, String.class, long.class, boolean.class);
Expand Down

0 comments on commit 5d57f75

Please sign in to comment.