Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/beans/XMLEncoder/Test4679556.java
38812 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* @author Sergey Malenkov, Mark Davidson, Philip Milne28*/2930import java.beans.DefaultPersistenceDelegate;31import java.beans.Encoder;32import java.beans.Expression;33import java.beans.XMLEncoder;3435/**36* Demonstrates the archiver bug, where the XMLEncoder duplicates37* the instance of class A because it is required as the target of38* a factory method (to produce an instance of class C).39* See the output in the file Test.xml for the results and note40* the (invalid) forward reference to the instance of class C.41*42* TO FIX43*44* Move the first line of the XMLEncoder::mark(Statement method)45* to the end of the method.46* I.e. replace the mark() method in XMLEncoder with this:47* <pre>private void mark(Statement stm) {48* Object[] args = stm.getArguments();49* for (int i = 0; i < args.length; i++) {50* Object arg = args[i];51* mark(arg, true);52* }53* mark(stm.getTarget(), false);54* }</pre>55*56* VALID ARCHIVE (WITH FIX):57* <pre><?xml version="1.0" encoding="UTF-8"?>58* <java version="1.4.0" class="java.beans.XMLDecoder">59* <object class="TestDuplicates$A">60* <void id="TestDuplicates$C0" method="createC"/>61* <void property="x">62* <void property="x">63* <object idref="TestDuplicates$C0"/>64* </void>65* </void>66* </object>67* </java></pre>68*69* INVALID ARCHIVE (WITHOUT FIX):70* <object class="TestDuplicates$A">71* <void property="x">72* <void property="x">73* <void class="TestDuplicates$A">74* <void property="x">75* <void property="x">76* <object idref="TestDuplicates$C0"/>77* </void>78* </void>79* <void id="TestDuplicates$C0" method="createC"/>80* </void>81* <object idref="TestDuplicates$C0"/>82* </void>83* </void>84* <void id="TestDuplicates$C0" method="createC"/>85* </object>86* </java></pre>87*/88public class Test4679556 extends AbstractTest {89public static void main(String[] args) {90new Test4679556().test(true);91}9293protected Object getObject() {94A a = new A();95B b = (B) a.getX();96b.setX(a.createC());97return a;98}99100protected Object getAnotherObject() {101return new A();102}103104protected void initialize(XMLEncoder encoder) {105encoder.setPersistenceDelegate(C.class, new DefaultPersistenceDelegate() {106protected Expression instantiate(Object oldInstance, Encoder out) {107C c = (C) oldInstance;108return new Expression(c, c.getX(), "createC", new Object[] {});109}110});111}112113public static class Base {114private Object x;115116public Object getX() {117return this.x;118}119120public void setX(Object x) {121this.x = x;122}123}124125public static class A extends Base {126public A() {127setX(new B());128}129130public C createC() {131return new C(this);132}133}134135public static class B extends Base {136}137138public static class C extends Base {139private C(Object x) {140setX(x);141}142}143}144145146