You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by to...@apache.org on 2007/07/18 08:36:04 UTC
svn commit: r557172 - in /harmony/enhanced/classlib/trunk/modules/beans/src:
main/java/java/beans/ test/java/org/apache/harmony/beans/tests/java/beans/
test/resources/xml/
Author: tonywu
Date: Tue Jul 17 23:36:03 2007
New Revision: 557172
URL: http://svn.apache.org/viewvc?view=rev&rev=557172
Log:
Appliy Harmony-4473 ([classlib][beans] Current bean implementation does not persist awt.Choice properly
) with minor modification
Added:
harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtChoicePersistenceDelegate.java (with props)
harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtColorPersistenceDelegate.java (with props)
harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtRectanglePersistenceDelegate.java (with props)
harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/ReferenceMap.java (with props)
harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Choice.xml (with props)
Modified:
harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/DefaultPersistenceDelegate.java
harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Encoder.java
harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java
harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/DefaultPersistenceDelegateTest.java
harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PersistenceDelegateTest.java
Added: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtChoicePersistenceDelegate.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtChoicePersistenceDelegate.java?view=auto&rev=557172
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtChoicePersistenceDelegate.java (added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtChoicePersistenceDelegate.java Tue Jul 17 23:36:03 2007
@@ -0,0 +1,76 @@
+/*
+ * 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.awt.Choice;
+
+class AwtChoicePersistenceDelegate extends DefaultPersistenceDelegate {
+ protected void initialize(Class<?> type, Object oldInstance,
+ Object newInstance, Encoder enc) {
+ super.initialize(type, oldInstance, newInstance, enc);
+ if (type != oldInstance.getClass()) {
+ return;
+ }
+
+ Choice choice = (Choice) oldInstance;
+ int count = choice.getItemCount();
+ Expression getterExp = null;
+ for (int i = 0; i < count; i++) {
+ getterExp = new Expression(choice, "getItem", new Object[] { i });
+ 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, "getItem",
+ new Object[] { i }).getValue();
+ } catch (IndexOutOfBoundsException 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, "add",
+ new Object[] { null });
+ enc.writeStatement(setterStm);
+ }
+ } else {
+ PersistenceDelegate pd = enc
+ .getPersistenceDelegate(targetVal.getClass());
+ if (!pd.mutatesTo(targetVal, newVal)) {
+ Statement setterStm = new Statement(oldInstance, "add",
+ new Object[] { oldVal });
+ enc.writeStatement(setterStm);
+ }
+ }
+ } catch (Exception ex) {
+ enc.getExceptionListener().exceptionThrown(ex);
+ }
+ }
+ }
+}
Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtChoicePersistenceDelegate.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtColorPersistenceDelegate.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtColorPersistenceDelegate.java?view=auto&rev=557172
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtColorPersistenceDelegate.java (added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtColorPersistenceDelegate.java Tue Jul 17 23:36:03 2007
@@ -0,0 +1,29 @@
+/*
+ * 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.awt.Color;
+
+class AwtColorPersistenceDelegate extends DefaultPersistenceDelegate {
+ protected Expression instantiate(Object oldInstance, Encoder enc) {
+ Color color = (Color) oldInstance;
+ return new Expression(oldInstance, oldInstance.getClass(),
+ Statement.CONSTRUCTOR_NAME, new Object[] { color.getRed(),
+ color.getGreen(), color.getBlue(), color.getAlpha() });
+ }
+}
Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtColorPersistenceDelegate.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtRectanglePersistenceDelegate.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtRectanglePersistenceDelegate.java?view=auto&rev=557172
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtRectanglePersistenceDelegate.java (added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtRectanglePersistenceDelegate.java Tue Jul 17 23:36:03 2007
@@ -0,0 +1,39 @@
+/*
+ * 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.awt.Rectangle;
+
+class AwtRectanglePersistenceDelegate extends DefaultPersistenceDelegate {
+
+ protected boolean mutatesTo(Object o1, Object o2) {
+ return o1.equals(o2);
+ }
+
+ protected void initialize(Class<?> type, Object oldInstance,
+ Object newInstance, Encoder enc) {
+ return;
+ }
+
+ protected Expression instantiate(Object oldInstance, Encoder enc) {
+ Rectangle rect = (Rectangle) oldInstance;
+
+ return new Expression(rect, rect.getClass(), Statement.CONSTRUCTOR_NAME,
+ new Object[] { rect.x, rect.y, rect.width, rect.height });
+ }
+}
Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/AwtRectanglePersistenceDelegate.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/DefaultPersistenceDelegate.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/DefaultPersistenceDelegate.java?view=diff&rev=557172&r1=557171&r2=557172
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/DefaultPersistenceDelegate.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/DefaultPersistenceDelegate.java Tue Jul 17 23:36:03 2007
@@ -17,220 +17,299 @@
package java.beans;
+import java.lang.reflect.Field;
import java.lang.reflect.Method;
-
-import org.apache.harmony.beans.internal.nls.Messages;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.HashMap;
+
+/**
+ * Default PersistenceDelegate for normal classes. The instances of this class
+ * are used when other customized PersistenceDelegate is not set in the encoders
+ * for a particular type.
+ * <p>
+ * This PersistenceDelegate assumes that the bean to be made persistent has a
+ * default constructor that takes no parameters or a constructor that takes some
+ * properties as its parameters. Only the properties that can be got or set
+ * based on the knowledge gained through an introspection will be made
+ * persistent. In the case that a bean is constructed with some properties, the
+ * value of these properties should be available via the conventional getter
+ * method.
+ * </p>
+ *
+ * @see Encoder
+ */
public class DefaultPersistenceDelegate extends PersistenceDelegate {
- private String[] constructorPropertyNames;
+ // shared empty property name array
+ private static String[] EMPTY_PROPERTIES = new String[0];
- public DefaultPersistenceDelegate(String[] constructorPropertyNames) {
- String[] arr = null;
-
- // convert first letters of property names to lower case
- if (constructorPropertyNames != null) {
- arr = new String[constructorPropertyNames.length];
- for (int i = 0; i < constructorPropertyNames.length; i++) {
- if (constructorPropertyNames[i] != null
- && constructorPropertyNames[i].length() > 0) {
- arr[i] = constructorPropertyNames[i].substring(0, 1)
- .toLowerCase()
- + constructorPropertyNames[i].substring(1);
- } else {
- arr[i] = constructorPropertyNames[i];
- }
- }
- }
-
- this.constructorPropertyNames = arr;
- }
+ // names of the properties accepted by the bean's constructor
+ private String[] propertyNames = EMPTY_PROPERTIES;
+ /**
+ * Constructs a <code>DefaultPersistenceDelegate</code> instance that
+ * supports the persistence of a bean which has a default constructor.
+ *
+ */
public DefaultPersistenceDelegate() {
- this.constructorPropertyNames = null;
+ // empty
}
- @Override
- protected void initialize(Class<?> type, Object oldInstance,
- Object newInstance, Encoder out) {
-
- // added for compatibility with RI
- if (out == null) {
- throw new NullPointerException(Messages.getString("beans.4C")); //$NON-NLS-1$
+ /**
+ * Constructs a <code>DefaultPersistenceDelegate</code> instance that
+ * supports the persistence of a bean which is constructed with some
+ * properties.
+ *
+ * @param propertyNames
+ * the name of the properties that are taken as parameters by the
+ * bean's constructor
+ */
+ public DefaultPersistenceDelegate(String[] propertyNames) {
+ if (null != propertyNames) {
+ this.propertyNames = propertyNames;
}
+ }
- // added for compatibility with RI
- if (newInstance == null) {
- out.getExceptionListener().exceptionThrown(
- new NullPointerException(Messages.getString("beans.4A"))); //$NON-NLS-1$
+ /**
+ * Initializes the new instance in the new environment so that it becomes
+ * equivalent with the old one, meanwhile recording this process in the
+ * encoder.
+ * <p>
+ * This is done by inspecting each property of the bean. The property value
+ * from the old bean instance and the value from the new bean instance are
+ * both retrieved and examined to see whether the latter mutates to the
+ * former, and if not, issue a call to the write method to set the
+ * equivalent value for the new instance. Exceptions occured during this
+ * process are reported to the exception listener of the encoder.
+ * </p>
+ *
+ * @param type
+ * the type of the bean
+ * @param oldInstance
+ * the original bean object to be recorded
+ * @param newInstance
+ * the simmulating new bean object to be initialized
+ * @param enc
+ * the encoder to write the outputs to
+ */
+ protected void initialize(Class<?> type, Object oldInstance,
+ Object newInstance, Encoder enc) {
+ // Call the initialization of the super type
+ super.initialize(type, oldInstance, newInstance, enc);
+ // Continue only if initializing the "current" type
+ if (type != oldInstance.getClass()) {
return;
}
- // added for compatibility with RI
- if (oldInstance == null) {
- throw new NullPointerException(Messages.getString("beans.4C")); //$NON-NLS-1$
- }
-
+ // Get all bean properties
BeanInfo info = null;
try {
info = Introspector.getBeanInfo(type);
- } catch (IntrospectionException e) {
- out.getExceptionListener().exceptionThrown(e);
+ } catch (IntrospectionException ex) {
+ enc.getExceptionListener().exceptionThrown(ex);
return;
}
PropertyDescriptor[] pds = info.getPropertyDescriptors();
- for (PropertyDescriptor pd : pds) {
- if (!isTransient(pd)) {
- Method getter = pd.getReadMethod();
-
- if (getter != null) {
- Method setter = pd.getWriteMethod();
-
- if (setter != null) {
- Expression getterExp = new Expression(oldInstance, pd
- .getReadMethod().getName(), null);
- try {
- // Calculate the old value of the property
- Object oldValue = getterExp.getValue();
-
- // Write the getter expression to the encoder
- out.writeExpression(getterExp);
-
- // Get the target value that exists in the new
- // environment
- Object targetVal = out.get(oldValue);
-
- Object newValue = new Expression(newInstance, pd
- .getReadMethod().getName(), null)
- .getValue();
-
- /*
- * Make the target value and current property value
- * equivalent in the new environment
- */
- if (null == targetVal) {
- if (null != newValue) {
- // Set to null
- Statement setterStm = new Statement(
- oldInstance, pd.getWriteMethod()
- .getName(),
- new Object[] { null });
- out.writeStatement(setterStm);
- }
- } else {
- PersistenceDelegate delegate = out
- .getPersistenceDelegate(targetVal
- .getClass());
- if (!delegate.mutatesTo(targetVal, newValue)) {
- Statement setterStm = new Statement(
- oldInstance, pd.getWriteMethod()
- .getName(),
- new Object[] { oldValue });
- out.writeStatement(setterStm);
- }
- }
- } catch (Exception ex) {
- out.getExceptionListener().exceptionThrown(ex);
- }
- } else {
- // commented since the process should be
- // continued even if no setter is found
- // throw new Exception("no setter for " +
- // pd.getName() + " property.");
- continue;
+ // Initialize each found non-transient property
+ for (int i = 0; i < pds.length; i++) {
+ // Skip a property whose transient attribute is true
+ if (Boolean.TRUE.equals(pds[i].getValue("transient"))) { //$NON-NLS-1$
+ continue;
+ }
+ // Skip a property having no setter or getter
+ if (null == pds[i].getWriteMethod()
+ || null == pds[i].getReadMethod()) {
+ continue;
+ }
+
+ // Get the value of the property in the old instance
+ Expression getterExp = new Expression(oldInstance, pds[i]
+ .getReadMethod().getName(), null);
+ 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 = new Expression(newInstance, pds[i]
+ .getReadMethod().getName(), null).getValue();
+ /*
+ * 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, pds[i]
+ .getWriteMethod().getName(),
+ new Object[] { null });
+ enc.writeStatement(setterStm);
+ }
+ } else {
+ PersistenceDelegate pd = enc
+ .getPersistenceDelegate(targetVal.getClass());
+ if (!pd.mutatesTo(targetVal, newVal)) {
+ Statement setterStm = new Statement(oldInstance, pds[i]
+ .getWriteMethod().getName(),
+ new Object[] { oldVal });
+ enc.writeStatement(setterStm);
}
}
+ } catch (Exception ex) {
+ enc.getExceptionListener().exceptionThrown(ex);
}
}
}
- @Override
- protected Expression instantiate(Object oldInstance, Encoder out) {
- Object[] args = null;
-
- if (constructorPropertyNames == null
- || constructorPropertyNames.length == 0) {
- args = new Object[] {};
- } else {
- args = new Object[constructorPropertyNames.length];
-
- try {
- PropertyDescriptor[] pds = Introspector.getBeanInfo(
- oldInstance.getClass()).getPropertyDescriptors();
-
- for (int i = 0; i < constructorPropertyNames.length; ++i) {
-
- boolean found = false;
+ /*
+ * Get the field value of an object using privileged code.
+ */
+ private Object getFieldValue(Object oldInstance, String fieldName)
+ throws NoSuchFieldException, IllegalAccessException {
+ Class c = oldInstance.getClass();
+ final Field f = c.getDeclaredField(fieldName);
+ AccessController.doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ f.setAccessible(true);
+ return null;
+ }
+ });
+ return f.get(oldInstance);
+ }
- for (PropertyDescriptor element : pds) {
+ /*
+ * Get the value for the specified property of the given bean instance.
+ */
+ private Object getPropertyValue(HashMap proDscMap, Object oldInstance,
+ String propName) throws Exception {
+ // Try to get the read method for the property
+ Method getter = null;
+ if (null != proDscMap) {
+ PropertyDescriptor pd = (PropertyDescriptor) proDscMap
+ .get(Introspector.decapitalize(propName));
+ if (null != pd) {
+ getter = pd.getReadMethod();
+ }
+ }
- if (constructorPropertyNames[i].equals(element
- .getName())) {
- Method getter = element.getReadMethod();
+ // Invoke read method to get the value if found
+ if (null != getter) {
+ return getter.invoke(oldInstance, (Object[])null);
+ }
- if (getter != null) {
- args[i] = getter.invoke(oldInstance,
- (Object[]) null);
- found = true;
- break;
- }
+ // Otherwise, ry to access the field directly
+ try {
+ return getFieldValue(oldInstance, propName);
+ } catch (Exception ex) {
+ // Fail, throw an exception
+ throw new NoSuchMethodException(
+ "The getter method for the property " //$NON-NLS-1$
+ + propName + " can't be found."); //$NON-NLS-1$
+ }
- throw new NoSuchMethodException(Messages.getString(
- "beans.00", element.getName())); //$NON-NLS-1$
+ }
- }
- }
+ /**
+ * Returns an expression that represents a call to the bean's constructor.
+ * The constructor may take zero or more parameters, as specified when this
+ * <code>DefaultPersistenceDelegate</code> is constructed.
+ *
+ * @param oldInstance
+ * the old instance
+ * @param enc
+ * the encoder that wants to record the old instance
+ * @return an expression for instantiating an object of the same type as the
+ * old instance
+ */
+ protected Expression instantiate(Object oldInstance, Encoder enc) {
+ Object[] args = null;
- if (found == false) {
- throw new NoSuchMethodException(Messages.getString(
- "beans.01", constructorPropertyNames[i])); //$NON-NLS-1$
- }
+ // Set the constructor arguments if any property names exist
+ if (this.propertyNames.length > 0) {
+ // Prepare the property descriptors for finding getter method later
+ BeanInfo info = null;
+ HashMap proDscMap = null;
+ try {
+ info = Introspector.getBeanInfo(oldInstance.getClass(),
+ Introspector.IGNORE_ALL_BEANINFO);
+ proDscMap = internalAsMap(info
+ .getPropertyDescriptors());
+ } catch (IntrospectionException ex) {
+ enc.getExceptionListener().exceptionThrown(ex);
+ throw new Error(ex);
+ }
+ // Get the arguments values
+ args = new Object[this.propertyNames.length];
+ for (int i = 0; i < this.propertyNames.length; i++) {
+ String propertyName = propertyNames[i];
+ if (null == propertyName || 0 == propertyName.length()) {
+ continue;
+ }
+
+ // Get the value for each property of the given instance
+ try {
+ args[i] = getPropertyValue(proDscMap, oldInstance,
+ this.propertyNames[i]);
+ } catch (Exception ex) {
+ enc.getExceptionListener().exceptionThrown(ex);
}
- } catch (RuntimeException e) {
- throw e;
- } catch (Exception e) {
- out.getExceptionListener().exceptionThrown(e);
}
-
}
- return new Expression(oldInstance, oldInstance.getClass(), "new", args); //$NON-NLS-1$
+ return new Expression(oldInstance, oldInstance.getClass(),
+ Statement.CONSTRUCTOR_NAME, args);
+ }
+
+ private static HashMap<String, PropertyDescriptor> internalAsMap(PropertyDescriptor[] propertyDescs) {
+ HashMap<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>();
+ for (int i = 0; i < propertyDescs.length; i++) {
+ map.put(propertyDescs[i].getName(), propertyDescs[i]);
+ }
+ return map;
}
- @Override
- protected boolean mutatesTo(Object oldInstance, Object newInstance) {
- if (oldInstance != null && constructorPropertyNames != null
- && constructorPropertyNames.length > 0) {
-
- // Get explicitly declared equals method.
- Method equalsMethod = null;
+ /**
+ * Determines whether one object mutates to the other object. If this
+ * <code>DefaultPersistenceDelegate</code> is constructed with one or more
+ * property names, and the class of <code>o1</code> overrides the
+ * "equals(Object)" method, then <code>o2</code> is considered to mutate
+ * to <code>o1</code> if <code>o1</code> equals to <code>o2</code>.
+ * Otherwise, the result is the same as the definition in
+ * <code>PersistenceDelegate</code>.
+ *
+ * @param o1
+ * one object
+ * @param o2
+ * the other object
+ * @return true if second object mutates to the first object, otherwise
+ * false
+ */
+ protected boolean mutatesTo(Object o1, Object o2) {
+ if (null == o1 || null == o2) {
+ return false;
+ }
+ Class c = o1.getClass();
+ if (this.propertyNames.length > 0) {
+ // Check the "equals" method has been declared
+ Method equalMethod = null;
try {
- equalsMethod = oldInstance.getClass().getDeclaredMethod(
- "equals", new Class[] { Object.class }); //$NON-NLS-1$
-
- } catch (NoSuchMethodException e) {
- // does not declare explicitly an equals method.
+ equalMethod = c.getDeclaredMethod("equals", //$NON-NLS-1$
+ new Class[] { Object.class });
+ } catch (NoSuchMethodException ex) {
+ // ignore
}
- if (equalsMethod != null) {
- Object result;
- try {
- result = equalsMethod.invoke(oldInstance,
- new Object[] { newInstance });
- } catch (Exception e) {
- // should not happen here.
- throw new Error(e);
- }
- return ((Boolean) result).booleanValue();
+ if (null != equalMethod) {
+ return o1.equals(o2);
}
}
- return super.mutatesTo(oldInstance, newInstance);
- }
- private static boolean isTransient(PropertyDescriptor pd) {
- Boolean isTransient = (Boolean) pd.getValue("transient"); //$NON-NLS-1$
- return (isTransient != null) && isTransient.equals(Boolean.TRUE);
+ return super.mutatesTo(o1, o2);
}
-}
+}
\ No newline at end of file
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=557172&r1=557171&r2=557172
==============================================================================
--- 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 Tue Jul 17 23:36:03 2007
@@ -17,12 +17,13 @@
package java.beans;
+import java.awt.*;
+import java.awt.font.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.Hashtable;
-import java.util.IdentityHashMap;
import org.apache.harmony.beans.*;
@@ -82,11 +83,15 @@
delegates.put(Method.class, new java_lang_reflect_MethodPersistenceDelegate());
delegates.put(String.class, new java_lang_StringPersistenceDelegate());
delegates.put(Proxy.class, new java_lang_reflect_ProxyPersistenceDelegate());
+
+ delegates.put(Choice.class, new AwtChoicePersistenceDelegate());
+ delegates.put(Color.class, new AwtColorPersistenceDelegate());
+ delegates.put(Rectangle.class, new AwtRectanglePersistenceDelegate());
}
private ExceptionListener listener = defaultExListener;
- private IdentityHashMap oldNewMap = new IdentityHashMap();
+ private ReferenceMap oldNewMap = new ReferenceMap();
/**
* Construct a new encoder.
Added: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/ReferenceMap.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/ReferenceMap.java?view=auto&rev=557172
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/ReferenceMap.java (added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/ReferenceMap.java Tue Jul 17 23:36:03 2007
@@ -0,0 +1,101 @@
+/*
+ * 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;
+
+/**
+ * A simple map.
+ * Keys are equal iff they are the same object (reference equals).
+ * The put() do not check key duplication, so always do a get() before put().
+ * Noop if either key or value is null.
+ *
+ */
+class ReferenceMap {
+
+ private static class Pair {
+ Object key;
+
+ Object value;
+
+ Pair(Object key, Object value) {
+ this.key = key;
+ this.value = value;
+ }
+ }
+
+ private static final int GROWING_UNIT = 16;
+
+ private Pair pairs[] = new Pair[GROWING_UNIT];
+
+ private int size = 0;
+
+ public void clear() {
+ size = 0;
+ }
+
+ public Object get(Object key) {
+ if (key == null) {
+ return null;
+ }
+ for (int i = size - 1; i >= 0; i--) {
+ Pair p = pairs[i];
+ if (p.key == key) {
+ return p.value;
+ }
+ }
+ return null;
+ }
+
+ public void put(Object key, Object value) {
+ if (key == null || value == null) {
+ return;
+ }
+
+ // grow pairs if necessary
+ if (pairs.length <= size) {
+ int newLength = pairs.length + GROWING_UNIT;
+ while (newLength <= size) {
+ newLength += GROWING_UNIT;
+ }
+ Pair newPairs[] = new Pair[newLength];
+ System.arraycopy(pairs, 0, newPairs, 0, size);
+ pairs = newPairs;
+ }
+
+ pairs[size++] = new Pair(key, value);
+ }
+
+ public Object remove(Object key) {
+ if (key == null) {
+ return key;
+ }
+ for (int i = size - 1; i >= 0; i--) {
+ Pair p = pairs[i];
+ if (p.key == key) {
+ pairs[i] = pairs[--size];
+ return p.value;
+ }
+ }
+ return null;
+ }
+
+}
+
+
+
+
Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/ReferenceMap.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java?view=diff&rev=557172&r1=557171&r2=557172
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java Tue Jul 17 23:36:03 2007
@@ -28,7 +28,6 @@
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collections;
-import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
@@ -93,7 +92,7 @@
private Object owner = null;
- private IdentityHashMap records = new IdentityHashMap();
+ private ReferenceMap records = new ReferenceMap();
private boolean writingObject = false;
Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/DefaultPersistenceDelegateTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/DefaultPersistenceDelegateTest.java?view=diff&rev=557172&r1=557171&r2=557172
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/DefaultPersistenceDelegateTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/DefaultPersistenceDelegateTest.java Tue Jul 17 23:36:03 2007
@@ -26,27 +26,27 @@
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;
import java.beans.Statement;
-
-import java.util.Vector;
import java.util.Iterator;
+import java.util.Vector;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.harmony.beans.tests.support.mock.MockFoo;
import org.apache.harmony.beans.tests.support.mock.MockFoo2;
-import org.apache.harmony.beans.tests.support.mock.MockFooStop;
import org.apache.harmony.beans.tests.support.mock.MockFooLabel;
+import org.apache.harmony.beans.tests.support.mock.MockFooStop;
import tests.util.CallVerificationStack;
/**
- * Tests the class java.beans.DefaultPersistenceDelegate
- * TODO refactor the class and remove all references to CallVerificationStack
+ * Tests the class java.beans.DefaultPersistenceDelegate TODO refactor the class
+ * and remove all references to CallVerificationStack
*/
public class DefaultPersistenceDelegateTest extends TestCase {
-
- public DefaultPersistenceDelegateTest() {}
+
+ public DefaultPersistenceDelegateTest() {
+ }
public DefaultPersistenceDelegateTest(String s) {
super(s);
@@ -61,13 +61,13 @@
Introspector.flushCaches();
CallVerificationStack.getInstance().clear();
}
-
+
public static TestSuite suite() {
-// TestSuite suite = new TestSuite();
+ // TestSuite suite = new TestSuite();
TestSuite suite = new TestSuite(DefaultPersistenceDelegateTest.class);
-
-// suite.addTest(new DefaultPersistenceDelegateTest(
-// "testInitialize_NotRegularGetter"));
+
+ // suite.addTest(new DefaultPersistenceDelegateTest(
+ // "testInitialize_NotRegularGetter"));
return suite;
}
@@ -158,12 +158,13 @@
"prop1", null });
MockBean b = new MockBean();
b.setAll("bean1", 2);
- try {
- pd.instantiate(b, new Encoder());
- fail("Should throw NullPointerException!");
- } catch (NullPointerException ex) {
- // expected
- }
+ pd.instantiate(b, new Encoder());
+
+ pd = new MockPersistenceDelegate(new String[] {
+ "prop1", null, "prop2"});
+ MockBean b2 = new MockBean();
+ b2.setAll("bean1", 2);
+ pd.instantiate(b2, new Encoder());
}
/*
@@ -174,12 +175,7 @@
"prop1", "" });
MockBean b = new MockBean();
b.setAll("bean1", 2);
- try {
- pd.instantiate(b, null);
- fail("Should throw NullPointerException!");
- } catch (NullPointerException ex) {
- // expected
- }
+ pd.instantiate(b, null);
}
/*
@@ -366,8 +362,8 @@
* method, defined by its beaninfo.
*/
public void testInstantiate_NotRegularGetter() throws Exception {
- MockPersistenceDelegate pd = new MockPersistenceDelegate(new String[] {
- "prop"});
+ MockPersistenceDelegate pd = new MockPersistenceDelegate(
+ new String[] { "prop" });
MockFoo2 b = new MockFoo2(2);
Expression e = pd.instantiate(b, new Encoder());
@@ -375,16 +371,15 @@
assertSame(MockFoo2.class, e.getTarget());
assertEquals("new", e.getMethodName());
assertEquals(1, e.getArguments().length);
- assertEquals(new Integer(2), e.getArguments()[0]);
+ assertNull(e.getArguments()[0]);
}
-
/*
* Tests mutatesTo() under normal conditions without any properties.
*/
public void testMutatesTo_NormalNoProperty() {
MockPersistenceDelegate pd = new MockPersistenceDelegate();
-
+
assertTrue(pd.mutatesTo("test1", "test1"));
assertFalse(pd.mutatesTo(new Object(), new Object() {
@Override
@@ -428,7 +423,7 @@
*/
public void testMutatesTo_NormalWithEmptyPropertyPublicEqualMethod() {
MockPersistenceDelegate pd = new MockPersistenceDelegate(new String[0]);
-
+
assertTrue(pd.mutatesTo("test1", "test1"));
}
@@ -487,6 +482,31 @@
assertTrue(o1.equalsCalled);
}
+ public void test_mutatesTo_Object() {
+ Object o1 = new Object();
+ Object o2 = new Object();
+ MockPersistenceDelegate mockPersistenceDelegate = new MockPersistenceDelegate();
+ assertTrue(mockPersistenceDelegate.mutatesTo(o1, o2));
+ }
+
+ public void test_initialize() {
+ MockBean3 bean1 = new MockBean3();
+ bean1.setValue("bean1");
+ MockBean3 bean2 = new MockBean3();
+ bean2.setValue("bean2");
+
+ // clear flags
+ bean1.setValueCalled = false;
+ bean2.setValueCalled = false;
+
+ MockPersistenceDelegate mockPersistenceDelegate = new MockPersistenceDelegate();
+ mockPersistenceDelegate.initialize(MockBean3.class, bean1, bean2,
+ new Encoder());
+ assertEquals("bean1", bean1.getValue());
+ assertEquals("bean2", bean2.getValue());
+ assertFalse(bean1.setValueCalled);
+ assertFalse(bean2.setValueCalled);
+ }
public void test_mutates_with_equals_false() {
MyObjectEqualsFalse o1 = new MyObjectEqualsFalse();
@@ -579,12 +599,12 @@
pd.writeObject(oldBean, enc);
enc.clearCache();
pd.initialize(MockFoo.class, oldBean, new MockFoo(), enc);
-
+
assertNotNull(findStatement(enc.statements(), oldBean, "setName",
new Object[] { oldBean.getName() }));
assertNotNull(findStatement(enc.statements(), oldBean, "setLabel",
new Object[] { oldBean.getLabel() }));
-
+
enc = new CollectingEncoder();
oldBean = new MockFoo();
oldBean.setComplexLabel(new MockFooLabel("myComplexLabel"));
@@ -613,34 +633,32 @@
pd.initialize(MockFoo2.class, b, b2, enc);
// XXX RI stores much more statements to the stream
- iter = enc.statements();
-// assertNotNull("required statement not found",
-// findStatement(iter, b, "myget", null));
- assertNotNull("required statement not found",
- findStatement(iter, null, "myset",
- new Object[] {new Integer(2)}));
+ iter = enc.statements();
+ // assertNotNull("required statement not found",
+ // findStatement(iter, b, "myget", null));
+ assertNotNull("required statement not found", findStatement(iter, null,
+ "myset", new Object[] { new Integer(2) }));
}
/*
- * Test initialize() when oldInstance == newInstance.
- * XXX The current implementation outputs nothing to the stream. And this
- * seems to be correct from the spec point of view since we need not to do
- * any actions to convert the object to itself. However, RI outputs a lot
- * of stuff to the stream here.
- */
-// public void testInitialize_SameInstance() throws Exception {
-// CollectingEncoder enc = new CollectingEncoder();
-// MockPersistenceDelegate pd = new MockPersistenceDelegate();
-// MockFoo b = new MockFoo();
-// Iterator<Statement> iter;
-//
-// b.setName("mymyName");
-// // b.setLabel("myLabel");
-//
-// pd.initialize(MockFoo.class, b, b, enc);
-//
-// }
-
+ * Test initialize() when oldInstance == newInstance. XXX The current
+ * implementation outputs nothing to the stream. And this seems to be
+ * correct from the spec point of view since we need not to do any actions
+ * to convert the object to itself. However, RI outputs a lot of stuff to
+ * the stream here.
+ */
+ // public void testInitialize_SameInstance() throws Exception {
+ // CollectingEncoder enc = new CollectingEncoder();
+ // MockPersistenceDelegate pd = new MockPersistenceDelegate();
+ // MockFoo b = new MockFoo();
+ // Iterator<Statement> iter;
+ //
+ // b.setName("mymyName");
+ // // b.setLabel("myLabel");
+ //
+ // pd.initialize(MockFoo.class, b, b, enc);
+ //
+ // }
/*
* Test initialize() with a bean with a transient property.
*/
@@ -648,14 +666,14 @@
CollectingEncoder enc = new CollectingEncoder();
MockPersistenceDelegate pd = new MockPersistenceDelegate();
MockTransientBean b = new MockTransientBean();
-
+
b.setName("myName");
pd.writeObject(b, enc);
enc.clearCache();
pd.initialize(MockTransientBean.class, b, new MockTransientBean(), enc);
- assertFalse("transient fields should not be affected",
- enc.statements().hasNext());
-
+ assertFalse("transient fields should not be affected", enc.statements()
+ .hasNext());
+
// set transient to false
Introspector.flushCaches();
MockTransientBeanBeanInfo.setTransient(false);
@@ -1061,7 +1079,9 @@
/**
* Searches for the statement with given parameters.
- * @param iter iterator to search through, null means ignore this parameter
+ *
+ * @param iter
+ * iterator to search through, null means ignore this parameter
* @param target
* @param methodName
* @param args
@@ -1072,29 +1092,26 @@
while (iter.hasNext()) {
Statement stmt = iter.next();
-
+
if (target != null && stmt.getTarget() != target) {
continue;
}
-
- if (methodName != null && !methodName.equals(stmt.getMethodName()))
- {
+
+ if (methodName != null && !methodName.equals(stmt.getMethodName())) {
continue;
}
if (args != null) {
- if ((stmt.getArguments() != null &&
- args.length != stmt.getArguments().length)
- || stmt.getArguments() == null)
- {
+ if ((stmt.getArguments() != null && args.length != stmt
+ .getArguments().length)
+ || stmt.getArguments() == null) {
continue;
- }
-
+ }
+
for (int i = 0; i < args.length; i++) {
- if ((args[i] == null && stmt.getArguments()[i] != null) ||
- (args[i] != null && stmt.getArguments()[i] == null) ||
- !args[i].equals(stmt.getArguments()[i]))
- {
+ if ((args[i] == null && stmt.getArguments()[i] != null)
+ || (args[i] != null && stmt.getArguments()[i] == null)
+ || !args[i].equals(stmt.getArguments()[i])) {
continue;
}
}
@@ -1102,13 +1119,13 @@
return stmt;
}
-
+
return null;
}
-
+
public static class CollectingEncoder extends Encoder {
private Vector<Statement> statements = new Vector<Statement>();
-
+
@Override
public void writeExpression(Expression exp) {
statements.add(exp);
@@ -1120,13 +1137,29 @@
statements.add(stm);
super.writeStatement(stm);
}
-
+
public Iterator<Statement> statements() {
return statements.iterator();
}
-
+
public void clearCache() {
statements = new Vector<Statement>();
+ }
+ }
+
+ public static class MockBean3
+ {
+ public boolean setValueCalled = false;
+
+ public String value;
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ setValueCalled = true;
}
}
}
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=557172&r1=557171&r2=557172
==============================================================================
--- 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 Tue Jul 17 23:36:03 2007
@@ -17,9 +17,13 @@
package org.apache.harmony.beans.tests.java.beans;
+import java.awt.*;
+import java.awt.event.*;
import java.beans.Encoder;
import java.beans.Expression;
import java.beans.PersistenceDelegate;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
import java.beans.Statement;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
@@ -38,6 +42,7 @@
import org.apache.harmony.beans.tests.support.mock.MockFoo;
import org.apache.harmony.beans.tests.support.mock.MockFooStop;
+
/**
* Test java.beans.PersistenceDelegate
*/
@@ -59,7 +64,7 @@
MockFoo foo = new MockFoo();
pd.writeObject(foo, enc);
-
+
assertEquals("initialize", pd.popMethod());
assertEquals("mutatesTo", pd.popMethod());
}
@@ -74,7 +79,7 @@
MockFoo foo = new MockFoo();
pd.writeObject(foo, enc);
-
+
assertEquals("instantiate", pd.popMethod());
assertEquals("mutatesTo", pd.popMethod());
assertWasAdded(MockFoo.class.getClass(), "new", null, enc);
@@ -84,8 +89,8 @@
* Tests writeObject() when object is null.
*/
public void testWriteObject_NullObject() {
- MockPersistenceDelegate2 pd = new MockPersistenceDelegate2();
- Encoder enc = new Encoder();
+ MockPersistenceDelegate2 pd = new MockPersistenceDelegate2();
+ Encoder enc = new Encoder();
try {
pd.writeObject(null, enc);
@@ -99,7 +104,7 @@
* Tests writeObject() when encoder is null.
*/
public void testWriteObject_NullEncoder() {
- MockPersistenceDelegate2 pd = new MockPersistenceDelegate2();
+ MockPersistenceDelegate2 pd = new MockPersistenceDelegate2();
try {
pd.writeObject(new MockFoo(), null);
@@ -116,17 +121,18 @@
DummyPersistenceDelegate pd = new DummyPersistenceDelegate();
MockPersistenceDelegate3 pd3 = new MockPersistenceDelegate3();
Encoder enc = new Encoder();
-
+
enc.setPersistenceDelegate(MockFooStop.class, pd3);
pd.initialize(MockFoo.class, new MockFoo(), new MockFoo(), enc);
assertEquals("initialize", pd3.popMethod());
assertFalse("Extra statement has been detected", pd3.hasMoreMethods());
-
+
// test interface
pd3 = new MockPersistenceDelegate3();
enc.setPersistenceDelegate(MockInterface.class, pd3);
- pd.initialize(MockObject.class, new MockObject(), new MockObject(),
- enc);
+ pd
+ .initialize(MockObject.class, new MockObject(),
+ new MockObject(), enc);
assertFalse("Extra statement has been detected", pd3.hasMoreMethods());
}
@@ -139,7 +145,7 @@
enc.setPersistenceDelegate(MockFooStop.class,
new DummyPersistenceDelegate());
-
+
try {
pd.initialize(null, new Object(), new Object(), enc);
fail("Should throw NullPointerException!");
@@ -176,13 +182,13 @@
}
/**
- * Circular redundancy check. Should not hang.
- * Regression test for HARMONY-2073
+ * Circular redundancy check. Should not hang. Regression test for
+ * HARMONY-2073
*/
public void testInitialize_circularRedundancy() {
Encoder enc = new Encoder();
DummyPersistenceDelegate pd = new DummyPersistenceDelegate();
-
+
enc.setPersistenceDelegate(MockFooStop.class, pd);
pd.initialize(MockFoo.class, new MockFoo(), new MockFoo(), enc);
}
@@ -213,50 +219,56 @@
assertFalse(pd.mutatesTo(null, "test"));
}
- public void test_writeObject_Null_LXMLEncoder() throws Exception{
+ public void test_writeObject_Null_LXMLEncoder() throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(byteArrayOutputStream));
+ XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
+ byteArrayOutputStream));
encoder.writeObject(null);
encoder.close();
- DataInputStream stream = new DataInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
+ DataInputStream stream = new DataInputStream(new ByteArrayInputStream(
+ byteArrayOutputStream.toByteArray()));
XMLDecoder decoder = new XMLDecoder(stream);
assertNull(decoder.readObject());
- stream = new DataInputStream(PersistenceDelegateTest.class.getResourceAsStream("/xml/null.xml"));
+ stream = new DataInputStream(PersistenceDelegateTest.class
+ .getResourceAsStream("/xml/null.xml"));
decoder = new XMLDecoder(stream);
assertNull(decoder.readObject());
}
class Bar {
public int value;
+
public void barTalk() {
System.out.println("Bar is coming!");
}
}
+
public void test_writeObject_java_lang_reflect_Field()
- throws SecurityException, NoSuchFieldException, IOException {
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
- byteArrayOutputStream));
- Field value = Bar.class.getField("value");
- encoder.writeObject(value);
- encoder.close();
-
- DataInputStream stream = new DataInputStream(new ByteArrayInputStream(
- byteArrayOutputStream.toByteArray()));
-
- XMLDecoder decoder = new XMLDecoder(stream); Field field = (Field)
- decoder.readObject();
+ throws SecurityException, NoSuchFieldException, IOException {
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
+ byteArrayOutputStream));
+ Field value = Bar.class.getField("value");
+ encoder.writeObject(value);
+ encoder.close();
+
+ DataInputStream stream = new DataInputStream(new ByteArrayInputStream(
+ byteArrayOutputStream.toByteArray()));
+
+ XMLDecoder decoder = new XMLDecoder(stream);
+ Field field = (Field) decoder.readObject();
assertEquals(value, field);
- assertEquals(value.getName(), field.getName());
- }
-
- public void test_writeObject_java_lang_reflect_Method() throws SecurityException, NoSuchMethodException{
+ assertEquals(value.getName(), field.getName());
+ }
+
+ public void test_writeObject_java_lang_reflect_Method()
+ throws SecurityException, NoSuchMethodException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
- byteArrayOutputStream));
- Method method = Bar.class.getMethod("barTalk", (Class[])null);
+ byteArrayOutputStream));
+ Method method = Bar.class.getMethod("barTalk", (Class[]) null);
encoder.writeObject(method);
encoder.close();
@@ -268,11 +280,12 @@
assertEquals(method.getName(), aMethod.getName());
assertEquals("barTalk", aMethod.getName());
}
-
+
+ @SuppressWarnings("unchecked")
public void test_writeObject_java_util_Collection() {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
- byteArrayOutputStream));
+ byteArrayOutputStream));
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(10);
list.addFirst(2);
@@ -287,13 +300,145 @@
assertEquals(list, l);
assertEquals(2, l.size());
assertEquals(new Integer(10), l.get(1));
-
+
+ }
+
+ public void test_writeObject_java_awt_Choice() {
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
+ byteArrayOutputStream));
+ Choice choice = new Choice();
+ choice.setBackground(Color.blue);
+ choice.setFocusTraversalKeysEnabled(true);
+ choice.setBounds(0, 0, 10, 10);
+ choice.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
+ choice.setCursor(new Cursor(Cursor.WAIT_CURSOR));
+ choice.setFocusTraversalKeysEnabled(true);
+ choice.setFocusable(true);
+ choice.setFont(new Font("Arial Bold", Font.ITALIC, 0));
+ choice.setForeground(Color.green);
+ choice.setIgnoreRepaint(true);
+ choice.setLocation(1, 2);
+ choice.setName("choice");
+ choice.setSize(new Dimension(200, 100));
+ choice.addItem("addItem");
+ choice.add("add");
+
+ ComponentListener cl = new ComponentAdapter() {
+ };
+ choice.addComponentListener(cl);
+ FocusListener fl = new FocusAdapter() {
+ };
+ choice.addFocusListener(fl);
+ HierarchyBoundsListener hbl = new HierarchyBoundsAdapter() {
+ };
+ choice.addHierarchyBoundsListener(hbl);
+ HierarchyListener hl = new HierarchyListener() {
+ public void hierarchyChanged(HierarchyEvent e) {
+ }
+ };
+ choice.addHierarchyListener(hl);
+ InputMethodListener il = new InputMethodListener() {
+ public void caretPositionChanged(InputMethodEvent e) {
+ }
+
+ public void inputMethodTextChanged(InputMethodEvent e) {
+ }
+ };
+ choice.addInputMethodListener(il);
+ ItemListener il2 = new ItemListener() {
+ public void itemStateChanged(ItemEvent e) {
+ }
+ };
+ choice.addItemListener(il2);
+ KeyListener kl = new KeyAdapter() {
+ };
+ choice.addKeyListener(kl);
+ MouseListener ml = new MouseAdapter() {
+ };
+ choice.addMouseListener(ml);
+ MouseMotionListener mml = new MouseMotionAdapter() {
+ };
+ choice.addMouseMotionListener(mml);
+ MouseWheelListener mwl = new MouseWheelListener() {
+ public void mouseWheelMoved(MouseWheelEvent e) {
+ }
+ };
+ choice.addMouseWheelListener(mwl);
+ PropertyChangeListener pcl = new PropertyChangeListener() {
+ public void propertyChange(PropertyChangeEvent event) {
+ }
+ };
+ choice.addPropertyChangeListener(pcl);
+ System.out.println(encoder.getPersistenceDelegate(Choice.class));
+ encoder.writeObject(choice);
+ encoder.close();
+ DataInputStream stream = new DataInputStream(new ByteArrayInputStream(
+ byteArrayOutputStream.toByteArray()));
+ XMLDecoder decoder = new XMLDecoder(stream);
+ Choice aChoice = (Choice) decoder.readObject();
+ assertEquals(choice.getFocusTraversalKeysEnabled(), aChoice
+ .getFocusTraversalKeysEnabled());
+ assertEquals(Color.blue, aChoice.getBackground());
+ assertEquals(new Rectangle(1, 2, 200, 100), aChoice.getBounds());
+ // ComponentOrientation is not persistent
+ assertTrue(aChoice.getComponentOrientation().isLeftToRight());
+
+ // Cursor will not be persisted
+ assertEquals(Cursor.DEFAULT_CURSOR, aChoice.getCursor().getType());
+ // DropTarget will not be persisted
+ assertNull(aChoice.getDropTarget());
+
+ assertEquals(choice.getName(), aChoice.getName());
+
+ assertEquals(choice.getItem(0), aChoice.getItem(0));
+ assertEquals(1, choice.getComponentListeners().length);
+ assertEquals(0, aChoice.getComponentListeners().length);
+ assertEquals(1, choice.getFocusListeners().length);
+ assertEquals(0, aChoice.getFocusListeners().length);
+ assertEquals(1, choice.getHierarchyBoundsListeners().length);
+ assertEquals(0, aChoice.getHierarchyBoundsListeners().length);
+ assertEquals(1, choice.getInputMethodListeners().length);
+ assertEquals(0, aChoice.getInputMethodListeners().length);
+ assertEquals(1, choice.getItemListeners().length);
+ assertEquals(0, aChoice.getItemListeners().length);
+ assertEquals(1, choice.getKeyListeners().length);
+ assertEquals(0, aChoice.getKeyListeners().length);
+ assertEquals(1, choice.getMouseListeners().length);
+ assertEquals(0, aChoice.getMouseListeners().length);
+ assertEquals(1, choice.getMouseMotionListeners().length);
+ assertEquals(0, aChoice.getMouseMotionListeners().length);
+ assertEquals(1, choice.getMouseWheelListeners().length);
+ assertEquals(0, aChoice.getMouseWheelListeners().length);
+ assertEquals(1, choice.getPropertyChangeListeners().length);
+ assertEquals(0, aChoice.getPropertyChangeListeners().length);
+
+ stream = new DataInputStream(PersistenceDelegateTest.class
+ .getResourceAsStream("/xml/Choice.xml"));
+
+ decoder = new XMLDecoder(stream);
+ aChoice = (Choice) decoder.readObject();
+ assertEquals(choice.getFocusTraversalKeysEnabled(), aChoice
+ .getFocusTraversalKeysEnabled());
+ assertEquals(Color.blue, aChoice.getBackground());
+ assertEquals(new Rectangle(1, 2, 200, 100), aChoice.getBounds());
+ // ComponentOrientation is not persistent
+ assertTrue(aChoice.getComponentOrientation().isLeftToRight());
+
+ // Cursor will not be persisted
+ assertEquals(Cursor.DEFAULT_CURSOR, aChoice.getCursor().getType());
+ // DropTarget will not be persisted
+ assertNull(aChoice.getDropTarget());
+
+ assertEquals(choice.getName(), aChoice.getName());
+
+ assertEquals(choice.getItem(0), aChoice.getItem(0));
}
// <--
private void assertWasAdded(Class<?> targetClass, String methodName,
- Object[] args, MockEncoder2 enc) {
+ Object[] args, MockEncoder2 enc) {
try {
while (true) {
Statement stmt = enc.pop();
@@ -308,8 +453,8 @@
}
private boolean equals(Statement stmt, Class<?> targetClass,
- String methodName, Object[] args) {
-
+ String methodName, Object[] args) {
+
if (stmt == null || !methodName.equals(stmt.getMethodName())) {
return false;
}
@@ -324,10 +469,10 @@
return false;
}
}
-
+
if (args != null) {
- if (stmt.getArguments() == null ||
- args.length != stmt.getArguments().length) {
+ if (stmt.getArguments() == null
+ || args.length != stmt.getArguments().length) {
return false;
}
@@ -348,7 +493,7 @@
return false;
}
}
-
+
return true;
}
@@ -368,7 +513,7 @@
static class MockEncoder2 extends Encoder {
Stack<Statement> stmts = new Stack<Statement>();
-
+
@Override
public void writeExpression(Expression expr) {
stmts.push(expr);
@@ -380,7 +525,7 @@
stmts.push(stmt);
super.writeStatement(stmt);
}
-
+
@Override
public void writeObject(Object obj) {
super.writeObject(obj);
@@ -394,10 +539,12 @@
static class MockPersistenceDelegate2 extends PersistenceDelegate {
private Boolean mutatesToFlag = null;
+
Stack<String> methods = new Stack<String>();
- public MockPersistenceDelegate2() {}
-
+ public MockPersistenceDelegate2() {
+ }
+
public MockPersistenceDelegate2(boolean mutatesToFlag) {
this.mutatesToFlag = Boolean.valueOf(mutatesToFlag);
}
@@ -408,13 +555,13 @@
methods.push("initialize");
super.initialize(type, oldInstance, newInstance, enc);
}
-
+
@Override
public Expression instantiate(Object oldInstance, Encoder out) {
methods.push("instantiate");
return new Expression(oldInstance.getClass(), "new", null);
}
-
+
@Override
public boolean mutatesTo(Object oldInstance, Object newInstance) {
methods.push("mutatesTo");
@@ -424,11 +571,11 @@
}
return super.mutatesTo(oldInstance, newInstance);
}
-
+
String popMethod() {
return methods.pop();
}
-
+
boolean hasMoreMethods() {
return !methods.empty();
}
Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Choice.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Choice.xml?view=auto&rev=557172
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Choice.xml (added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Choice.xml Tue Jul 17 23:36:03 2007
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<java version="1.6.0" class="java.beans.XMLDecoder">
+ <object class="java.awt.Choice">
+ <void property="background">
+ <object class="java.awt.Color">
+ <int>0</int>
+ <int>0</int>
+ <int>255</int>
+ <int>255</int>
+ </object>
+ </void>
+ <void property="foreground">
+ <object class="java.awt.Color">
+ <int>0</int>
+ <int>255</int>
+ <int>0</int>
+ <int>255</int>
+ </object>
+ </void>
+ <void property="font">
+ <object class="java.awt.Font">
+ <string>Arial Bold</string>
+ <int>2</int>
+ <int>0</int>
+ </object>
+ </void>
+ <void property="bounds">
+ <object class="java.awt.Rectangle">
+ <int>1</int>
+ <int>2</int>
+ <int>200</int>
+ <int>100</int>
+ </object>
+ </void>
+ <void property="name">
+ <string>choice</string>
+ </void>
+ <void method="add">
+ <string>addItem</string>
+ </void>
+ <void method="add">
+ <string>add</string>
+ </void>
+ </object>
+</java>
\ No newline at end of file
Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/resources/xml/Choice.xml
------------------------------------------------------------------------------
svn:eol-style = native