Path: blob/master/test/jdk/java/beans/XMLEncoder/Test4679556.java
41152 views
/*1* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 467955626* @summary Tests for duplication of some kind instances27* @run main/othervm -Djava.security.manager=allow Test467955628* @author Sergey Malenkov, Mark Davidson, Philip Milne29*/3031import java.beans.DefaultPersistenceDelegate;32import java.beans.Encoder;33import java.beans.Expression;34import java.beans.XMLEncoder;3536/**37* Demonstrates the archiver bug, where the XMLEncoder duplicates38* the instance of class A because it is required as the target of39* a factory method (to produce an instance of class C).40* See the output in the file Test.xml for the results and note41* the (invalid) forward reference to the instance of class C.42*43* TO FIX44*45* Move the first line of the XMLEncoder::mark(Statement method)46* to the end of the method.47* I.e. replace the mark() method in XMLEncoder with this:48* <pre>private void mark(Statement stm) {49* Object[] args = stm.getArguments();50* for (int i = 0; i < args.length; i++) {51* Object arg = args[i];52* mark(arg, true);53* }54* mark(stm.getTarget(), false);55* }</pre>56*57* VALID ARCHIVE (WITH FIX):58* <pre><?xml version="1.0" encoding="UTF-8"?>59* <java version="1.4.0" class="java.beans.XMLDecoder">60* <object class="TestDuplicates$A">61* <void id="TestDuplicates$C0" method="createC"/>62* <void property="x">63* <void property="x">64* <object idref="TestDuplicates$C0"/>65* </void>66* </void>67* </object>68* </java></pre>69*70* INVALID ARCHIVE (WITHOUT FIX):71* <object class="TestDuplicates$A">72* <void property="x">73* <void property="x">74* <void class="TestDuplicates$A">75* <void property="x">76* <void property="x">77* <object idref="TestDuplicates$C0"/>78* </void>79* </void>80* <void id="TestDuplicates$C0" method="createC"/>81* </void>82* <object idref="TestDuplicates$C0"/>83* </void>84* </void>85* <void id="TestDuplicates$C0" method="createC"/>86* </object>87* </java></pre>88*/89public class Test4679556 extends AbstractTest {90public static void main(String[] args) {91new Test4679556().test(true);92}9394protected Object getObject() {95A a = new A();96B b = (B) a.getX();97b.setX(a.createC());98return a;99}100101protected Object getAnotherObject() {102return new A();103}104105protected void initialize(XMLEncoder encoder) {106encoder.setPersistenceDelegate(C.class, new DefaultPersistenceDelegate() {107protected Expression instantiate(Object oldInstance, Encoder out) {108C c = (C) oldInstance;109return new Expression(c, c.getX(), "createC", new Object[] {});110}111});112}113114public static class Base {115private Object x;116117public Object getX() {118return this.x;119}120121public void setX(Object x) {122this.x = x;123}124}125126public static class A extends Base {127public A() {128setX(new B());129}130131public C createC() {132return new C(this);133}134}135136public static class B extends Base {137}138139public static class C extends Base {140private C(Object x) {141setX(x);142}143}144}145146147