LANG-1371: Fix TypeUtils.parameterize to work correctly with narrower-typed varargs array (closes #307)

This commit is contained in:
Dmitry Ovchinnikov 2017-11-12 21:49:07 +03:00 committed by pascalschumacher
parent bfb43d3fe2
commit d8ec011d77
2 changed files with 11 additions and 1 deletions

View File

@ -157,7 +157,7 @@ public class TypeUtils {
private ParameterizedTypeImpl(final Class<?> raw, final Type useOwner, final Type[] typeArguments) {
this.raw = raw;
this.useOwner = useOwner;
this.typeArguments = typeArguments.clone();
this.typeArguments = Arrays.copyOf(typeArguments, typeArguments.length, Type[].class);
}
/**

View File

@ -34,6 +34,7 @@ import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -690,6 +691,15 @@ public class TypeUtilsTest<B> {
assertEquals("java.lang.Comparable<java.lang.String>", stringComparableType.toString());
}
@Test
public void testParameterizeNarrowerTypeArray() {
final TypeVariable<?>[] variables = ArrayList.class.getTypeParameters();
final ParameterizedType parameterizedType = TypeUtils.parameterize(ArrayList.class, variables);
final Map<TypeVariable<?>, Type> mapping = Collections.<TypeVariable<?>, Type>singletonMap(variables[0], String.class);
final Type unrolled = TypeUtils.unrollVariables(mapping, parameterizedType);
assertEquals(TypeUtils.parameterize(ArrayList.class, String.class), unrolled);
}
@Test
public void testParameterizeWithOwner() throws Exception {
final Type owner = TypeUtils.parameterize(TypeUtilsTest.class, String.class);