6921644: XMLEncoder generates invalid XML
authormalenkov
Thu, 04 Mar 2010 21:17:03 +0300
changeset 4969 5a6e31d5ab6d
parent 4968 517b279d7f2b
child 4971 2e385fe87079
6921644: XMLEncoder generates invalid XML Reviewed-by: peterz
jdk/src/share/classes/java/beans/XMLEncoder.java
jdk/test/java/beans/XMLEncoder/Test5023550.java
jdk/test/java/beans/XMLEncoder/Test5023557.java
jdk/test/java/beans/XMLEncoder/Test6921644.java
--- a/jdk/src/share/classes/java/beans/XMLEncoder.java	Wed Mar 03 20:53:35 2010 +0300
+++ b/jdk/src/share/classes/java/beans/XMLEncoder.java	Thu Mar 04 21:17:03 2010 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2000-2010 Sun Microsystems, Inc.  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
@@ -214,8 +214,8 @@
     private Object owner;
     private int indentation = 0;
     private boolean internal = false;
-    private Map valueToExpression;
-    private Map targetToStatementList;
+    private Map<Object, ValueData> valueToExpression;
+    private Map<Object, List<Statement>> targetToStatementList;
     private boolean preambleWritten = false;
     private NameGenerator nameGenerator;
 
@@ -287,8 +287,8 @@
         this.declaration = declaration;
         this.indentation = indentation;
         this.out = new OutputStreamWriter(out, cs.newEncoder());
-        valueToExpression = new IdentityHashMap();
-        targetToStatementList = new IdentityHashMap();
+        valueToExpression = new IdentityHashMap<Object, ValueData>();
+        targetToStatementList = new IdentityHashMap<Object, List<Statement>>();
         nameGenerator = new NameGenerator();
     }
 
@@ -331,13 +331,12 @@
         }
     }
 
-    private Vector statementList(Object target) {
-        Vector list = (Vector)targetToStatementList.get(target);
-        if (list != null) {
-            return list;
+    private List<Statement> statementList(Object target) {
+        List<Statement> list = targetToStatementList.get(target);
+        if (list == null) {
+            list = new ArrayList<Statement>();
+            targetToStatementList.put(target, list);
         }
-        list = new Vector();
-        targetToStatementList.put(target, list);
         return list;
     }
 
@@ -363,13 +362,13 @@
         }
         d.marked = true;
         Object target = exp.getTarget();
+        mark(exp);
         if (!(target instanceof Class)) {
             statementList(target).add(exp);
             // Pending: Why does the reference count need to
             // be incremented here?
             d.refs++;
         }
-        mark(exp);
     }
 
     private void mark(Statement stm) {
@@ -463,9 +462,9 @@
             preambleWritten = true;
         }
         indentation++;
