Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 94284 invoked from network); 19 Jul 2007 06:40:18 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 19 Jul 2007 06:40:18 -0000 Received: (qmail 33508 invoked by uid 500); 19 Jul 2007 06:39:59 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 33496 invoked by uid 500); 19 Jul 2007 06:39:59 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 33487 invoked by uid 99); 19 Jul 2007 06:39:59 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Jul 2007 23:39:59 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Jul 2007 23:39:56 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 5D5891A981A; Wed, 18 Jul 2007 23:39:36 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r557505 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/Encoder.java main/java/java/beans/UtilMapPersistenceDelegate.java test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java Date: Thu, 19 Jul 2007 06:39:35 -0000 To: commits@harmony.apache.org From: leoli@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070719063936.5D5891A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: leoli Date: Wed Jul 18 23:39:34 2007 New Revision: 557505 URL: http://svn.apache.org/viewvc?view=rev&rev=557505 Log: Apply patch for HARMONY-4487([classlib][beans] Persistence delegate for j.u.Map is missing). Added: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilMapPersistenceDelegate.java (with props) Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java?view=diff&rev=557505&r1=557504&r2=557505 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java (original) +++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java Wed Jul 18 23:39:34 2007 @@ -23,6 +23,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Collection; +import java.util.Map; import java.util.Hashtable; import org.apache.harmony.beans.*; @@ -190,6 +191,10 @@ if (Collection.class.isAssignableFrom(type)) { return new UtilCollectionPersistenceDelegate(); + } + + if (Map.class.isAssignableFrom(type)) { + return new UtilMapPersistenceDelegate(); } if (type.isArray()) { Added: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilMapPersistenceDelegate.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilMapPersistenceDelegate.java?view=auto&rev=557505 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilMapPersistenceDelegate.java (added) +++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilMapPersistenceDelegate.java Wed Jul 18 23:39:34 2007 @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package java.beans; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +class UtilMapPersistenceDelegate extends DefaultPersistenceDelegate { + @Override + @SuppressWarnings({ "unchecked", "nls" }) + protected void initialize(Class type, Object oldInstance, + Object newInstance, Encoder enc) { + // Call the initialization of the super type + super.initialize(type, oldInstance, newInstance, enc); + + Map map = (Map) oldInstance; + Set keySet = map.keySet(); + for (Iterator i = keySet.iterator(); i.hasNext();) { + Object key = i.next(); + Expression getterExp = new Expression(oldInstance, "get", + new Object[] { key}); + try { + // Calculate the old value of the property + Object oldVal = getterExp.getValue(); + // Write the getter expression to the encoder + enc.writeExpression(getterExp); + // Get the target value that exists in the new environment + Object targetVal = enc.get(oldVal); + // Get the current property value in the new environment + Object newVal = null; + try { + newVal = new Expression(newInstance, "get", new Object[] { key }).getValue(); + } catch (ArrayIndexOutOfBoundsException ex) { + // The newInstance has no elements, so current property + // value remains null + } + /* + * Make the target value and current property value equivalent + * in the new environment + */ + if (null == targetVal) { + if (null != newVal) { + // Set to null + Statement setterStm = new Statement(oldInstance, "put", + new Object[] { key, null }); + enc.writeStatement(setterStm); + } + } else { + PersistenceDelegate pd = enc + .getPersistenceDelegate(targetVal.getClass()); + if (!pd.mutatesTo(targetVal, newVal)) { + Statement setterStm = new Statement(oldInstance, "put", + new Object[] { key, oldVal }); + enc.writeStatement(setterStm); + } + } + } catch (Exception ex) { + enc.getExceptionListener().exceptionThrown(ex); + } + } + } +} Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/UtilMapPersistenceDelegate.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java?view=diff&rev=557505&r1=557504&r2=557505 ============================================================================== --- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java (original) +++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java Wed Jul 18 23:39:34 2007 @@ -40,9 +40,9 @@ import java.util.LinkedList; import java.util.Locale; import java.util.Stack; +import java.util.TreeMap; -import javax.swing.JTabbedPane; -import javax.swing.LayoutFocusTraversalPolicy; +import javax.swing.*; import junit.framework.TestCase; @@ -800,6 +800,23 @@ assertEquals(scrollPane.getAlignmentY(), aScrollPane.getAlignmentY()); assertEquals(scrollPane.getScrollbarDisplayPolicy(), aScrollPane .getScrollbarDisplayPolicy()); + } + + public void test_writeObject_java_util_Map(){ + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream( + byteArrayOutputStream)); + TreeMap map = new TreeMap(); + map.put(new Integer(10), "first element"); + + encoder.writeObject(map); + encoder.close(); + DataInputStream stream = new DataInputStream(new ByteArrayInputStream( + byteArrayOutputStream.toByteArray())); + XMLDecoder decoder = new XMLDecoder(stream); + TreeMap aMap = (TreeMap) decoder.readObject(); + assertEquals(map.size(), aMap.size()); + assertEquals(map.get(10), aMap.get(10)); } // <--