Skip to content

Commit dc52e6e

Browse files
cpovirkkluever
authored andcommitted
Log a warning if running under a Java 7 VM.
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
1 parent ca3cc12 commit dc52e6e

File tree

8 files changed

+254
-0
lines changed

8 files changed

+254
-0
lines changed

android/guava/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@
9090
</plugin>
9191
<plugin>
9292
<artifactId>maven-compiler-plugin</artifactId>
93+
<executions>
94+
<execution>
95+
<!--
96+
The execution named default-compile happens first, regardless
97+
of the order of the executions in the source file. So, because
98+
Java8Usage is a dependency of the main sources, we need to call
99+
its compilation "default-compile," even though it's the special
100+
case.
101+
-->
102+
<id>default-compile</id>
103+
<phase>compile</phase>
104+
<goals>
105+
<goal>compile</goal>
106+
</goals>
107+
<configuration>
108+
<includes>
109+
<include>**/Java8Usage.java</include>
110+
</includes>
111+
<!-- -source 8 -target 8 is a no-op in the mainline but matters in the backport. -->
112+
<source>8</source>
113+
<target>8</target>
114+
</configuration>
115+
</execution>
116+
<execution>
117+
<id>main-compile</id>
118+
<phase>compile</phase>
119+
<goals>
120+
<goal>compile</goal>
121+
</goals>
122+
<configuration>
123+
<excludes>
124+
<exclude>**/Java8Usage.java</exclude>
125+
</excludes>
126+
</configuration>
127+
</execution>
128+
</executions>
93129
</plugin>
94130
<plugin>
95131
<artifactId>maven-source-plugin</artifactId>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2020 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.common.base;
18+
19+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
20+
21+
/**
22+
* A class that uses a couple Java 8 features but doesn't really do anything. This lets us attempt
23+
* to load it and log a warning if that fails, giving users advance notice of our dropping Java 8
24+
* support.
25+
*/
26+
/*
27+
* This class should be annotated @GwtCompatible. But if we annotate it @GwtCompatible, then we need
28+
* to build GwtCompatible.java (-source 7 -target 7 in the Android flavor) before we build
29+
* Java8Usage.java (-source 8 target 8, which we already need to build before the rest of
30+
* common.base). We could configure Maven to do that, but it's easier to just skip the annotation.
31+
*/
32+
final class Java8Usage {
33+
@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE)
34+
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
35+
private @interface SomeTypeAnnotation {}
36+
37+
@CanIgnoreReturnValue
38+
static @SomeTypeAnnotation String performCheck() {
39+
Runnable r = () -> {};
40+
r.run();
41+
return "";
42+
}
43+
44+
private Java8Usage() {}
45+
}

android/guava/src/com/google/common/base/MoreObjects.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
package com.google.common.base;
1616

1717
import static com.google.common.base.Preconditions.checkNotNull;
18+
import static java.util.logging.Level.WARNING;
1819

1920
import com.google.common.annotations.GwtCompatible;
2021
import com.google.errorprone.annotations.CanIgnoreReturnValue;
22+
import com.google.errorprone.annotations.concurrent.GuardedBy;
2123
import java.util.Arrays;
24+
import java.util.logging.Logger;
2225
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
2326

