diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperGenericsTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperGenericsTests.java index 3a9e03accc..26c6cfea1c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperGenericsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperGenericsTests.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 { + private final D data; + public Holder(D data) { + this.data = data; + } + public D getData() { + return this.data; + } + } + + Map data = new HashMap(); + data.put("x", "y"); + Holder> context = new Holder>(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 { diff --git a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java index 02f07be0be..89b7a4a634 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java +++ b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java @@ -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); } diff --git a/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java b/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java index 3074d77617..ab750a44ca 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java +++ b/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. @@ -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> nestedMapField = new HashMap>(); + @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)); } + }