mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Merge pull request #933 from square/z/throwOnMissingTypeVars
Throw on missing type variables in Types.newParameterizedType* methods
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user