mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Throw on missing type variables in Types.newParameterizedType* methods
It's a programmer error to omit this, but vararg declaration allows you to. This checks that. We could improve it to ensure matching lengths or bounds, but this is a good start.
This commit is contained in:
@@ -109,6 +109,9 @@ public final class Types {
|
|||||||
* method if {@code rawType} is not enclosed in another type.
|
* method if {@code rawType} is not enclosed in another type.
|
||||||
*/
|
*/
|
||||||
public static ParameterizedType newParameterizedType(Type rawType, Type... typeArguments) {
|
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);
|
return new ParameterizedTypeImpl(null, rawType, typeArguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,6 +121,9 @@ public final class Types {
|
|||||||
*/
|
*/
|
||||||
public static ParameterizedType newParameterizedTypeWithOwner(
|
public static ParameterizedType newParameterizedTypeWithOwner(
|
||||||
Type ownerType, Type rawType, Type... typeArguments) {
|
Type ownerType, Type rawType, Type... typeArguments) {
|
||||||
|
if (typeArguments.length == 0) {
|
||||||
|
throw new IllegalArgumentException("Missing type arguments for " + rawType);
|
||||||
|
}
|
||||||
return new ParameterizedTypeImpl(ownerType, rawType, typeArguments);
|
return new ParameterizedTypeImpl(ownerType, rawType, typeArguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -85,6 +85,22 @@ public final class TypesTest {
|
|||||||
assertThat(getFirstTypeArgument(type)).isEqualTo(B.class);
|
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 {
|
@Test public void parameterizedTypeWithRequiredOwnerMissing() throws Exception {
|
||||||
try {
|
try {
|
||||||
Types.newParameterizedType(A.class, B.class);
|
Types.newParameterizedType(A.class, B.class);
|
||||||
|
Reference in New Issue
Block a user