You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2019/11/02 22:24:49 UTC
[juneau] branch master updated: JUNEAU-160 Setting bean properties
of type list don't use setters.
This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 7c6b059 JUNEAU-160 Setting bean properties of type list don't use setters.
7c6b059 is described below
commit 7c6b059a66fc1513e9ad78683e37dde4e7f8febd
Author: JamesBognar <ja...@apache.org>
AuthorDate: Sat Nov 2 18:24:33 2019 -0400
JUNEAU-160 Setting bean properties of type list don't use setters.
---
.../test/java/org/apache/juneau/BeanMapTest.java | 37 ++++++++++++++++++----
.../java/org/apache/juneau/BeanPropertyMeta.java | 7 ++--
2 files changed, 35 insertions(+), 9 deletions(-)
diff --git a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/BeanMapTest.java b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/BeanMapTest.java
index 3b5c789..84b90d8 100755
--- a/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/BeanMapTest.java
+++ b/juneau-core/juneau-core-utest/src/test/java/org/apache/juneau/BeanMapTest.java
@@ -343,7 +343,7 @@ public class BeanMapTest {
// The rest are proper superclasses of ObjectList.
assertEquals(ObjectList.class.getName(), m.get("l1").getClass().getName());
- assertEquals(LinkedList.class.getName(), m.get("ll1").getClass().getName());
+ assertEquals(ObjectList.class.getName(), m.get("ll1").getClass().getName());
assertEquals(ObjectList.class.getName(), m.get("c1").getClass().getName());
assertEquals(ObjectList.class.getName(), m.get("jl1").getClass().getName());
@@ -358,7 +358,7 @@ public class BeanMapTest {
// The rest are propert superclasses of ObjectMap
assertEquals(ObjectMap.class.getName(), m.get("m1").getClass().getName());
- assertEquals(HashMap.class.getName(), m.get("hm1").getClass().getName());
+ assertEquals(ObjectMap.class.getName(), m.get("hm1").getClass().getName());
assertEquals(ObjectMap.class.getName(), m.get("jm1").getClass().getName());
// Initialized fields should reuse existing field value.
@@ -372,12 +372,12 @@ public class BeanMapTest {
m.put("jm2", new ObjectMap("{foo:'bar'}"));
m.put("jl2", new ObjectList("[1,2,3]"));
- assertEquals(ArrayList.class.getName(), m.get("l2").getClass().getName());
+ assertEquals(ObjectList.class.getName(), m.get("l2").getClass().getName());
assertEquals(ArrayList.class.getName(), m.get("al2").getClass().getName());
- assertEquals(LinkedList.class.getName(), m.get("ll2").getClass().getName());
- assertEquals(ArrayList.class.getName(), m.get("c2").getClass().getName());
- assertEquals(HashMap.class.getName(), m.get("m2").getClass().getName());
- assertEquals(HashMap.class.getName(), m.get("hm2").getClass().getName());
+ assertEquals(ObjectList.class.getName(), m.get("ll2").getClass().getName());
+ assertEquals(ObjectList.class.getName(), m.get("c2").getClass().getName());
+ assertEquals(ObjectMap.class.getName(), m.get("m2").getClass().getName());
+ assertEquals(ObjectMap.class.getName(), m.get("hm2").getClass().getName());
assertEquals(TreeMap.class.getName(), m.get("tm2").getClass().getName());
assertEquals(ObjectMap.class.getName(), m.get("jm2").getClass().getName());
assertEquals(ObjectList.class.getName(), m.get("jl2").getClass().getName());
@@ -1932,4 +1932,27 @@ public class BeanMapTest {
public static class Z {
public String a, b, c;
}
+
+ //====================================================================================================
+ // testCollectionSetters_preferSetter
+ //====================================================================================================
+ @Test
+ public void testCollectionSetters_preferSetter() throws Exception {
+ AA aa = new AA();
+ BeanMap<AA> bm = BeanContext.DEFAULT.createSession().toBeanMap(aa);
+
+ bm.put("a", AList.create("x"));
+ assertObjectEquals("['x']", aa.a);
+ }
+
+ public static class AA {
+ private List<String> a = new ArrayList<>();
+
+ public List<String> getA() {
+ return Collections.emptyList();
+ }
+ public void setA(List<String> a) {
+ this.a = a;
+ }
+ }
}
\ No newline at end of file
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
index 99cfd3d..eb8c047 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanPropertyMeta.java
@@ -661,13 +661,16 @@ public final class BeanPropertyMeta {
Object r = (beanContext.isBeanMapPutReturnsOldValue() || isMap || isCollection) && (getter != null || field != null) ? get(m, pName) : null;
Class<?> propertyClass = rawTypeMeta.getInnerClass();
+ ClassInfo pcInfo = rawTypeMeta.getInfo();
if (value == null && (isMap || isCollection)) {
invokeSetter(bean, pName, null);
return r;
}
- if (isMap) {
+ Class<?> vc = value == null ? null : value.getClass();
+
+ if (isMap && (setter == null || ! pcInfo.isParentOf(vc))) {
if (! (value instanceof Map)) {
if (value instanceof CharSequence)
@@ -724,7 +727,7 @@ public final class BeanPropertyMeta {
if (setter != null || field != null)
invokeSetter(bean, pName, propMap);
- } else if (isCollection) {
+ } else if (isCollection && (setter == null || ! pcInfo.isParentOf(vc))) {
if (! (value instanceof Collection)) {
if (value instanceof CharSequence)