Merge pull request #392 from square/eric.primitives

Add error message for accidental primitive usage.
This commit is contained in:
Jesse Wilson 2017-11-21 19:06:13 -05:00 committed by GitHub
commit 03323ae998
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View file

@ -492,7 +492,7 @@ public final class Types {
static void checkNotPrimitive(Type type) {
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(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.");
}
}
}