TypeDescriptor's nested type traversal leniently returns null in case of unresolvable nested type

Issue: SPR-11898
master
Juergen Hoeller 10 years ago
parent a6f3f101e4
commit d6635802c4
  1. 28
      spring-beans/src/test/java/org/springframework/beans/BeanWrapperGenericsTests.java
  2. 3
      spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
  3. 14
      spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@ -28,7 +28,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
@ -40,6 +39,8 @@ import org.springframework.tests.sample.beans.GenericIntegerBean;
import org.springframework.tests.sample.beans.GenericSetOfIntegerBean;
import org.springframework.tests.sample.beans.TestBean;
import static org.junit.Assert.*;
/**
* @author Juergen Hoeller
* @author Chris Beams
@ -485,6 +486,29 @@ public class BeanWrapperGenericsTests {
assertEquals(new Long(10), bean.getId());
}
@Test
public void testUntypedPropertyWithMapAtRuntime() {
class Holder<D> {
private final D data;
public Holder(D data) {
this.data = data;
}
public D getData() {
return this.data;
}
}
Map<String, Object> data = new HashMap<String, Object>();
data.put("x", "y");
Holder<Map<String, Object>> context = new Holder<Map<String,Object>>(data);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(context);
assertEquals("y", bw.getPropertyValue("data['x']"));
bw.setPropertyValue("data['message']", "it works!");
assertEquals(data.get("message"), "it works!");
}
private static abstract class BaseGenericCollectionBean {

@ -701,8 +701,7 @@ public class TypeDescriptor implements Serializable {
}
}
if (nested == ResolvableType.NONE) {
throw new IllegalStateException(
"Unable to obtain nested generic from " + typeDescriptor + " at level " + nestingLevel);
return null;
}
return getRelatedIfResolvable(typeDescriptor, nested);
}

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@ -36,6 +36,7 @@ import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@ -69,6 +70,7 @@ public class TypeDescriptorTests {
public Map<String, List<Integer>> nestedMapField = new HashMap<String, List<Integer>>();
@Test
public void parameterPrimitive() throws Exception {
TypeDescriptor desc = new TypeDescriptor(new MethodParameter(getClass().getMethod("testParameterPrimitive", int.class), 0));
@ -555,15 +557,16 @@ public class TypeDescriptorTests {
TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test4", List.class), 0, 2), 2);
}
@Test(expected=IllegalStateException.class)
@Test
public void nestedTooManyLevels() throws Exception {
TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test4", List.class), 0), 3);
assertEquals(String.class, t1.getType());
assertNull(t1);
}
@Test(expected=IllegalStateException.class)
@Test
public void nestedMethodParameterTypeNotNestable() throws Exception {
TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test5", String.class), 0), 2);
TypeDescriptor t1 = TypeDescriptor.nested(new MethodParameter(getClass().getMethod("test5", String.class), 0), 2);
assertNull(t1);
}
@Test(expected=IllegalArgumentException.class)
@ -941,4 +944,5 @@ public class TypeDescriptorTests {
assertThat(new TypeDescriptor(methodParameter).getSource(), equalTo((Object) methodParameter));
assertThat(TypeDescriptor.valueOf(Integer.class).getSource(), equalTo((Object) Integer.class));
}
}

Loading…
Cancel
Save