Add error message for accidental primitive usage.

Also, add a test for the requirement.
This commit is contained in:
Eric Cochran
2017-11-11 18:37:04 -08:00
parent b7a91e0557
commit 2b7e5a3453
2 changed files with 22 additions and 1 deletions

View File

@@ -492,7 +492,7 @@ public final class Types {
static void checkNotPrimitive(Type type) { static void checkNotPrimitive(Type type) {
if ((type instanceof Class<?>) && ((Class<?>) type).isPrimitive()) { if ((type instanceof Class<?>) && ((Class<?>) type).isPrimitive()) {
throw new IllegalArgumentException(); throw new IllegalArgumentException("Unexpected primitive " + type + ". Use the boxed type.");
} }
} }

View File

@@ -227,4 +227,25 @@ public final class TypesTest {
assertThat(Types.equals(String[].class, Types.arrayOf(String.class))).isTrue(); assertThat(Types.equals(String[].class, Types.arrayOf(String.class))).isTrue();
assertThat(Types.equals(Types.arrayOf(String.class), String[].class)).isTrue(); assertThat(Types.equals(Types.arrayOf(String.class), String[].class)).isTrue();
} }
@Test public void parameterizedAndWildcardTypesCannotHavePrimitiveArguments() throws Exception {
try {
Types.newParameterizedType(List.class, int.class);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Unexpected primitive int. Use the boxed type.");
}
try {
Types.subtypeOf(byte.class);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Unexpected primitive byte. Use the boxed type.");
}
try {
Types.subtypeOf(boolean.class);
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Unexpected primitive boolean. Use the boxed type.");
}
}
} }