8013557: XMLEncoder in 1.7 can't encode objects initialized in no argument constructor
authormalenkov
Fri, 31 May 2013 18:25:38 +0400
changeset 17896 271991446515
parent 17895 ba1e105d3d7f
child 17897 fb367e67916d
8013557: XMLEncoder in 1.7 can't encode objects initialized in no argument constructor Reviewed-by: alexsch
jdk/src/share/classes/java/beans/XMLEncoder.java
jdk/test/java/beans/XMLEncoder/Test6989223.java
jdk/test/java/beans/XMLEncoder/Test7080156.java
jdk/test/java/beans/XMLEncoder/Test8013557.java
--- a/jdk/src/share/classes/java/beans/XMLEncoder.java	Fri May 31 15:56:07 2013 +0400
+++ b/jdk/src/share/classes/java/beans/XMLEncoder.java	Fri May 31 18:25:38 2013 +0400
@@ -487,6 +487,12 @@
         }
         indentation--;
 
+        Statement statement = getMissedStatement();
+        while (statement != null) {
+            outputStatement(statement, this, false);
+            statement = getMissedStatement();
+        }
+
         try {
             out.flush();
         }
@@ -503,6 +509,17 @@
         targetToStatementList.clear();
     }
 
+    Statement getMissedStatement() {
+        for (List<Statement> statements : this.targetToStatementList.values()) {
+            for (int i = 0; i < statements.size(); i++) {
+                if (Statement.class == statements.get(i).getClass()) {
+                    return statements.remove(i);
+                }
+            }
+        }
+        return null;
+    }
+
 
     /**
      * This method calls <code>flush</code>, writes the closing
@@ -597,7 +614,7 @@
                                                 "methodName") + " should not be null");
             }
 
-            if (target instanceof Field && methodName.equals("get")) {
+            if (isArgument && target instanceof Field && methodName.equals("get")) {
                 Field f = (Field)target;
                 writeln("<object class=" + quote(f.getDeclaringClass().getName()) +
                         " field=" + quote(f.getName()) + "/>");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/beans/XMLEncoder/Test6989223.java	Fri May 31 18:25:38 2013 +0400
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6989223
+ * @summary Tests Rectangle2D.Double encoding
+ * @author Sergey Malenkov
+ */
+
+import java.awt.geom.Rectangle2D;
+
+public class Test6989223 extends AbstractTest {
+    public static void main(String[] args) {
+        new Test6989223().test(true);
+    }
+
+    protected Object getObject() {
+        return new Bean(1, 2, 3, 4);
+    }
+
+    @Override
+    protected Object getAnotherObject() {
+        return new Bean(1, 2, 3, 5);
+    }
+
+    public static class Bean extends Rectangle2D.Double {
+        public Bean() {
+        }
+
+        public Bean(double x, double y, double w, double h) {
+            super(x, y, w, h);
+        }
+
+        @Override
+        public boolean equals(Object object) {
+            return super.equals(object); // to avoid recursion during validation
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/beans/XMLEncoder/Test7080156.java	Fri May 31 18:25:38 2013 +0400
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 7080156 7094245
+ * @summary Tests beans with public arrays
+ * @author Sergey Malenkov
+ */
+
+public class Test7080156 extends AbstractTest {
+    public static void main(String[] args) {
+        new Test7080156().test(true);
+    }
+
+    protected Object getObject() {
+        Bean bean = new Bean();
+        bean.setArray("something");
+        return bean;
+    }
+
+    @Override
+    protected Object getAnotherObject() {
+        Bean bean = new Bean();
+        bean.setArray("some", "thing");
+        return bean;
+    }
+
+    public static class Bean {
+        public String[] array = {"default"};
+
+        public void setArray(String... array) {
+            this.array = array;
+        }
+
+        public String[] getArray() {
+            return this.array;
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/beans/XMLEncoder/Test8013557.java	Fri May 31 18:25:38 2013 +0400
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8013557
+ * @summary Tests beans with public fields
+ * @author Sergey Malenkov
+ */
+
+public class Test8013557 extends AbstractTest {
+    public static void main(String[] args) {
+        new Test8013557().test(true);
+    }
+
+    protected Object getObject() {
+        return new Bean(new Value("something"));
+    }
+
+    @Override
+    protected Object getAnotherObject() {
+        return new Bean(new Value());
+    }
+
+    public static class Bean {
+        public Value value;
+
+        public Bean() {
+            this.value = new Value();
+        }
+
+        public Bean(Value value) {
+            this.value = value;
+        }
+
+        public void setValue(Value value) {
+            this.value = value;
+        }
+
+        public Value getValue() {
+            return this.value;
+        }
+    }
+
+    public static class Value {
+        private String string;
+
+        public Value() {
+            this.string = "default";
+        }
+
+        public Value(String value) {
+            this.string = value;
+        }
+
+        public void setString(String string) {
+            this.string = string;
+        }
+
+        public String getString() {
+            return this.string;
+        }
+    }
+}