Skip to content

Commit

Permalink
Move Java-7-VM warning from MoreObjects to Preconditions.
Browse files Browse the repository at this point in the history
RELNOTES=Increased the aggressiveness of [Guava 30.1](https://github.com/google/guava/releases/tag/v30.1)'s warning log message for running `guava-android` under a Java 7 VM. (Android VMs are unaffected.) If the warning _itself_ causes you trouble, you can eliminate it by silencing the logger for `com.google.common.base.Preconditions` (which is used _only_ for this warning). This warning prepares for [removing support for Java 7 in 2021](#5269). Please report any problems. We have tried to make the warning as safe as possible, but anytime a common library logs, especially as aggressively as we do in this new release, there is the potential for [`NullPointerException`](https://stackoverflow.com/a/41017717/28465) or even [deadlock](https://stackoverflow.com/a/48009613/28465). (To be clear, Guava will not log under Java 8 or Android, but it will under Java 7.)
PiperOrigin-RevId: 361604103
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Mar 9, 2021
1 parent 7f30024 commit d6fc364
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 72 deletions.
36 changes: 0 additions & 36 deletions android/guava/src/com/google/common/base/MoreObjects.java
Expand Up @@ -15,13 +15,10 @@
package com.google.common.base;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.logging.Level.WARNING;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import java.util.Arrays;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

/**
Expand Down Expand Up @@ -144,46 +141,13 @@ public static ToStringHelper toStringHelper(String className) {
* @since 18.0 (since 2.0 as {@code Objects.ToStringHelper}).
*/
public static final class ToStringHelper {
@GuardedBy("ToStringHelper.class")
private static boolean performedJava8CompatibilityCheck;

private static void java8CompatibilityCheck() {
@SuppressWarnings("GuardedBy")
boolean racyReadForDoubleCheckedLock = performedJava8CompatibilityCheck;
if (racyReadForDoubleCheckedLock) {
return;
}
synchronized (ToStringHelper.class) {
if (performedJava8CompatibilityCheck) {
return;
}
performedJava8CompatibilityCheck = true;
}

try {
Java8Usage.performCheck();
} catch (Throwable underlying) {
Exception toLog =
new Exception(
"Guava will drop support for Java 7 in 2021. Please let us know if this will cause"
+ " you problems: https://github.com/google/guava/issues/5269",
underlying);
Logger.getLogger(ToStringHelper.class.getName())
.log(
WARNING,
"Java 7 compatibility warning: See https://github.com/google/guava/issues/5269",
toLog);
}
}

private final String className;
private final ValueHolder holderHead = new ValueHolder();
private ValueHolder holderTail = holderHead;
private boolean omitNullValues = false;

/** Use {@link MoreObjects#toStringHelper(Object)} to create an instance. */
private ToStringHelper(String className) {
java8CompatibilityCheck();
this.className = checkNotNull(className);
}

Expand Down
19 changes: 19 additions & 0 deletions android/guava/src/com/google/common/base/Preconditions.java
Expand Up @@ -15,9 +15,11 @@
package com.google.common.base;

import static com.google.common.base.Strings.lenientFormat;
import static java.util.logging.Level.WARNING;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.compatqual.NonNullDecl;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

Expand Down Expand Up @@ -1488,4 +1490,21 @@ private static String badPositionIndexes(int start, int end, int size) {
// end < start
return lenientFormat("end index (%s) must not be less than start index (%s)", end, start);
}

static {
try {
Java8Usage.performCheck();
} catch (Throwable underlying) {
Exception toLog =
new Exception(
"Guava will drop support for Java 7 in 2021. Please let us know if this will cause"
+ " you problems: https://github.com/google/guava/issues/5269",
underlying);
Logger.getLogger(Preconditions.class.getName())
.log(
WARNING,
"Java 7 compatibility warning: See https://github.com/google/guava/issues/5269",
toLog);
}
}
}
36 changes: 0 additions & 36 deletions guava/src/com/google/common/base/MoreObjects.java
Expand Up @@ -15,13 +15,10 @@
package com.google.common.base;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.logging.Level.WARNING;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import java.util.Arrays;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
Expand Down Expand Up @@ -144,46 +141,13 @@ public static ToStringHelper toStringHelper(String className) {
* @since 18.0 (since 2.0 as {@code Objects.ToStringHelper}).
*/
public static final class ToStringHelper {
@GuardedBy("ToStringHelper.class")
private static boolean performedJava8CompatibilityCheck;

private static void java8CompatibilityCheck() {
@SuppressWarnings("GuardedBy")
boolean racyReadForDoubleCheckedLock = performedJava8CompatibilityCheck;
if (racyReadForDoubleCheckedLock) {
return;
}
synchronized (ToStringHelper.class) {
if (performedJava8CompatibilityCheck) {
return;
}
performedJava8CompatibilityCheck = true;
}

try {
Java8Usage.performCheck();
} catch (Throwable underlying) {
Exception toLog =
new Exception(
"Guava will drop support for Java 7 in 2021. Please let us know if this will cause"
+ " you problems: https://github.com/google/guava/issues/5269",
underlying);
Logger.getLogger(ToStringHelper.class.getName())
.log(
WARNING,
"Java 7 compatibility warning: See https://github.com/google/guava/issues/5269",
toLog);
}
}

private final String className;
private final ValueHolder holderHead = new ValueHolder();
private ValueHolder holderTail = holderHead;
private boolean omitNullValues = false;

/** Use {@link MoreObjects#toStringHelper(Object)} to create an instance. */
private ToStringHelper(String className) {
java8CompatibilityCheck();
this.className = checkNotNull(className);
}

Expand Down
19 changes: 19 additions & 0 deletions guava/src/com/google/common/base/Preconditions.java
Expand Up @@ -15,9 +15,11 @@
package com.google.common.base;

import static com.google.common.base.Strings.lenientFormat;
import static java.util.logging.Level.WARNING;

import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -1431,4 +1433,21 @@ private static String badPositionIndexes(int start, int end, int size) {
// end < start
return lenientFormat("end index (%s) must not be less than start index (%s)", end, start);
}

static {
try {
Java8Usage.performCheck();
} catch (Throwable underlying) {
Exception toLog =
new Exception(
"Guava will drop support for Java 7 in 2021. Please let us know if this will cause"
+ " you problems: https://github.com/google/guava/issues/5269",
underlying);
Logger.getLogger(Preconditions.class.getName())
.log(
WARNING,
"Java 7 compatibility warning: See https://github.com/google/guava/issues/5269",
toLog);
}
}
}

0 comments on commit d6fc364

Please sign in to comment.