changeset 2374:a71c27e3ec4a

6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type Reviewed-by: rupashka
author malenkov
date Thu, 20 May 2010 18:44:51 +0400
parents 57d7b80faad8
children bbd5a5a4e64f
files src/share/classes/java/beans/DefaultPersistenceDelegate.java src/share/classes/java/beans/MetaData.java src/share/classes/java/beans/XMLEncoder.java test/java/beans/XMLEncoder/java_awt_GridBagConstraints.java
diffstat 4 files changed, 41 insertions(+), 104 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/beans/DefaultPersistenceDelegate.java	Tue May 18 16:40:53 2010 +0900
+++ b/src/share/classes/java/beans/DefaultPersistenceDelegate.java	Thu May 20 18:44:51 2010 +0400
@@ -222,6 +222,25 @@
 
     // Write out the properties of this instance.
     private void initBean(Class type, Object oldInstance, Object newInstance, Encoder out) {
+        for (Field field : type.getFields()) {
+            int mod = field.getModifiers();
+            if (Modifier.isFinal(mod) || Modifier.isStatic(mod) || Modifier.isTransient(mod)) {
+                continue;
+            }
+            try {
+                Expression oldGetExp = new Expression(field, "get", new Object[] { oldInstance });
+                Expression newGetExp = new Expression(field, "get", new Object[] { newInstance });
+                Object oldValue = oldGetExp.getValue();
+                Object newValue = newGetExp.getValue();
+                out.writeExpression(oldGetExp);
+                if (!equals(newValue, out.get(oldValue))) {
+                    out.writeStatement(new Statement(field, "set", new Object[] { oldInstance, oldValue }));
+                }
+            }
+            catch (Exception exception) {
+                out.getExceptionListener().exceptionThrown(exception);
+            }
+        }
         BeanInfo info;
         try {
             info = Introspector.getBeanInfo(type);
--- a/src/share/classes/java/beans/MetaData.java	Tue May 18 16:40:53 2010 +0900
+++ b/src/share/classes/java/beans/MetaData.java	Thu May 20 18:44:51 2010 +0400
@@ -701,56 +701,6 @@
 // AWT
 
 /**
- * The persistence delegate for {@link Dimension}.
- * It is impossible to use {@link DefaultPersistenceDelegate}
- * because all getters have return types that differ from parameter types
- * of the constructor {@link Dimension#Dimension(int, int)}.
- *
- * @author Sergey A. Malenkov
- */
-final class java_awt_Dimension_PersistenceDelegate extends PersistenceDelegate {
-    protected boolean mutatesTo(Object oldInstance, Object newInstance) {
-        return oldInstance.equals(newInstance);
-    }
-
-    protected Expression instantiate(Object oldInstance, Encoder out) {
-        Dimension dimension = (Dimension) oldInstance;
-        Object[] args = new Object[] {
-                dimension.width,
-                dimension.height,
-        };
-        return new Expression(dimension, dimension.getClass(), "new", args);
-    }
-}
-
-/**
- * The persistence delegate for {@link GridBagConstraints}.
- * It is impossible to use {@link DefaultPersistenceDelegate}
- * because this class does not have any properties.
- *
- * @author Sergey A. Malenkov
- */
-final class java_awt_GridBagConstraints_PersistenceDelegate extends PersistenceDelegate {
-    protected Expression instantiate(Object oldInstance, Encoder out) {
-        GridBagConstraints gbc = (GridBagConstraints) oldInstance;
-        Object[] args = new Object[] {
-                gbc.gridx,
-                gbc.gridy,
-                gbc.gridwidth,
-                gbc.gridheight,
-                gbc.weightx,
-                gbc.weighty,
-                gbc.anchor,
-                gbc.fill,
-                gbc.insets,
-                gbc.ipadx,
-                gbc.ipady,
-        };
-        return new Expression(gbc, gbc.getClass(), "new", args);
-    }
-}
-
-/**
  * The persistence delegate for {@link Insets}.
  * It is impossible to use {@link DefaultPersistenceDelegate}
  * because this class does not have any properties.
@@ -775,54 +725,6 @@
 }
 
 /**
- * The persistence delegate for {@link Point}.
- * It is impossible to use {@link DefaultPersistenceDelegate}
- * because all getters have return types that differ from parameter types
- * of the constructor {@link Point#Point(int, int)}.
- *
- * @author Sergey A. Malenkov
- */
-final class java_awt_Point_PersistenceDelegate extends PersistenceDelegate {
-    protected boolean mutatesTo(Object oldInstance, Object newInstance) {
-        return oldInstance.equals(newInstance);
-    }
-
-    protected Expression instantiate(Object oldInstance, Encoder out) {
-        Point point = (Point) oldInstance;
-        Object[] args = new Object[] {
-                point.x,
-                point.y,
-        };
-        return new Expression(point, point.getClass(), "new", args);
-    }
-}
-
-/**
- * The persistence delegate for {@link Rectangle}.
- * It is impossible to use {@link DefaultPersistenceDelegate}
- * because all getters have return types that differ from parameter types
- * of the constructor {@link Rectangle#Rectangle(int, int, int, int)}.
- *
- * @author Sergey A. Malenkov
- */
-final class java_awt_Rectangle_PersistenceDelegate extends PersistenceDelegate {
-    protected boolean mutatesTo(Object oldInstance, Object newInstance) {
-        return oldInstance.equals(newInstance);
-    }
-
-    protected Expression instantiate(Object oldInstance, Encoder out) {
-        Rectangle rectangle = (Rectangle) oldInstance;
-        Object[] args = new Object[] {
-                rectangle.x,
-                rectangle.y,
-                rectangle.width,
-                rectangle.height,
-        };
-        return new Expression(rectangle, rectangle.getClass(), "new", args);
-    }
-}
-
-/**
  * The persistence delegate for {@link Font}.
  * It is impossible to use {@link DefaultPersistenceDelegate}
  * because size of the font can be float value.
--- a/src/share/classes/java/beans/XMLEncoder.java	Tue May 18 16:40:53 2010 +0900
+++ b/src/share/classes/java/beans/XMLEncoder.java	Thu May 20 18:44:51 2010 +0400
@@ -407,7 +407,20 @@
                    os.writeObject(this);
             */
             mark(oldStm);
-            statementList(oldStm.getTarget()).add(oldStm);
+            Object target = oldStm.getTarget();
+            if (target instanceof Field) {
+                String method = oldStm.getMethodName();
+                Object[] args = oldStm.getArguments();
+                if ((method == null) || (args == null)) {
+                }
+                else if (method.equals("get") && (args.length == 1)) {
+                    target = args[0];
+                }
+                else if (method.equals("set") && (args.length == 2)) {
+                    target = args[0];
+                }
+            }
+            statementList(target).add(oldStm);
         }
         catch (Exception e) {
             getExceptionListener().exceptionThrown(new Exception("XMLEncoder: discarding statement " + oldStm, e));
@@ -703,7 +716,9 @@
                 statements.add(exp);
             }
             outputValue(target, outer, false);
-            outputValue(value, outer, isArgument);
+            if (expression) {
+                outputValue(value, outer, isArgument);
+            }
             return;
         }
         if (expression && (d.refs > 1)) {
@@ -722,8 +737,10 @@
         }
         else if ((!expression && methodName.startsWith("set") && args.length == 1) ||
                  (expression && methodName.startsWith("get") && args.length == 0)) {
-            attributes = attributes + " property=" +
-                quote(Introspector.decapitalize(methodName.substring(3)));
+            if (3 < methodName.length()) {
+                attributes = attributes + " property=" +
+                    quote(Introspector.decapitalize(methodName.substring(3)));
+            }
         }
         else if (!methodName.equals("new") && !methodName.equals("newInstance")) {
             attributes = attributes + " method=" + quote(methodName);
--- a/test/java/beans/XMLEncoder/java_awt_GridBagConstraints.java	Tue May 18 16:40:53 2010 +0900
+++ b/test/java/beans/XMLEncoder/java_awt_GridBagConstraints.java	Thu May 20 18:44:51 2010 +0400
@@ -55,7 +55,6 @@
     }
 
     protected GridBagConstraints getAnotherObject() {
-        return null; // TODO: could not update property
-        // return new GridBagConstraints();
+        return new GridBagConstraints();
     }
 }