Skip to content

Commit

Permalink
Support type annotations in NullPointerTester
Browse files Browse the repository at this point in the history
RELNOTES=N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=195695054
  • Loading branch information
cushon authored and ronshapiro committed May 9, 2018
1 parent 71b04c4 commit d94eb93
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
Expand Up @@ -474,15 +474,12 @@ static boolean isPrimitiveOrNullable(Parameter param) {
return param.getType().getRawType().isPrimitive() || isNullable(param);
}

private static final ImmutableSet<String> NULLABLE_ANNOTATIONS =
ImmutableSet.of(
"javax.annotation.CheckForNull",
"javax.annotation.Nullable",
"org.checkerframework.checker.nullness.compatqual.NullableDecl");
private static final ImmutableSet<String> NULLABLE_ANNOTATION_SIMPLE_NAMES =
ImmutableSet.of("CheckForNull", "Nullable", "NullableDecl", "NullableType");

static boolean isNullable(AnnotatedElement e) {
for (Annotation annotation : e.getAnnotations()) {
if (NULLABLE_ANNOTATIONS.contains(annotation.annotationType().getName())) {
if (NULLABLE_ANNOTATION_SIMPLE_NAMES.contains(annotation.annotationType().getSimpleName())) {
return true;
}
}
Expand Down
20 changes: 13 additions & 7 deletions guava-testlib/src/com/google/common/testing/NullPointerTester.java
Expand Up @@ -474,15 +474,21 @@ static boolean isPrimitiveOrNullable(Parameter param) {
return param.getType().getRawType().isPrimitive() || isNullable(param);
}

private static final ImmutableSet<String> NULLABLE_ANNOTATIONS =
ImmutableSet.of(
"javax.annotation.CheckForNull",
"javax.annotation.Nullable",
"org.checkerframework.checker.nullness.compatqual.NullableDecl");
private static final ImmutableSet<String> NULLABLE_ANNOTATION_SIMPLE_NAMES =
ImmutableSet.of("CheckForNull", "Nullable", "NullableDecl", "NullableType");

static boolean isNullable(AnnotatedElement e) {
for (Annotation annotation : e.getAnnotations()) {
if (NULLABLE_ANNOTATIONS.contains(annotation.annotationType().getName())) {
return isNullable(e.getAnnotations());
}

static boolean isNullable(Parameter param) {
return isNullable(param.getAnnotatedType().getAnnotations())
|| isNullable(param.getAnnotations());
}

private static boolean isNullable(Annotation[] annotations) {
for (Annotation annotation : annotations) {
if (NULLABLE_ANNOTATION_SIMPLE_NAMES.contains(annotation.annotationType().getSimpleName())) {
return true;
}
}
Expand Down
18 changes: 17 additions & 1 deletion guava/src/com/google/common/reflect/Invokable.java
Expand Up @@ -21,6 +21,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Constructor;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -116,9 +117,12 @@ public final TypeToken<? extends R> getReturnType() {
public final ImmutableList<Parameter> getParameters() {
Type[] parameterTypes = getGenericParameterTypes();
Annotation[][] annotations = getParameterAnnotations();
AnnotatedType[] annotatedTypes = getAnnotatedParameterTypes();
ImmutableList.Builder<Parameter> builder = ImmutableList.builder();
for (int i = 0; i < parameterTypes.length; i++) {
builder.add(new Parameter(this, i, TypeToken.of(parameterTypes[i]), annotations[i]));
builder.add(
new Parameter(
this, i, TypeToken.of(parameterTypes[i]), annotations[i], annotatedTypes[i]));
}
return builder.build();
}
Expand Down Expand Up @@ -178,6 +182,8 @@ abstract Object invokeInternal(@NullableDecl Object receiver, Object[] args)

abstract Type[] getGenericParameterTypes();

abstract AnnotatedType[] getAnnotatedParameterTypes();

/** This should never return a type that's not a subtype of Throwable. */
abstract Type[] getGenericExceptionTypes();

Expand Down Expand Up @@ -210,6 +216,11 @@ Type[] getGenericParameterTypes() {
return method.getGenericParameterTypes();
}

@Override
AnnotatedType[] getAnnotatedParameterTypes() {
return method.getAnnotatedParameterTypes();
}

@Override
Type[] getGenericExceptionTypes() {
return method.getGenericExceptionTypes();
Expand Down Expand Up @@ -287,6 +298,11 @@ Type[] getGenericParameterTypes() {
return types;
}

@Override
AnnotatedType[] getAnnotatedParameterTypes() {
return constructor.getAnnotatedParameterTypes();
}

@Override
Type[] getGenericExceptionTypes() {
return constructor.getGenericExceptionTypes();
Expand Down
15 changes: 14 additions & 1 deletion guava/src/com/google/common/reflect/Parameter.java
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.ImmutableList;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.AnnotatedType;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;

/**
Expand All @@ -36,13 +37,19 @@ public final class Parameter implements AnnotatedElement {
private final int position;
private final TypeToken<?> type;
private final ImmutableList<Annotation> annotations;
private final AnnotatedType annotatedType;

Parameter(
Invokable<?, ?> declaration, int position, TypeToken<?> type, Annotation[] annotations) {
Invokable<?, ?> declaration,
int position,
TypeToken<?> type,
Annotation[] annotations,
AnnotatedType annotatedType) {
this.declaration = declaration;
this.position = position;
this.type = type;
this.annotations = ImmutableList.copyOf(annotations);
this.annotatedType = annotatedType;
}

/** Returns the type of the parameter. */
Expand Down Expand Up @@ -104,6 +111,12 @@ public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotati
return FluentIterable.from(annotations).filter(annotationType).toArray(annotationType);
}

/** @since NEXT */
// @Override on JDK8
public AnnotatedType getAnnotatedType() {
return annotatedType;
}

@Override
public boolean equals(@NullableDecl Object obj) {
if (obj instanceof Parameter) {
Expand Down

0 comments on commit d94eb93

Please sign in to comment.