Skip to content

Commit

Permalink
Log a warning if running under a Java 7 VM.
Browse files Browse the repository at this point in the history
More precisely, log a warning if lambda expressions or type annotations in our classes would produce an exception. If someone wants to use Retrolambda or a similar tool to rewrite our classes, that's fine with us if it works. And our support for Android is unchanged: The Android toolchain rewrites lambdas and removes type annotations.

This is a step toward removing Java 7 support entirely: #5269

RELNOTES=Introduced a warning log message when running under Java 7. This warning is not _guaranteed_ to be logged when running under Java 7, so please don't rely on it as your only warning about future problems. If the warning _itself_ causes you trouble, you can eliminate it by silencing the logger for `com.google.common.base.MoreObjects$ToStringHelper` (which is used _only_ for this warning). This warning prepares for [removing support for Java 7 in 2021](#5269). Please report any problems.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=346795766
  • Loading branch information
cpovirk authored and kluever committed Dec 11, 2020
1 parent 8d571ca commit 0357006
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 0 deletions.
36 changes: 36 additions & 0 deletions android/guava/pom.xml
Expand Up @@ -90,6 +90,42 @@
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<!--
The execution named default-compile happens first, regardless
of the order of the executions in the source file. So, because
Java8Usage is a dependency of the main sources, we need to call
its compilation "default-compile," even though it's the special
case.
-->
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<includes>
<include>**/Java8Usage.java</include>
</includes>
<!-- -source 8 -target 8 is a no-op in the mainline but matters in the backport. -->
<source>8</source>
<target>8</target>
</configuration>
</execution>
<execution>
<id>main-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<excludes>
<exclude>**/Java8Usage.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
Expand Down
45 changes: 45 additions & 0 deletions android/guava/src/com/google/common/base/Java8Usage.java
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2020 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.base;

import com.google.errorprone.annotations.CanIgnoreReturnValue;

/**
* A class that uses a couple Java 8 features but doesn't really do anything. This lets us attempt
* to load it and log a warning if that fails, giving users advance notice of our dropping Java 8
* support.
*/
/*
* This class should be annotated @GwtCompatible. But if we annotate it @GwtCompatible, then we need
* to build GwtCompatible.java (-source 7 -target 7 in the Android flavor) before we build
* Java8Usage.java (-source 8 target 8, which we already need to build before the rest of
* common.base). We could configure Maven to do that, but it's easier to just skip the annotation.
*/
final class Java8Usage {
@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE)
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
private @interface SomeTypeAnnotation {}

@CanIgnoreReturnValue
static @SomeTypeAnnotation String performCheck() {
Runnable r = () -> {};
r.run();
return "";
}

private Java8Usage() {}
}
36 changes: 36 additions & 0 deletions android/guava/src/com/google/common/base/MoreObjects.java
Expand Up @@ -15,10 +15,13 @@
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 @@ -141,13 +144,46 @@ 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
10 changes: 10 additions & 0 deletions android/pom.xml
Expand Up @@ -118,6 +118,16 @@
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArgs>
<!--
Make includes/excludes fully work:
https://issues.apache.org/jira/browse/MCOMPILER-174
(Compare what guava-gwt has to do for maven-javadoc-plugin.)
-->
<arg>-sourcepath</arg>
<arg>doesnotexist</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
Expand Down
36 changes: 36 additions & 0 deletions guava/pom.xml
Expand Up @@ -90,6 +90,42 @@
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<!--
The execution named default-compile happens first, regardless
of the order of the executions in the source file. So, because
Java8Usage is a dependency of the main sources, we need to call
its compilation "default-compile," even though it's the special
case.
-->
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<includes>
<include>**/Java8Usage.java</include>
</includes>
<!-- -source 8 -target 8 is a no-op in the mainline but matters in the backport. -->
<source>8</source>
<target>8</target>
</configuration>
</execution>
<execution>
<id>main-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<excludes>
<exclude>**/Java8Usage.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
Expand Down
45 changes: 45 additions & 0 deletions guava/src/com/google/common/base/Java8Usage.java
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2020 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.base;

import com.google.errorprone.annotations.CanIgnoreReturnValue;

/**
* A class that uses a couple Java 8 features but doesn't really do anything. This lets us attempt
* to load it and log a warning if that fails, giving users advance notice of our dropping Java 8
* support.
*/
/*
* This class should be annotated @GwtCompatible. But if we annotate it @GwtCompatible, then we need
* to build GwtCompatible.java (-source 7 -target 7 in the Android flavor) before we build
* Java8Usage.java (-source 8 target 8, which we already need to build before the rest of
* common.base). We could configure Maven to do that, but it's easier to just skip the annotation.
*/
final class Java8Usage {
@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE)
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
private @interface SomeTypeAnnotation {}

@CanIgnoreReturnValue
static @SomeTypeAnnotation String performCheck() {
Runnable r = () -> {};
r.run();
return "";
}

private Java8Usage() {}
}
36 changes: 36 additions & 0 deletions guava/src/com/google/common/base/MoreObjects.java
Expand Up @@ -15,10 +15,13 @@
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 @@ -141,13 +144,46 @@ 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
10 changes: 10 additions & 0 deletions pom.xml
Expand Up @@ -119,6 +119,16 @@
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgs>
<!--
Make includes/excludes fully work:
https://issues.apache.org/jira/browse/MCOMPILER-174
(Compare what guava-gwt has to do for maven-javadoc-plugin.)
-->
<arg>-sourcepath</arg>
<arg>doesnotexist</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
Expand Down

0 comments on commit 0357006

Please sign in to comment.