From d8ec011d770e1e04ef4f87fba673f3748f363278 Mon Sep 17 00:00:00 2001 From: Dmitry Ovchinnikov Date: Sun, 12 Nov 2017 21:49:07 +0300 Subject: [PATCH] LANG-1371: Fix TypeUtils.parameterize to work correctly with narrower-typed varargs array (closes #307) --- .../org/apache/commons/lang3/reflect/TypeUtils.java | 2 +- .../apache/commons/lang3/reflect/TypeUtilsTest.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java b/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java index 3a7ac1780..efb630c62 100644 --- a/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java +++ b/src/main/java/org/apache/commons/lang3/reflect/TypeUtils.java @@ -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); } /** diff --git a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java index acdf77c86..a9bb9fab6 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java @@ -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 { assertEquals("java.lang.Comparable", stringComparableType.toString()); } + @Test + public void testParameterizeNarrowerTypeArray() { + final TypeVariable[] variables = ArrayList.class.getTypeParameters(); + final ParameterizedType parameterizedType = TypeUtils.parameterize(ArrayList.class, variables); + final Map, Type> mapping = Collections., 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);