From 4aa8b96687add68542acab8a2b5af63fe4691576 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 8 Feb 2012 17:51:06 +0100 Subject: [PATCH] write method parameter type preferred over read method parameter type for property conversion (fixing regression; SPR-8964) --- .../beans/BeanWrapperEnumTests.java | 15 +++++++++++++-- .../src/test/java/test/beans/GenericBean.java | 14 +++++++++++++- .../springframework/core/convert/Property.java | 8 +++----- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java index d41aa7a215e..3946cccb25e 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 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. @@ -16,11 +16,12 @@ package org.springframework.beans; -import static org.junit.Assert.*; import org.junit.Test; import test.beans.CustomEnum; import test.beans.GenericBean; +import static org.junit.Assert.*; + /** * @author Juergen Hoeller * @author Chris Beams @@ -109,4 +110,14 @@ public final class BeanWrapperEnumTests { assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2)); } + @Test + public void testCustomEnumSetWithGetterSetterMismatch() { + GenericBean gb = new GenericBean(); + BeanWrapper bw = new BeanWrapperImpl(gb); + bw.setPropertyValue("customEnumSetMismatch", new String[] {"VALUE_1", "VALUE_2"}); + assertEquals(2, gb.getCustomEnumSet().size()); + assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1)); + assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2)); + } + } diff --git a/org.springframework.beans/src/test/java/test/beans/GenericBean.java b/org.springframework.beans/src/test/java/test/beans/GenericBean.java index acb9bdb76e5..12ff808dc84 100644 --- a/org.springframework.beans/src/test/java/test/beans/GenericBean.java +++ b/org.springframework.beans/src/test/java/test/beans/GenericBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 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. @@ -21,6 +21,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -244,6 +245,17 @@ public class GenericBean { this.customEnumSet = customEnumSet; } + public Set getCustomEnumSetMismatch() { + return customEnumSet; + } + + public void setCustomEnumSetMismatch(Set customEnumSet) { + this.customEnumSet = new HashSet(customEnumSet.size()); + for (Iterator iterator = customEnumSet.iterator(); iterator.hasNext(); ) { + this.customEnumSet.add(CustomEnum.valueOf(iterator.next())); + } + } + public static GenericBean createInstance(Set integerSet) { return new GenericBean(integerSet); } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/Property.java b/org.springframework.core/src/main/java/org/springframework/core/convert/Property.java index 7cdf0bff801..b1e0d9c6129 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/Property.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/Property.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.core.convert; import java.lang.annotation.Annotation; @@ -144,10 +145,7 @@ public final class Property { if (read == null && write == null) { throw new IllegalStateException("Property is neither readable nor writeable"); } - if (read != null && write != null && !write.getParameterType().isAssignableFrom(read.getParameterType())) { - throw new IllegalStateException("Write parameter is not assignable from read parameter"); - } - return read != null ? read : write; + return (write != null ? write : read); } private MethodParameter resolveReadMethodParameter() {