You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by le...@apache.org on 2007/07/19 08:39:35 UTC
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
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<Integer, String> map = new TreeMap<Integer, String>();
+ 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));
}
// <--