Skip to content

Commit

Permalink
Use most of the main AbstractFuture implementation from J2KT and fr…
Browse files Browse the repository at this point in the history
…om 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
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Feb 20, 2025
1 parent c4965e4 commit 9062e9f
Show file tree
Hide file tree
Showing 16 changed files with 2,088 additions and 1,941 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Arrays.stream;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Stream.concat;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
Expand All @@ -33,6 +36,7 @@
import com.google.common.reflect.Parameter;
import com.google.common.reflect.Reflection;
import com.google.common.reflect.TypeToken;
import com.google.common.util.concurrent.AbstractFuture;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -88,8 +92,28 @@ public NullPointerTester() {
*/
ignoredMembers.add(Converter.class.getMethod("apply", Object.class));
} catch (NoSuchMethodException shouldBeImpossible) {
// OK, fine: If it doesn't exist, then there's chance that we're going to be asked to test it.
// Fine: If it doesn't exist, then there's no chance that we're going to be asked to test it.
}

/*
* These methods "should" call checkNotNull. However, I'm wary of accidentally introducing
* anything that might slow down execution on such a hot path. Given that the methods are only
* package-private, I feel OK with just not testing them for NPE.
*
* Note that testing casValue is particularly dangerous because it uses Unsafe under some
* versions of Java, and apparently Unsafe can cause SIGSEGV instead of NPE—almost as if it's
* not safe.
*/
concat(
stream(AbstractFuture.class.getDeclaredMethods()),
stream(requireNonNull(AbstractFuture.class.getSuperclass()).getDeclaredMethods()))
.filter(
m ->
m.getName().equals("getDoneValue")
|| m.getName().equals("casValue")
|| m.getName().equals("casListeners")
|| m.getName().equals("gasListeners"))
.forEach(ignoredMembers::add);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ private void runTestMethod(ClassLoader classLoader) throws Exception {
private void checkHelperVersion(ClassLoader classLoader, String expectedHelperClassName)
throws Exception {
// Make sure we are actually running with the expected helper implementation
Class<?> abstractFutureClass = classLoader.loadClass(AbstractFuture.class.getName());
Field helperField = abstractFutureClass.getDeclaredField("ATOMIC_HELPER");
Class<?> abstractFutureStateClass = classLoader.loadClass(AbstractFutureState.class.getName());
Field helperField = abstractFutureStateClass.getDeclaredField("ATOMIC_HELPER");
helperField.setAccessible(true);
assertEquals(expectedHelperClassName, helperField.get(null).getClass().getSimpleName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ private void runTestMethod(ClassLoader classLoader) throws Exception {
private void checkHelperVersion(ClassLoader classLoader, String expectedHelperClassName)
throws Exception {
// Make sure we are actually running with the expected helper implementation
Class<?> abstractFutureClass = classLoader.loadClass(AggregateFutureState.class.getName());
Field helperField = abstractFutureClass.getDeclaredField("ATOMIC_HELPER");
Class<?> abstractFutureStateClass = classLoader.loadClass(AggregateFutureState.class.getName());
Field helperField = abstractFutureStateClass.getDeclaredField("ATOMIC_HELPER");
helperField.setAccessible(true);
assertEquals(expectedHelperClassName, helperField.get(null).getClass().getSimpleName());
}
Expand Down
Loading

0 comments on commit 9062e9f

Please sign in to comment.