From cd6f30d3bc8b1fcf191f105833b4feedce9f2db6 Mon Sep 17 00:00:00 2001 From: Sergey Malenkov Date: Thu, 20 May 2010 18:44:51 +0400 Subject: [PATCH] 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type Reviewed-by: rupashka --- .../beans/DefaultPersistenceDelegate.java | 19 ++++ .../share/classes/java/beans/MetaData.java | 98 ------------------- .../share/classes/java/beans/XMLEncoder.java | 25 ++++- .../java_awt_GridBagConstraints.java | 3 +- 4 files changed, 41 insertions(+), 104 deletions(-) diff --git a/jdk/src/share/classes/java/beans/DefaultPersistenceDelegate.java b/jdk/src/share/classes/java/beans/DefaultPersistenceDelegate.java index 9152909816e..2dc9a9d3d17 100644 --- a/jdk/src/share/classes/java/beans/DefaultPersistenceDelegate.java +++ b/jdk/src/share/classes/java/beans/DefaultPersistenceDelegate.java @@ -222,6 +222,25 @@ public class DefaultPersistenceDelegate extends PersistenceDelegate { // 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); diff --git a/jdk/src/share/classes/java/beans/MetaData.java b/jdk/src/share/classes/java/beans/MetaData.java index 4827d785b38..9d3a1053d0e 100644 --- a/jdk/src/share/classes/java/beans/MetaData.java +++ b/jdk/src/share/classes/java/beans/MetaData.java @@ -700,56 +700,6 @@ class java_beans_beancontext_BeanContextSupport_PersistenceDelegate extends java // 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} @@ -774,54 +724,6 @@ final class java_awt_Insets_PersistenceDelegate extends PersistenceDelegate { } } -/** - * 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} diff --git a/jdk/src/share/classes/java/beans/XMLEncoder.java b/jdk/src/share/classes/java/beans/XMLEncoder.java index e13c8262c88..e07286592fe 100644 --- a/jdk/src/share/classes/java/beans/XMLEncoder.java +++ b/jdk/src/share/classes/java/beans/XMLEncoder.java @@ -407,7 +407,20 @@ public class XMLEncoder extends Encoder { 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 @@ public class XMLEncoder extends Encoder { 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 @@ public class XMLEncoder extends Encoder { } 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); diff --git a/jdk/test/java/beans/XMLEncoder/java_awt_GridBagConstraints.java b/jdk/test/java/beans/XMLEncoder/java_awt_GridBagConstraints.java index 452491c7a14..a832f2e1b77 100644 --- a/jdk/test/java/beans/XMLEncoder/java_awt_GridBagConstraints.java +++ b/jdk/test/java/beans/XMLEncoder/java_awt_GridBagConstraints.java @@ -55,7 +55,6 @@ public final class java_awt_GridBagConstraints extends AbstractTest