Skip to content

Commit

Permalink
Make our Android builds work with an Android bootclasspath.
Browse files Browse the repository at this point in the history
Notably:

- Remove all usages of `AnnotatedType` from the Android flavor.
- Make `NullPointerTester` a no-op in the Android flavor (instead of only in the Android flavor _when running under an Android VM_).
  - (This requires removing its tests from the Android flavor entirely, rather than merely skipping them during our internal Android-VM test runs.)

In both cases, these changes apply only to (a) guava-android and (b) our actual internal _Android_ builds. In contrast, in the internal copy of the backport _that we test under a JRE_, the `AnnotatedType` APIs are still in place, and `NullPointerTester` still runs.

RELNOTES=`reflect`: In `guava-android` only, removed `Invokable.getAnnotatedReturnType()` and `Parameter.getAnnotatedType()`. These methods never worked in an Android VM, and to reflect that, they were born `@Deprecated`, `@Beta`, and `@DoNotCall`. They're now preventing us from rolling out some new Android compatibility testing.
PiperOrigin-RevId: 583032835
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Nov 16, 2023
1 parent 7feac90 commit 045cd84
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 2,927 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@
import com.google.common.reflect.TypeToken;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -353,9 +351,9 @@ private void testParameter(
@Nullable Object instance, Invokable<?, ?> invokable, int paramIndex, Class<?> testedClass) {
/*
* com.google.common is starting to rely on type-use annotations, which aren't visible under
* Android VMs. So we skip testing there.
* Android VMs and in open-source guava-android. So we skip testing there.
*/
if (isAndroid() && Reflection.getPackageName(testedClass).startsWith("com.google.common")) {
if (Reflection.getPackageName(testedClass).startsWith("com.google.common")) {
return;
}
if (isPrimitiveOrNullable(invokable.getParameters().get(paramIndex))) {
Expand Down Expand Up @@ -612,39 +610,19 @@ private static boolean annotatedTypeExists() {
* don't know that anyone uses it there, anyway.
*/
private enum NullnessAnnotationReader {
// Usages (which are unsafe only for Android) are guarded by the annotatedTypeExists() check.
@SuppressWarnings({"Java7ApiChecker", "AndroidApiChecker", "DoNotCall", "deprecation"})
@SuppressWarnings("Java7ApiChecker")
FROM_DECLARATION_AND_TYPE_USE_ANNOTATIONS {
@Override
@IgnoreJRERequirement
boolean isNullable(Invokable<?, ?> invokable) {
return FROM_DECLARATION_ANNOTATIONS_ONLY.isNullable(invokable)
|| containsNullable(invokable.getAnnotatedReturnType().getAnnotations());
;
// TODO(cpovirk): Should we also check isNullableTypeVariable?
}

@Override
@IgnoreJRERequirement
boolean isNullable(Parameter param) {
return FROM_DECLARATION_ANNOTATIONS_ONLY.isNullable(param)
|| containsNullable(param.getAnnotatedType().getAnnotations())
|| isNullableTypeVariable(param.getAnnotatedType().getType());
}

@IgnoreJRERequirement
boolean isNullableTypeVariable(Type type) {
if (!(type instanceof TypeVariable)) {
return false;
}
TypeVariable<?> typeVar = (TypeVariable<?>) type;
for (AnnotatedType bound : typeVar.getAnnotatedBounds()) {
// Until Java 15, the isNullableTypeVariable case here won't help:
// https://bugs.openjdk.java.net/browse/JDK-8202469
if (containsNullable(bound.getAnnotations()) || isNullableTypeVariable(bound.getType())) {
return true;
}
}
return false;
;
}
},
FROM_DECLARATION_ANNOTATIONS_ONLY {
Expand All @@ -663,9 +641,4 @@ boolean isNullable(Parameter param) {

abstract boolean isNullable(Parameter param);
}

private static boolean isAndroid() {
// Arguably it would make more sense to test "can we see type-use annotations" directly....
return checkNotNull(System.getProperty("java.runtime.name", "")).contains("Android");
}
}
Loading

0 comments on commit 045cd84

Please sign in to comment.