Skip to content

Commit 9062e9f

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Use most of the main AbstractFuture implementation from J2KT and from GWT/J2CL.
This CL introduces a superclass, `AbstractFutureState`, following the pattern of [`AggregateFutureState`](https://github.com/google/guava/blob/master/guava/src/com/google/common/util/concurrent/AggregateFutureState.java). That superclass contains platform-specific operations. Fixes #2934 RELNOTES=n/a PiperOrigin-RevId: 726495822
1 parent c4965e4 commit 9062e9f

File tree

16 files changed

+2088
-1941
lines changed

16 files changed

+2088
-1941
lines changed

android/guava-testlib/src/com/google/common/testing/NullPointerTester.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
21+
import static java.util.Arrays.stream;
22+
import static java.util.Objects.requireNonNull;
23+
import static java.util.stream.Stream.concat;
2124

2225
import com.google.common.annotations.GwtIncompatible;
2326
import com.google.common.annotations.J2ktIncompatible;
@@ -33,6 +36,7 @@
3336
import com.google.common.reflect.Parameter;
3437
import com.google.common.reflect.Reflection;
3538
import com.google.common.reflect.TypeToken;
39+
import com.google.common.util.concurrent.AbstractFuture;
3640
import com.google.errorprone.annotations.CanIgnoreReturnValue;
3741
import java.lang.annotation.Annotation;
3842
import java.lang.reflect.Constructor;
@@ -88,8 +92,28 @@ public NullPointerTester() {
8892
*/
8993
ignoredMembers.add(Converter.class.getMethod("apply", Object.class));
9094
} catch (NoSuchMethodException shouldBeImpossible) {
91-
// OK, fine: If it doesn't exist, then there's chance that we're going to be asked to test it.
95+
// Fine: If it doesn't exist, then there's no chance that we're going to be asked to test it.
9296
}
97+
98+
/*
99+
* These methods "should" call checkNotNull. However, I'm wary of accidentally introducing
100+
* anything that might slow down execution on such a hot path. Given that the methods are only
101+
* package-private, I feel OK with just not testing them for NPE.
102+
*
103+
* Note that testing casValue is particularly dangerous because it uses Unsafe under some
104+
* versions of Java, and apparently Unsafe can cause SIGSEGV instead of NPE—almost as if it's
105+
* not safe.
106+
*/
107+
concat(
108+
stream(AbstractFuture.class.getDeclaredMethods()),
109+
stream(requireNonNull(AbstractFuture.class.getSuperclass()).getDeclaredMethods()))
110+
.filter(
111+
m ->
112+
m.getName().equals("getDoneValue")
113+
|| m.getName().equals("casValue")
114+
|| m.getName().equals("casListeners")
115+
|| m.getName().equals("gasListeners"))
116+
.forEach(ignoredMembers::add);
93117
}
94118

95119
/**

android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureFallbackAtomicHelperTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ private void runTestMethod(ClassLoader classLoader) throws Exception {
110110
private void checkHelperVersion(ClassLoader classLoader, String expectedHelperClassName)
111111
throws Exception {
112112
// Make sure we are actually running with the expected helper implementation
113-
Class<?> abstractFutureClass = classLoader.loadClass(AbstractFuture.class.getName());
114-
Field helperField = abstractFutureClass.getDeclaredField("ATOMIC_HELPER");
113+
Class<?> abstractFutureStateClass = classLoader.loadClass(AbstractFutureState.class.getName());
114+
Field helperField = abstractFutureStateClass.getDeclaredField("ATOMIC_HELPER");
115115
helperField.setAccessible(true);
116116
assertEquals(expectedHelperClassName, helperField.get(null).getClass().getSimpleName());
117117
}

android/guava-tests/test/com/google/common/util/concurrent/AggregateFutureStateFallbackAtomicHelperTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ private void runTestMethod(ClassLoader classLoader) throws Exception {
112112
private void checkHelperVersion(ClassLoader classLoader, String expectedHelperClassName)
113113
throws Exception {
114114
// Make sure we are actually running with the expected helper implementation
115-
Class<?> abstractFutureClass = classLoader.loadClass(AggregateFutureState.class.getName());
116-
Field helperField = abstractFutureClass.getDeclaredField("ATOMIC_HELPER");
115+
Class<?> abstractFutureStateClass = classLoader.loadClass(AggregateFutureState.class.getName());
116+
Field helperField = abstractFutureStateClass.getDeclaredField("ATOMIC_HELPER");
117117
helperField.setAccessible(true);
118118
assertEquals(expectedHelperClassName, helperField.get(null).getClass().getSimpleName());
119119
}

0 commit comments

Comments
 (0)