Skip to content

Commit 9c032d5

Browse files
committed
Allow TypeDescriptor array construction
Add a static factory method that can be used to create an array TypeDescriptor with a specific element type. Allows array types with generic elements to be constructed. Issue: SPR-9792
1 parent 3c09b07 commit 9c032d5

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.core.convert;
1818

1919
import java.lang.annotation.Annotation;
20+
import java.lang.reflect.Array;
2021
import java.lang.reflect.Field;
2122
import java.util.Collection;
2223
import java.util.HashMap;
@@ -33,6 +34,7 @@
3334
* @author Keith Donald
3435
* @author Andy Clement
3536
* @author Juergen Hoeller
37+
* @author Phillip Webb
3638
* @since 3.0
3739
*/
3840
public class TypeDescriptor {
@@ -146,6 +148,22 @@ public static TypeDescriptor map(Class<?> mapType, TypeDescriptor keyTypeDescrip
146148
return new TypeDescriptor(mapType, keyTypeDescriptor, valueTypeDescriptor);
147149
}
148150

151+
/**
152+
* Create a new type descriptor as an array of the specified type. For example to
153+
* create a {@code Map<String,String>[]} use
154+
* {@code TypeDescriptor.array(TypeDescriptor.map(Map.class, TypeDescriptor.value(String.class), TypeDescriptor.value(String.class)))}.
155+
* @param elementTypeDescriptor the {@link TypeDescriptor} of the array element or {@code null}
156+
* @return an array {@link TypeDescriptor} or {@code null} if {@code elementTypeDescriptor} is {@code null}
157+
* @since 3.2
158+
*/
159+
public static TypeDescriptor array(TypeDescriptor elementTypeDescriptor) {
160+
if(elementTypeDescriptor == null) {
161+
return null;
162+
}
163+
Class<?> type = Array.newInstance(elementTypeDescriptor.getType(), 0).getClass();
164+
return new TypeDescriptor(type, elementTypeDescriptor, null, null, elementTypeDescriptor.getAnnotations());
165+
}
166+
149167
/**
150168
* Creates a type descriptor for a nested type declared within the method parameter.
151169
* For example, if the methodParameter is a List&lt;String&gt; and the nestingLevel is 1, the nested type descriptor will be String.class.

spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import java.util.Date;
2626
import java.util.HashMap;
2727
import java.util.HashSet;
28+
import java.util.LinkedHashMap;
2829
import java.util.List;
2930
import java.util.Map;
3031

@@ -36,6 +37,7 @@
3637
/**
3738
* @author Keith Donald
3839
* @author Andy Clement
40+
* @author Phillip Webb
3941
*/
4042
@SuppressWarnings("rawtypes")
4143
public class TypeDescriptorTests {
@@ -849,4 +851,23 @@ class CustomMap extends HashMap<String, Integer> {
849851
assertEquals(TypeDescriptor.forObject(new CustomMap()).getMapValueTypeDescriptor(), TypeDescriptor.valueOf(Integer.class));
850852
}
851853

854+
@Test
855+
public void createMapArray() throws Exception {
856+
TypeDescriptor mapType = TypeDescriptor.map(LinkedHashMap.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
857+
TypeDescriptor arrayType = TypeDescriptor.array(mapType);
858+
assertEquals(arrayType.getType(), LinkedHashMap[].class);
859+
assertEquals(arrayType.getElementTypeDescriptor(), mapType);
860+
}
861+
862+
863+
@Test
864+
public void createStringArray() throws Exception {
865+
TypeDescriptor arrayType = TypeDescriptor.array(TypeDescriptor.valueOf(String.class));
866+
assertEquals(arrayType, TypeDescriptor.valueOf(String[].class));
867+
}
868+
869+
@Test
870+
public void createNullArray() throws Exception {
871+
assertNull(TypeDescriptor.array(null));
872+
}
852873
}

0 commit comments

Comments
 (0)