Browse Source

write method parameter type preferred over read method parameter type for property conversion (fixing regression; SPR-8964)

pull/32/merge
Juergen Hoeller 14 years ago
parent
commit
4aa8b96687
  1. 15
      org.springframework.beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java
  2. 14
      org.springframework.beans/src/test/java/test/beans/GenericBean.java
  3. 8
      org.springframework.core/src/main/java/org/springframework/core/convert/Property.java

15
org.springframework.beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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 { @@ -109,4 +110,14 @@ public final class BeanWrapperEnumTests {
assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2));
}
@Test
public void testCustomEnumSetWithGetterSetterMismatch() {
GenericBean<?> gb = new GenericBean<Object>();
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));
}
}

14
org.springframework.beans/src/test/java/test/beans/GenericBean.java

@ -1,5 +1,5 @@ @@ -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; @@ -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<T> { @@ -244,6 +245,17 @@ public class GenericBean<T> {
this.customEnumSet = customEnumSet;
}
public Set<CustomEnum> getCustomEnumSetMismatch() {
return customEnumSet;
}
public void setCustomEnumSetMismatch(Set<String> customEnumSet) {
this.customEnumSet = new HashSet<CustomEnum>(customEnumSet.size());
for (Iterator<String> iterator = customEnumSet.iterator(); iterator.hasNext(); ) {
this.customEnumSet.add(CustomEnum.valueOf(iterator.next()));
}
}
public static GenericBean createInstance(Set<Integer> integerSet) {
return new GenericBean(integerSet);
}

8
org.springframework.core/src/main/java/org/springframework/core/convert/Property.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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 { @@ -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() {

Loading…
Cancel
Save