Return-Path: Delivered-To: apmail-ibatis-user-java-archive@www.apache.org Received: (qmail 51309 invoked from network); 31 Aug 2006 18:34:42 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 31 Aug 2006 18:34:42 -0000 Received: (qmail 29942 invoked by uid 500); 31 Aug 2006 18:34:33 -0000 Delivered-To: apmail-ibatis-user-java-archive@ibatis.apache.org Received: (qmail 29932 invoked by uid 500); 31 Aug 2006 18:34:33 -0000 Mailing-List: contact user-java-help@ibatis.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user-java@ibatis.apache.org Delivered-To: mailing list user-java@ibatis.apache.org Received: (qmail 29911 invoked by uid 99); 31 Aug 2006 18:34:33 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 31 Aug 2006 11:34:32 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: unknown (asf.osuosl.org: error in processing during lookup of Adam.Klein@aqr.com) Received: from [65.223.234.92] (HELO SG0PIMX02.AQRCAPITAL.COM) (65.223.234.92) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 31 Aug 2006 11:34:31 -0700 Received: from sg0pexf02.aqrcapital.com ([10.30.4.183]) by SG0PIMX02.AQRCAPITAL.COM with ESMTP; 31 Aug 2006 14:34:07 -0400 X-IronPort-AV: i="4.08,195,1154923200"; d="scan'208"; a="1201438:sNHT93130988" Received: from MG0PEXC01.aqrcapital.com ([10.30.4.170]) by sg0pexf02.aqrcapital.com with Microsoft SMTPSVC(6.0.3790.1830); Thu, 31 Aug 2006 14:34:07 -0400 X-MimeOLE: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Subject: RE: bug in javabeans setter (ComplexBeanProbe.java)? Date: Thu, 31 Aug 2006 14:34:07 -0400 Message-ID: <8A0052EB3E72CF45B236A55DA0E3B4371A8BE8@MG0PEXC01.aqrcapital.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: bug in javabeans setter (ComplexBeanProbe.java)? Thread-Index: AcbLl6ato7P/B0PvRfW0lWqNyPRPKwAEu0bgACzgvoAAKIo18AAJX8EQAACqUWA= From: "Adam Klein" To: X-OriginalArrivalTime: 31 Aug 2006 18:34:07.0903 (UTC) FILETIME=[0B5542F0:01C6CD2C] X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N I don't think solves it. Check out the setObject(...) function in ComplexBeanProbe.java, listed below, followed by my analysis. =20 public void setObject(Object object, String name, Object value) { (1) if (name.indexOf('.') > -1) { StringTokenizer parser =3D new StringTokenizer(name, "."); (2) String property =3D parser.nextToken(); (3) Object child =3D object; (4) while (parser.hasMoreTokens()) { (5) Class type =3D getPropertyTypeForSetter(child, property); (6) Object parent =3D child; (7) child =3D getProperty(parent, property); (8) if (child =3D=3D null) { (9) if (value =3D=3D null) { return; // don't instantiate child path if value is null } else { try { (10) child =3D type.newInstance(); (11) setObject(parent, property, child); } catch (Exception e) { ... } } } property =3D parser.nextToken(); } (13) setProperty(child, property, value); } else { setProperty(object, name, value); } } Here is what happens. Ibatis calls: setObject(myClass, "myHashMap.key1", "value1") with myClass =3D new MyClass(), myHashMap =3D=3D null, and then: 1. true condition, "myHashMap.key1" has "." in it 2. property =3D "myHashMap"=09 3. child =3D myClass 4. true condition, parser has more tokens 5. type =3D "java.util.HashMap" 6. parent =3D myClass 7. child =3D myHashMap 8. true condition, child (myHashMap) IS null 9. false condition, value =3D=3D "value1", not null 10. child =3D new HashMap() 11. setObject(parent, property, child), ie, myClass.myHashMap =3D COPY(child) 12. ... 13. setProperty(child, property, value), ie, child.key1 =3D "value1" Note that child !=3D myHashMap at step 13 due to the setter's deep copying. Shouldn't it call setProperty(child, property, value) BEFORE step 11? Best, Adam -----Original Message----- From: Poitras Christian [mailto:Christian.Poitras@ircm.qc.ca]=20 Sent: Thursday, August 31, 2006 1:58 PM To: user-java@ibatis.apache.org Subject: RE: bug in javabeans setter (ComplexBeanProbe.java)? Ok. I have an answer for this type of problem. For your resultMap, iBatis will generate objects by calling the following. MyClass myClass =3D new MyClass(); <- iBatis calls instantiate but the result is the same. myClass.setMyDouble(value0); myClass.setMyHashMap(new HashMap()); <- I think this is done. myClass.getMyHashMap().setKey1(value1); myClass.getMyHashMap().setKey2(value2); To get pass your problem, you should change the setter with this. public void setMyHashMap(HashMap h) { if (myHashMap =3D=3D null) { myHashMap =3D new HashMap(h); } // create COPY of h if (h =3D=3D null) myHashMap =3D null; else { myHashMap.clear(); myHashMap.putAll(h); } }=09 -----Original Message----- From: Adam Klein [mailto:Adam.Klein@aqr.com]=20 Sent: Thursday, 31 August 2006 09:32 To: user-java@ibatis.apache.org Subject: RE: bug in javabeans setter (ComplexBeanProbe.java)? My class looks like this: Public class MyClass { HashMap myHashMap; ... =20 MyClass() { // Uncomment to fix bad iBATIS behavior: // myHashMap =3D new HashMap(); } public void setMyHashMap(HashMap h) { // create COPY of h if (h =3D=3D null) myHashMap =3D null; else if (myHashMap =3D=3D null) myHashMap =3D new HashMap(h); else { myHashMap.clear(); myHashMap.putAll(h); } }=09 Public HashMap getMyHashMap() { return myHashMap; } } Now, if I use the result map below to generate a List of MyClass objects, if myHashMap is not initialized to a non-null value in the constructor (see commented-out line above), the value Key1 is not assigned value1, because what happens is the code creates a new HashMap object, say X, and before calling X.put(Key1, Value1), the code calls setMyHashMap(X), and THEN it calls X.put(Key1, value1), but by this time X no longer refers to the same object as myHashMap. Hope this makes sense. Regards, Adam -----Original Message----- From: Poitras Christian [mailto:Christian.Poitras@ircm.qc.ca] Sent: Wednesday, August 30, 2006 2:01 PM To: user-java@ibatis.apache.org Subject: RE: bug in javabeans setter (ComplexBeanProbe.java)? Can you post a copy of your setters? I'm not sure what you're trying to ask or to do. Christian -----Original Message----- From: Adam Klein [mailto:Adam.Klein@aqr.com] Sent: Tuesday, 29 August 2006 16:41 To: user-java@ibatis.apache.org Subject: bug in javabeans setter (ComplexBeanProbe.java)? Apologies if this is posted twice, I don't think my first one made it. I had implemented defensive copying on my javabean setter methods - ie, so the value of the bean would be set to a COPY of the parameter - while the getter would return a direct reference. Then, when I was trying to set keys in an (uninitiailized) HashMap object to values like this: I got a very strange result: of the values in myHashMap, only the value of myHashMap.key2 would be set; in general, the first key of an (uninitialized) HashMap would never be assigned a value. I traced the reason to the ComplexBeanProbe.java file, in the setObject method: it called the method setObject(parent, property, child) prior to calling setProperty(child, property, value), which meant that the parent.child object no longer referenced the child object by the time the code attempted to set values on the child object (due specifically to my defensive copying). If myHashMap is initialized to an empty HashMap value on creation of MyClass, there are no problems. The question is, is this just a symptom of defensive copying, or is this a bug, where setProperty(child, property, value) should be called first? Regards, Adam =20 =20 =20 =20 =20 Disclaimer: This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please notify the sender immediately and destroy/delete this e-mail. You are hereby notified that any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly prohibited. =20 This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All information contained in this communication is not warranted as to completeness or accuracy and is subject to change without notice. Any comments or statements made in this communication do not necessarily reflect those of AQR Capital Management, LLC and its affiliates.