aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉamonn McManus <emcmanus@google.com>2021-04-20 10:08:09 -0700
committerGoogle Java Core Libraries <java-libraries-firehose+copybara@google.com>2021-04-20 10:08:55 -0700
commit79d07c5889c0bbd2d99cfc24634e6347adb07a54 (patch)
tree82f4b670f46d29b84b07aa22aed6441675251f25
parent02cd653c53258fb8160734dfc472c29f1710bc56 (diff)
downloadauto-79d07c5889c0bbd2d99cfc24634e6347adb07a54.tar.gz
`MoreTypes.isTypeOf` returns false for `ErrorType` rather than throwing.
Throwing `IllegalArgumentException` can mask the real source of a problem, or prevent code that would have handled the `ErrorType` from running. The `ErrorType` typically (maybe always) corresponds to an identifier in the source code, so it is in fact a class, just one that doesn't exist. RELNOTES=MoreTypes.isTypeOf returns false for ErrorType rather than throwing. PiperOrigin-RevId: 369464743
-rw-r--r--common/src/main/java/com/google/auto/common/MoreTypes.java5
-rw-r--r--common/src/test/java/com/google/auto/common/MoreTypesTest.java16
2 files changed, 21 insertions, 0 deletions
diff --git a/common/src/main/java/com/google/auto/common/MoreTypes.java b/common/src/main/java/com/google/auto/common/MoreTypes.java
index b84432f4..af433aa0 100644
--- a/common/src/main/java/com/google/auto/common/MoreTypes.java
+++ b/common/src/main/java/com/google/auto/common/MoreTypes.java
@@ -836,6 +836,11 @@ public final class MoreTypes {
}
@Override
+ public Boolean visitError(ErrorType errorType, Void p) {
+ return false;
+ }
+
+ @Override
public Boolean visitPrimitive(PrimitiveType type, Void p) {
switch (type.getKind()) {
case BOOLEAN:
diff --git a/common/src/test/java/com/google/auto/common/MoreTypesTest.java b/common/src/test/java/com/google/auto/common/MoreTypesTest.java
index 3cd360db..ff33ffc1 100644
--- a/common/src/test/java/com/google/auto/common/MoreTypesTest.java
+++ b/common/src/test/java/com/google/auto/common/MoreTypesTest.java
@@ -18,6 +18,7 @@ package com.google.auto.common;
import static com.google.common.truth.Truth.assertThat;
import static javax.lang.model.type.TypeKind.NONE;
import static javax.lang.model.type.TypeKind.VOID;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import com.google.common.base.Function;
@@ -471,6 +472,21 @@ public class MoreTypesTest {
}
}
+ @Test
+ public void testIsTypeOf() {
+ Types types = compilationRule.getTypes();
+ PrimitiveType intType = types.getPrimitiveType(TypeKind.INT);
+ TypeMirror integerType = types.boxedClass(intType).asType();
+ WildcardType wildcardType = types.getWildcardType(null, null);
+ expect.that(MoreTypes.isTypeOf(int.class, intType)).isTrue();
+ expect.that(MoreTypes.isTypeOf(Integer.class, integerType)).isTrue();
+ expect.that(MoreTypes.isTypeOf(Integer.class, intType)).isFalse();
+ expect.that(MoreTypes.isTypeOf(int.class, integerType)).isFalse();
+ expect.that(MoreTypes.isTypeOf(Integer.class, FAKE_ERROR_TYPE)).isFalse();
+ assertThrows(
+ IllegalArgumentException.class, () -> MoreTypes.isTypeOf(Integer.class, wildcardType));
+ }
+
// The type of every field here is such that casting to it provokes an "unchecked" warning.
@SuppressWarnings("unused")
private static class Unchecked<T> {