Skip to content

Commit 94d690f

Browse files
author
Keith Donald
committed
javadoc and polishing
1 parent 2127b16 commit 94d690f

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

org.springframework.core/src/main/java/org/springframework/core/convert/AbstractDescriptor.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ abstract class AbstractDescriptor {
2424
private final Class<?> type;
2525

2626
public AbstractDescriptor(Class<?> type) {
27-
//if (type == null) {
28-
// throw new IllegalArgumentException("type cannot be null");
29-
//}
27+
if (type == null) {
28+
throw new IllegalArgumentException("type cannot be null");
29+
}
3030
this.type = type;
3131
}
3232

@@ -68,11 +68,13 @@ public TypeDescriptor getMapValueType() {
6868

6969
public AbstractDescriptor nested() {
7070
if (isCollection()) {
71-
return nested(resolveCollectionElementType(), 0);
71+
Class<?> elementType = resolveCollectionElementType();
72+
return elementType != null ? nested(elementType, 0) : null;
7273
} else if (isArray()) {
7374
return nested(getType().getComponentType(), 0);
7475
} else if (isMap()) {
75-
return nested(resolveMapValueType(), 1);
76+
Class<?> mapValueType = resolveMapValueType();
77+
return mapValueType != null ? nested(mapValueType, 1) : null;
7678
} else {
7779
throw new IllegalStateException("Not a collection, array, or map: cannot resolve nested value types");
7880
}

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,15 @@ public TypeDescriptor getElementType() {
316316
}
317317

318318
/**
319-
* Creates a elementType descriptor from the provided collection or array element.
319+
* If this type is a {@link Collection} or an Array, creates a elementType descriptor from the provided collection or array element.
320+
* Narrows the {@link #getElementType() elementType} property to the class of the provided collection or array element.
321+
* For example, if this describes a java.util.List&lt;java.lang.Number&lt; and the element argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer.
322+
* If this describes a java.util.List&lt;?&gt; and the element argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer as well.
323+
* Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
320324
* @param element the collection or array element
321-
* @return the element type descriptor
325+
* @return a element type descriptor, narrowed to the type of the provided element
326+
* @throws IllegalStateException if this type is not a java.util.Collection or Array type
327+
* @see #narrow(Object)
322328
*/
323329
public TypeDescriptor elementType(Object element) {
324330
assertCollectionOrArray();
@@ -350,9 +356,15 @@ public TypeDescriptor getMapKeyType() {
350356
}
351357

352358
/**
353-
* Creates a mapKeyType descriptor from the provided map key.
359+
* If this type is a {@link Map}, creates a mapKeyType descriptor from the provided map key.
360+
* Narrows the {@link #getMapKeyType() mapKeyType} property to the class of the provided map key.
361+
* For example, if this describes a java.util.Map&lt;java.lang.Number, java.lang.String&lt; and the key argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer.
362+
* If this describes a java.util.Map&lt;?, ?&gt; and the key argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer as well.
363+
* Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
354364
* @param mapKey the map key
355365
* @return the map key type descriptor
366+
* @throws IllegalStateException if this type is not a java.util.Map.
367+
* @see #narrow(Object)
356368
*/
357369
public TypeDescriptor mapKeyType(Object mapKey) {
358370
assertMap();
@@ -375,9 +387,14 @@ public TypeDescriptor getMapValueType() {
375387
}
376388

377389
/**
378-
* Creates a mapValueType descriptor from the provided map value.
390+
* If this type is a {@link Map}, creates a mapValueType descriptor from the provided map value.
391+
* Narrows the {@link #getMapValueType() mapValueType} property to the class of the provided map value.
392+
* For example, if this describes a java.util.Map&lt;java.lang.String, java.lang.Number&lt; and the value argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer.
393+
* If this describes a java.util.Map&lt;?, ?&gt; and the value argument is a java.lang.Integer, the returned TypeDescriptor will be java.lang.Integer as well.
394+
* Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
379395
* @param mapValue the map value
380396
* @return the map value type descriptor
397+
* @throws IllegalStateException if this type is not a java.util.Map.
381398
*/
382399
public TypeDescriptor mapValueType(Object mapValue) {
383400
assertMap();
@@ -473,6 +490,9 @@ private TypeDescriptor(Class<?> type, TypeDescriptor elementType, TypeDescriptor
473490
private static TypeDescriptor nested(AbstractDescriptor descriptor, int nestingLevel) {
474491
for (int i = 0; i < nestingLevel; i++) {
475492
descriptor = descriptor.nested();
493+
if (descriptor == null) {
494+
return null;
495+
}
476496
}
477497
return new TypeDescriptor(descriptor);
478498
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,12 @@ public void nestedMethodParameterTypeMapTwoLevels() throws Exception {
514514
assertEquals(String.class, t1.getType());
515515
}
516516

517+
@Test(expected=IllegalStateException.class)
518+
public void nestedTooManyLevels() throws Exception {
519+
TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test4", List.class), 0), 3);
520+
assertEquals(String.class, t1.getType());
521+
}
522+
517523
@Test(expected=IllegalStateException.class)
518524
public void nestedMethodParameterTypeNotNestable() throws Exception {
519525
TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test5", String.class), 0), 2);
@@ -543,7 +549,20 @@ public void test4(List<Map<Integer, String>> param1) {
543549
public void test5(String param1) {
544550

545551
}
546-
552+
553+
@Test
554+
public void nestedNotParameterized() throws Exception {
555+
TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test6", List.class), 0), 1);
556+
assertEquals(List.class,t1.getType());
557+
assertEquals("java.util.List<?>", t1.toString());
558+
TypeDescriptor t2 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test6", List.class), 0), 2);
559+
assertNull(t2);
560+
}
561+
562+
public void test6(List<List> param1) {
563+
564+
}
565+
547566
@Test
548567
public void nestedFieldTypeMapTwoLevels() throws Exception {
549568
TypeDescriptor t1 = TypeDescriptor.nested(getClass().getField("test4"), 2);

org.springframework.expression/src/test/java/org/springframework/expression/spel/SpringEL300Tests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,6 @@ public void testProjectionTypeDescriptors_1() throws Exception {
726726
String el1 = "ls.![#this.equals('abc')]";
727727
SpelExpression exp = parser.parseRaw(el1);
728728
List value = (List)exp.getValue(ctx);
729-
System.out.println(value);
730729
// value is list containing [true,false]
731730
Assert.assertEquals(Boolean.class,value.get(0).getClass());
732731
TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);

0 commit comments

Comments
 (0)