Path: blob/master/test/jdk/java/beans/XMLEncoder/Test5023550.java
41149 views
/*1* Copyright (c) 2010, 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 502355026* @summary Tests complex references to owner27* @run main/othervm -Djava.security.manager=allow Test502355028* @author Sergey Malenkov29*/3031import java.beans.DefaultPersistenceDelegate;32import java.beans.Encoder;33import java.beans.Expression;34import java.beans.XMLDecoder;35import java.beans.XMLEncoder;3637public class Test5023550 extends AbstractTest {38public static void main(String[] args) {39new Test5023550().test(true);40}4142private final Owner owner = new Owner();4344@Override45protected void initialize(XMLEncoder encoder) {46encoder.setOwner(this.owner);47encoder.setPersistenceDelegate(A.class, new ADelegate());48encoder.setPersistenceDelegate(B.class, new BDelegate());49encoder.setPersistenceDelegate(C.class, new CDelegate());50}5152@Override53protected void initialize(XMLDecoder decoder) {54decoder.setOwner(this.owner);55}5657protected Object getObject() {58return this.owner.newA(this.owner.newB().newC());59}6061public static class Owner {62public A newA(C c) {63return new A(c);64}6566public B newB() {67return new B();68}69}7071public static class A {72private final C c;7374private A(C c) {75this.c = c;76}7778public C getC() {79return this.c;80}81}8283public static class B {84public C newC() {85return new C(this);86}87}8889public static class C {90private final B b;9192private C(B b) {93this.b = b;94}9596public B getB() {97return this.b;98}99}100101public static class ADelegate extends DefaultPersistenceDelegate {102protected Expression instantiate(Object old, Encoder out) {103XMLEncoder encoder = (XMLEncoder) out;104A a = (A) old;105return new Expression(old, encoder.getOwner(), "newA", new Object[] { a.getC() });106}107}108109public static class BDelegate extends DefaultPersistenceDelegate {110protected Expression instantiate(Object old, Encoder out) {111XMLEncoder encoder = (XMLEncoder) out;112return new Expression(old, encoder.getOwner(), "newB", new Object[0]);113}114}115116public static class CDelegate extends DefaultPersistenceDelegate {117protected Expression instantiate(Object old, Encoder out) {118C c = (C) old;119return new Expression(c, c.getB(), "newC", new Object[0]);120}121}122}123124125