Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/beans/XMLEncoder/Test6187118.java
38812 views
/*1* Copyright (c) 2007, 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 618711826* @summary Tests encoding of immutable list that creates itself27* @author Sergey Malenkov28*/2930import java.beans.Encoder;31import java.beans.Expression;32import java.beans.PersistenceDelegate;33import java.beans.XMLEncoder;3435import java.util.ArrayList;36import java.util.Collections;37import java.util.Iterator;38import java.util.List;3940public final class Test6187118 extends AbstractTest {41public static void main(String[] args) {42new Test6187118().test(true);43}4445protected ImmutableList<String> getObject() {46return new ImmutableList<String>();47}4849protected ImmutableList<String> getAnotherObject() {50return new ImmutableList<String>().add("1").add("2").add("3").add("4");51}5253protected void initialize(XMLEncoder encoder) {54encoder.setPersistenceDelegate(55ImmutableList.class,56new PersistenceDelegate() {57protected boolean mutatesTo(Object oldInstance, Object newInstance) {58return oldInstance.equals(newInstance);59}6061protected Expression instantiate(Object oldInstance, Encoder out) {62ImmutableList list = (ImmutableList) oldInstance;63if (!list.hasEntries()) {64return getExpression(oldInstance, ImmutableList.class, "new");65}66Object object = list.getLast();67ImmutableList shortenedList = list.removeLast();68return getExpression(oldInstance, shortenedList, "add", object);69}7071private Expression getExpression(Object value, Object target, String method, Object... args) {72return new Expression(value, target, method, args);73}74}75);76}7778public static final class ImmutableList<T> implements Iterable {79private final List<T> list = new ArrayList<T>();8081public ImmutableList() {82}8384private ImmutableList(Iterable<T> iterable) {85for (T object : iterable) {86this.list.add(object);87}88}8990public Iterator<T> iterator() {91return Collections.unmodifiableList(this.list).iterator();92}9394public ImmutableList<T> add(T object) {95ImmutableList<T> list = new ImmutableList<T>(this.list);96list.list.add(object);97return list;98}99100public ImmutableList<T> removeLast() {101ImmutableList<T> list = new ImmutableList<T>(this.list);102int size = list.list.size();103if (0 < size) {104list.list.remove(size - 1);105}106return list;107}108109public T getLast() {110int size = this.list.size();111return (0 < size)112? this.list.get(size - 1)113: null;114}115116public boolean hasEntries() {117return 0 < this.list.size();118}119120public boolean equals(Object object) {121if (object instanceof ImmutableList) {122ImmutableList list = (ImmutableList) object;123return this.list.equals(list.list);124}125return false;126}127128public int hashCode() {129return this.list.hashCode();130}131132public String toString() {133return this.list.toString();134}135}136}137138139