|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2012 the original author or authors. |
| 2 | + * Copyright 2002-2013 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.core.convert;
|
18 | 18 |
|
19 | 19 | import java.lang.annotation.Annotation;
|
| 20 | +import java.lang.reflect.Array; |
20 | 21 | import java.lang.reflect.Field;
|
21 | 22 | import java.util.Collection;
|
22 | 23 | import java.util.HashMap;
|
|
33 | 34 | * @author Keith Donald
|
34 | 35 | * @author Andy Clement
|
35 | 36 | * @author Juergen Hoeller
|
| 37 | + * @author Phillip Webb |
36 | 38 | * @since 3.0
|
37 | 39 | */
|
38 | 40 | public class TypeDescriptor {
|
@@ -146,6 +148,22 @@ public static TypeDescriptor map(Class<?> mapType, TypeDescriptor keyTypeDescrip
|
146 | 148 | return new TypeDescriptor(mapType, keyTypeDescriptor, valueTypeDescriptor);
|
147 | 149 | }
|
148 | 150 |
|
| 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 | + |
149 | 167 | /**
|
150 | 168 | * Creates a type descriptor for a nested type declared within the method parameter.
|
151 | 169 | * For example, if the methodParameter is a List<String> and the nestingLevel is 1, the nested type descriptor will be String.class.
|
|
0 commit comments