-        Vector roots = statementList(this);
-        for(int i = 0; i < roots.size(); i++) {
-            Statement s = (Statement)roots.get(i);
+        List<Statement> statements = statementList(this);
+        while (!statements.isEmpty()) {
+            Statement s = statements.remove(0);
             if ("writeObject".equals(s.getMethodName())) {
                 outputValue(s.getArguments()[0], this, true);
             }
@@ -513,7 +512,7 @@
     }
 
     private ValueData getValueData(Object o) {
-        ValueData d = (ValueData)valueToExpression.get(o);
+        ValueData d = valueToExpression.get(o);
         if (d == null) {
             d = new ValueData();
             valueToExpression.put(o, d);
@@ -619,11 +618,11 @@
         }
 
         if (d.name != null) {
-            writeln("<object idref=" + quote(d.name) + "/>");
-            return;
+            outputXML(isArgument ? "object" : "void", " idref=" + quote(d.name), value);
         }
-
-        outputStatement(d.exp, outer, isArgument);
+        else if (d.exp != null) {
+            outputStatement(d.exp, outer, isArgument);
+        }
     }
 
     private static String quoteCharCode(int code) {
@@ -683,13 +682,6 @@
         String tag = (expression && isArgument) ? "object" : "void";
         String attributes = "";
         ValueData d = getValueData(value);
-        if (expression) {
-            if (d.refs > 1) {
-                String instanceName = nameGenerator.instanceName(value);
-                d.name = instanceName;
-                attributes = attributes + " id=" + quote(instanceName);
-            }
-        }
 
         // Special cases for targets.
         if (target == outer) {
@@ -706,13 +698,19 @@
         else {
             d.refs = 2;
             getValueData(target).refs++;
+            List<Statement> statements = statementList(target);
+            if (!statements.contains(exp)) {
+                statements.add(exp);
+            }
             outputValue(target, outer, false);
-            if (isArgument) {
-                outputValue(value, outer, false);
-            }
+            outputValue(value, outer, isArgument);
             return;
         }
-
+        if (expression && (d.refs > 1)) {
+            String instanceName = nameGenerator.instanceName(value);
+            d.name = instanceName;
+            attributes = attributes + " id=" + quote(instanceName);
+        }
 
         // Special cases for methods.
         if ((!expression && methodName.equals("set") && args.length == 2 &&
@@ -730,8 +728,11 @@
         else if (!methodName.equals("new") && !methodName.equals("newInstance")) {
             attributes = attributes + " method=" + quote(methodName);
         }
+        outputXML(tag, attributes, value, args);
+    }
 
-        Vector statements = statementList(value);
+    private void outputXML(String tag, String attributes, Object value, Object... args) {
+        List<Statement> statements = statementList(value);
         // Use XML's short form when there is no body.
         if (args.length == 0 && statements.size() == 0) {
             writeln("<" + tag + attributes + "/>");
@@ -745,8 +746,8 @@
             outputValue(args[i], null, true);
         }
 
-        for(int i = 0; i < statements.size(); i++) {
-            Statement s = (Statement)statements.get(i);
+        while (!statements.isEmpty()) {
+            Statement s = statements.remove(0);
             outputStatement(s, value, false);
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/beans/XMLEncoder/Test5023550.java	Thu Mar 04 21:17:03 2010 +0300
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 5023550
+ * @summary Tests complex references to owner
+ * @author Sergey Malenkov
+ */
+
+import java.beans.DefaultPersistenceDelegate;
+import java.beans.Encoder;
+import java.beans.Expression;
+import java.beans.XMLDecoder;
+import java.beans.XMLEncoder;
+
+public class Test5023550 extends AbstractTest {
+    public static void main(String[] args) {
+        new Test5023550().test(true);
+    }
+
+    private final Owner owner = new Owner();
+
+    @Override
+    protected void initialize(XMLEncoder encoder) {
+        encoder.setOwner(this.owner);
+        encoder.setPersistenceDelegate(A.class, new ADelegate());
+        encoder.setPersistenceDelegate(B.class, new BDelegate());
+        encoder.setPersistenceDelegate(C.class, new CDelegate());
+    }
+
+    @Override
+    protected void initialize(XMLDecoder decoder) {
+        decoder.setOwner(this.owner);
+    }
+
+    protected Object getObject() {
+        return this.owner.newA(this.owner.newB().newC());
+    }
+
+    public static class Owner {
+        public A newA(C c) {
+            return new A(c);
+        }
+
+        public B newB() {
+            return new B();
+        }
+    }
+
+    public static class A {
+        private final C c;
+
+        private A(C c) {
+            this.c = c;
+        }
+
+        public C getC() {
+            return this.c;
+        }
+    }
+
+    public static class B {
+        public C newC() {
+            return new C(this);
+        }
+    }
+
+    public static class C {
+        private final B b;
+
+        private C(B b) {
+            this.b = b;
+        }
+
+        public B getB() {
+            return this.b;
+        }
+    }
+
+    public static class ADelegate extends DefaultPersistenceDelegate {
+        protected Expression instantiate(Object old, Encoder out) {
+            XMLEncoder encoder = (XMLEncoder) out;
+            A a = (A) old;
+            return new Expression(old, encoder.getOwner(), "newA", new Object[] { a.getC() });
+        }
+    }
+
+    public static class BDelegate extends DefaultPersistenceDelegate {
+        protected Expression instantiate(Object old, Encoder out) {
+            XMLEncoder encoder = (XMLEncoder) out;
+            return new Expression(old, encoder.getOwner(), "newB", new Object[0]);
+        }
+    }
+
+    public static class CDelegate extends DefaultPersistenceDelegate {
+        protected Expression instantiate(Object old, Encoder out) {
+            C c = (C) old;
+            return new Expression(c, c.getB(), "newC", new Object[0]);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/beans/XMLEncoder/Test5023557.java	Thu Mar 04 21:17:03 2010 +0300
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 5023557
+ * @summary Tests complex references
+ * @author Sergey Malenkov
+ */
+
+import java.beans.DefaultPersistenceDelegate;
+import java.beans.Encoder;
+import java.beans.Expression;
+import java.beans.XMLEncoder;
+
+public class Test5023557 extends AbstractTest {
+    public static void main(String[] args) {
+        new Test5023557().test(true);
+    }
+
+    @Override
+    protected void initialize(XMLEncoder encoder) {
+        encoder.setPersistenceDelegate(B.class, new BDelegate());
+        encoder.setPersistenceDelegate(C.class, new CDelegate());
+    }
+
+    protected Object getObject() {
+        A a = new A();
+        return a.newC(a.newB());
+    }
+
+    public static class A {
+        public B newB() {
+            return new B(this);
+        }
+
+        public C newC(B b) {
+            return new C(b);
+        }
+    }
+
+    public static class B {
+        private final A a;
+
+        private B(A a) {
+            this.a = a;
+        }
+
+        public A getA() {
+            return this.a;
+        }
+    }
+
+    public static class C {
+        private final B b;
+
+        private C(B b) {
+            this.b = b;
+        }
+
+        public B getB() {
+            return this.b;
+        }
+    }
+
+    public static class BDelegate extends DefaultPersistenceDelegate {
+        protected Expression instantiate(Object old, Encoder out) {
+            B b = (B) old;
+            return new Expression(b, b.getA(), "newB", new Object[0]);
+        }
+    }
+
+    public static class CDelegate extends DefaultPersistenceDelegate {
+        protected Expression instantiate(Object old, Encoder out) {
+            C c = (C) old;
+            return new Expression(c, c.getB().getA(), "newC", new Object[] { c.getB() });
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/beans/XMLEncoder/Test6921644.java	Thu Mar 04 21:17:03 2010 +0300
@@ -0,0 +1,197 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6921644
+ * @summary Tests references to cached integer
+ * @author Sergey Malenkov
+ */
+
+import java.beans.ConstructorProperties;
+import java.util.ArrayList;
+import java.util.List;
+
+public final class Test6921644 extends AbstractTest {
+    public static void main(String[] args) {
+        new Test6921644().test(true);
+    }
+
+    protected Object getObject() {
+        Owner<Author> o = new Owner<Author>(100); // it works if ID >= 128
+
+        Category c = new Category(o);
+
+        Document d1 = new Document(o);
+        Document d2 = new Document(o);
+        Document d3 = new Document(o);
+
+        Author a1 = new Author(o);
+        Author a2 = new Author(o);
+        Author a3 = new Author(o);
+
+        o.getList().add(a1);
+        o.getList().add(a2);
+        o.getList().add(a3);
+
+        a3.setRef(o.getId());
+
+        d2.setCategory(c);
+        d3.setCategory(c);
+
+        a1.addDocument(d1);
+        a1.addDocument(d2);
+        a3.addDocument(d3);
+
+        c.addDocument(d2);
+        c.addDocument(d3);
+
+        return o;
+    }
+
+    public static class Owner<T> {
+        private int id;
+        private List<T> list = new ArrayList<T>();
+
+        @ConstructorProperties("id")
+        public Owner(int id) {
+            this.id = id;
+        }
+
+        public int getId() {
+            return this.id;
+        }
+
+        public List<T> getList() {
+            return this.list;
+        }
+
+        public void setList(List<T> list) {
+            this.list = list;
+        }
+    }
+
+    public static class Author {
+        private int id;
+        private int ref;
+        private Owner owner;
+        private List<Document> list = new ArrayList<Document>();
+
+        @ConstructorProperties("owner")
+        public Author(Owner<Author> owner) {
+            this.owner = owner;
+            this.id = owner.getId();
+        }
+
+        public Owner getOwner() {
+            return this.owner;
+        }
+
+        public Integer getId() {
+            return this.id;
+        }
+
+        public void setId(Integer id) {
+            this.id = id;
+        }
+
+        public Integer getRef() {
+            return this.ref;
+        }
+
+        public void setRef(Integer ref) {
+            this.ref = ref;
+        }
+
+        public List<Document> getList() {
+            return this.list;
+        }
+
+        public void setList(List<Document> list) {
+            this.list = list;
+        }
+
+        public void addDocument(Document document) {
+            this.list.add(document);
+            document.setAuthor(this);
+        }
+    }
+
+    public static class Category {
+        private Owner owner;
+        private List<Document> list = new ArrayList<Document>();
+
+        @ConstructorProperties("owner")
+        public Category(Owner owner) {
+            this.owner = owner;
+        }
+
+        public Owner getOwner() {
+            return this.owner;
+        }
+
+        public List<Document> getList() {
+            return this.list;
+        }
+
+        public void setList(List<Document> list) {
+            this.list = list;
+        }
+
+        public void addDocument(Document document) {
+            this.list.add(document);
+            document.setCategory(this);
+        }
+    }
+
+    public static class Document {
+        private Owner owner;
+        private Author author;
+        private Category category;
+
+        @ConstructorProperties("owner")
+        public Document(Owner owner) {
+            this.owner = owner;
+        }
+
+        public Owner getOwner() {
+            return this.owner;
+        }
+
+        public Author getAuthor() {
+            return this.author;
+        }
+
+        public void setAuthor(Author author) {
+            this.author = author;
+        }
+
+        public Category getCategory() {
+            return this.category;
+        }
+
+        public void setCategory(Category category) {
+            this.category = category;
+        }
+    }
+}