2427
/**
@@ -141,13 +144,46 @@ public static ToStringHelper toStringHelper(String className) {
141144
* @since 18.0 (since 2.0 as {@code Objects.ToStringHelper}).
142145
*/
143146
public static final class ToStringHelper {
147+
@GuardedBy("ToStringHelper.class")
148+
private static boolean performedJava8CompatibilityCheck;
149+
150+
private static void java8CompatibilityCheck() {
151+
@SuppressWarnings("GuardedBy")
152+
boolean racyReadForDoubleCheckedLock = performedJava8CompatibilityCheck;
153+
if (racyReadForDoubleCheckedLock) {
154+
return;
155+
}
156+
synchronized (ToStringHelper.class) {
157+
if (performedJava8CompatibilityCheck) {
158+
return;
159+
}
160+
performedJava8CompatibilityCheck = true;
161+
}
162+
163+
try {
164+
Java8Usage.performCheck();
165+
} catch (Throwable underlying) {
166+
Exception toLog =
167+
new Exception(
168+
"Guava will drop support for Java 7 in 2021. Please let us know if this will cause"
169+
+ " you problems: https://github.com/google/guava/issues/5269",
170+
underlying);
171+
Logger.getLogger(ToStringHelper.class.getName())
172+
.log(
173+
WARNING,
174+
"Java 7 compatibility warning: See https://github.com/google/guava/issues/5269",
175+
toLog);
176+
}
177+
}
178+
144179
private final String className;
145180
private final ValueHolder holderHead = new ValueHolder();
146181
private ValueHolder holderTail = holderHead;
147182
private boolean omitNullValues = false;
148183

149184
/** Use {@link MoreObjects#toStringHelper(Object)} to create an instance. */
150185
private ToStringHelper(String className) {
186+
java8CompatibilityCheck();
151187
this.className = checkNotNull(className);
152188
}
153189

android/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@
118118
<configuration>
119119
<source>1.7</source>
120120
<target>1.7</target>
121+
<compilerArgs>
122+
<!--
123+
Make includes/excludes fully work:
124+
https://issues.apache.org/jira/browse/MCOMPILER-174
125+
126+
(Compare what guava-gwt has to do for maven-javadoc-plugin.)
127+
-->
128+
<arg>-sourcepath</arg>
129+
<arg>doesnotexist</arg>
130+
</compilerArgs>
121131
</configuration>
122132
</plugin>
123133
<plugin>

guava/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@
9090
</plugin>
9191
<plugin>
9292
<artifactId>maven-compiler-plugin</artifactId>
93+
<executions>
94+
<execution>
95+
<!--
96+
The execution named default-compile happens first, regardless
97+
of the order of the executions in the source file. So, because
98+
Java8Usage is a dependency of the main sources, we need to call
99+
its compilation "default-compile," even though it's the special
100+
case.
101+
-->
102+
<id>default-compile</id>
103+
<phase>compile</phase>
104+
<goals>
105+
<goal>compile</goal>
106+
</goals>
107+
<configuration>
108+
<includes>
109+
<include>**/Java8Usage.java</include>
110+
</includes>
111+
<!-- -source 8 -target 8 is a no-op in the mainline but matters in the backport. -->
112+
<source>8</source>
113+
<target>8</target>
114+
</configuration>
115+
</execution>
116+
<execution>
117+
<id>main-compile</id>
118+
<phase>compile</phase>
119+
<goals>
120+
<goal>compile</goal>
121+
</goals>
122+
<configuration>
123+
<excludes>
124+
<exclude>**/Java8Usage.java</exclude>
125+
</excludes>
126+
</configuration>
127+
</execution>
128+
</executions>
93129
</plugin>
94130
<plugin>
95131
<artifactId>maven-source-plugin</artifactId>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2020 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.common.base;
18+
19+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
20+
21+
/**
22+
* A class that uses a couple Java 8 features but doesn't really do anything. This lets us attempt
23+
* to load it and log a warning if that fails, giving users advance notice of our dropping Java 8
24+
* support.
25+
*/
26+
/*
27+
* This class should be annotated @GwtCompatible. But if we annotate it @GwtCompatible, then we need
28+
* to build GwtCompatible.java (-source 7 -target 7 in the Android flavor) before we build
29+
* Java8Usage.java (-source 8 target 8, which we already need to build before the rest of
30+
* common.base). We could configure Maven to do that, but it's easier to just skip the annotation.
31+
*/
32+
final class Java8Usage {
33+
@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE)
34+
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
35+
private @interface SomeTypeAnnotation {}
36+
37+
@CanIgnoreReturnValue
38+
static @SomeTypeAnnotation String performCheck() {
39+
Runnable r = () -> {};
40+
r.run();
41+
return "";
42+
}
43+
44+
private Java8Usage() {}
45+
}

guava/src/com/google/common/base/MoreObjects.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
package com.google.common.base;
1616

1717
import static com.google.common.base.Preconditions.checkNotNull;
18+
import static java.util.logging.Level.WARNING;
1819

1920
import com.google.common.annotations.GwtCompatible;
2021
import com.google.errorprone.annotations.CanIgnoreReturnValue;
22+
import com.google.errorprone.annotations.concurrent.GuardedBy;
2123
import java.util.Arrays;
24+
import java.util.logging.Logger;
2225
import org.checkerframework.checker.nullness.qual.Nullable;
2326

2427
/**
@@ -141,13 +144,46 @@ public static ToStringHelper toStringHelper(String className) {
141144
* @since 18.0 (since 2.0 as {@code Objects.ToStringHelper}).
142145
*/
143146
public static final class ToStringHelper {
147+
@GuardedBy("ToStringHelper.class")
148+
private static boolean performedJava8CompatibilityCheck;
149+
150+
private static void java8CompatibilityCheck() {
151+
@SuppressWarnings("GuardedBy")
152+
boolean racyReadForDoubleCheckedLock = performedJava8CompatibilityCheck;
153+
if (racyReadForDoubleCheckedLock) {
154+
return;
155+
}
156+
synchronized (ToStringHelper.class) {
157+
if (performedJava8CompatibilityCheck) {
158+
return;
159+
}
160+
performedJava8CompatibilityCheck = true;
161+
}
162+
163+
try {
164+
Java8Usage.performCheck();
165+
} catch (Throwable underlying) {
166+
Exception toLog =
167+
new Exception(
168+
"Guava will drop support for Java 7 in 2021. Please let us know if this will cause"
169+
+ " you problems: https://github.com/google/guava/issues/5269",
170+
underlying);
171+
Logger.getLogger(ToStringHelper.class.getName())
172+
.log(
173+
WARNING,
174+
"Java 7 compatibility warning: See https://github.com/google/guava/issues/5269",
175+
toLog);
176+
}
177+
}
178+
144179
private final String className;
145180
private final ValueHolder holderHead = new ValueHolder();
146181
private ValueHolder holderTail = holderHead;
147182
private boolean omitNullValues = false;
148183

149184
/** Use {@link MoreObjects#toStringHelper(Object)} to create an instance. */
150185
private ToStringHelper(String className) {
186+
java8CompatibilityCheck();
151187
this.className = checkNotNull(className);
152188
}
153189

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@
119119
<configuration>
120120
<source>1.8</source>
121121
<target>1.8</target>
122+
<compilerArgs>
123+
<!--
124+
Make includes/excludes fully work:
125+
https://issues.apache.org/jira/browse/MCOMPILER-174
126+
127+
(Compare what guava-gwt has to do for maven-javadoc-plugin.)
128+
-->
129+
<arg>-sourcepath</arg>
130+
<arg>doesnotexist</arg>
131+
</compilerArgs>
122132
</configuration>
123133
</plugin>
124134
<plugin>

0 commit comments

Comments
 (0)