Browse Source

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
pull/218/head
Phillip Webb 12 years ago
parent
commit
9c032d52d4
  1. 20
      spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
  2. 23
      spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
package org.springframework.core.convert;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
@ -33,6 +34,7 @@ import org.springframework.util.ObjectUtils; @@ -33,6 +34,7 @@ import org.springframework.util.ObjectUtils;
* @author Keith Donald
* @author Andy Clement
* @author Juergen Hoeller
* @author Phillip Webb
* @since 3.0
*/
public class TypeDescriptor {
@ -146,6 +148,22 @@ public class TypeDescriptor { @@ -146,6 +148,22 @@ public class TypeDescriptor {
return new TypeDescriptor(mapType, keyTypeDescriptor, valueTypeDescriptor);
}
/**
* Create a new type descriptor as an array of the specified type. For example to
* create a {@code Map<String,String>[]} use
* {@code TypeDescriptor.array(TypeDescriptor.map(Map.class, TypeDescriptor.value(String.class), TypeDescriptor.value(String.class)))}.
* @param elementTypeDescriptor the {@link TypeDescriptor} of the array element or {@code null}
* @return an array {@link TypeDescriptor} or {@code null} if {@code elementTypeDescriptor} is {@code null}
* @since 3.2
*/
public static TypeDescriptor array(TypeDescriptor elementTypeDescriptor) {
if(elementTypeDescriptor == null) {
return null;
}
Class<?> type = Array.newInstance(elementTypeDescriptor.getType(), 0).getClass();
return new TypeDescriptor(type, elementTypeDescriptor, null, null, elementTypeDescriptor.getAnnotations());
}
/**
* Creates a type descriptor for a nested type declared within the method parameter.
* For example, if the methodParameter is a List&lt;String&gt; and the nestingLevel is 1, the nested type descriptor will be String.class.

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,6 +25,7 @@ import java.util.Collection; @@ -25,6 +25,7 @@ import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -36,6 +37,7 @@ import static org.junit.Assert.*; @@ -36,6 +37,7 @@ import static org.junit.Assert.*;
/**
* @author Keith Donald
* @author Andy Clement
* @author Phillip Webb
*/
@SuppressWarnings("rawtypes")
public class TypeDescriptorTests {
@ -849,4 +851,23 @@ public class TypeDescriptorTests { @@ -849,4 +851,23 @@ public class TypeDescriptorTests {
assertEquals(TypeDescriptor.forObject(new CustomMap()).getMapValueTypeDescriptor(), TypeDescriptor.valueOf(Integer.class));
}
@Test
public void createMapArray() throws Exception {
TypeDescriptor mapType = TypeDescriptor.map(LinkedHashMap.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
TypeDescriptor arrayType = TypeDescriptor.array(mapType);
assertEquals(arrayType.getType(), LinkedHashMap[].class);
assertEquals(arrayType.getElementTypeDescriptor(), mapType);
}
@Test
public void createStringArray() throws Exception {
TypeDescriptor arrayType = TypeDescriptor.array(TypeDescriptor.valueOf(String.class));
assertEquals(arrayType, TypeDescriptor.valueOf(String[].class));
}
@Test
public void createNullArray() throws Exception {
assertNull(TypeDescriptor.array(null));
}
}

Loading…
Cancel
Save