Merge pull request #933 from square/z/throwOnMissingTypeVars

Throw on missing type variables in Types.newParameterizedType* methods
This commit is contained in:
Jesse Wilson
2019-09-26 21:57:23 -04:00
committed by GitHub
2 changed files with 22 additions and 0 deletions

View File

@@ -109,6 +109,9 @@ public final class Types {
* method if {@code rawType} is not enclosed in another type.
*/
public static ParameterizedType newParameterizedType(Type rawType, Type... typeArguments) {
if (typeArguments.length == 0) {
throw new IllegalArgumentException("Missing type arguments for " + rawType);
}
return new ParameterizedTypeImpl(null, rawType, typeArguments);
}
@@ -118,6 +121,9 @@ public final class Types {
*/
public static ParameterizedType newParameterizedTypeWithOwner(
Type ownerType, Type rawType, Type... typeArguments) {
if (typeArguments.length == 0) {
throw new IllegalArgumentException("Missing type arguments for " + rawType);
}
return new ParameterizedTypeImpl(ownerType, rawType, typeArguments);
}

View File

@@ -85,6 +85,22 @@ public final class TypesTest {
assertThat(getFirstTypeArgument(type)).isEqualTo(B.class);
}
@Test public void newParameterizedType_missingTypeVars() {
try {
Types.newParameterizedType(List.class);
fail("Should have errored due to missing type variable");
} catch (Exception e) {
assertThat(e).hasMessageContaining("Missing type arguments");
}
try {
Types.newParameterizedTypeWithOwner(TypesTest.class, A.class);
fail("Should have errored due to missing type variable");
} catch (Exception e) {
assertThat(e).hasMessageContaining("Missing type arguments");
}
}
@Test public void parameterizedTypeWithRequiredOwnerMissing() throws Exception {
try {
Types.newParameterizedType(A.class, B.class);