Path: blob/master/test/jdk/java/beans/XMLEncoder/Test6187118.java
41149 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* @run main/othervm -Djava.security.manager=allow Test618711828* @author Sergey Malenkov29*/3031import java.beans.Encoder;32import java.beans.Expression;33import java.beans.PersistenceDelegate;34import java.beans.XMLEncoder;3536import java.util.ArrayList;37import java.util.Collections;38import java.util.Iterator;39import java.util.List;4041public final class Test6187118 extends AbstractTest {42public static void main(String[] args) {43new Test6187118().test(true);44}4546protected ImmutableList<String> getObject() {47return new ImmutableList<String>();48}4950protected ImmutableList<String> getAnotherObject() {51return new ImmutableList<String>().add("1").add("2").add("3").add("4");52}5354protected void initialize(XMLEncoder encoder) {55encoder.setPersistenceDelegate(56ImmutableList.class,57new PersistenceDelegate() {58protected boolean mutatesTo(Object oldInstance, Object newInstance) {59return oldInstance.equals(newInstance);60}6162protected Expression instantiate(Object oldInstance, Encoder out) {63ImmutableList list = (ImmutableList) oldInstance;64if (!list.hasEntries()) {65return getExpression(oldInstance, ImmutableList.class, "new");66}67Object object = list.getLast();68ImmutableList shortenedList = list.removeLast();69return getExpression(oldInstance, shortenedList, "add", object);70}7172private Expression getExpression(Object value, Object target, String method, Object... args) {73return new Expression(value, target, method, args);74}75}76);77}7879public static final class ImmutableList<T> implements Iterable {80private final List<T> list = new ArrayList<T>();8182public ImmutableList() {83}8485private ImmutableList(Iterable<T> iterable) {86for (T object : iterable) {87this.list.add(object);88}89}9091public Iterator<T> iterator() {92return Collections.unmodifiableList(this.list).iterator();93}9495public ImmutableList<T> add(T object) {96ImmutableList<T> list = new ImmutableList<T>(this.list);97list.list.add(object);98return list;99}100101public ImmutableList<T> removeLast() {102ImmutableList<T> list = new ImmutableList<T>(this.list);103int size = list.list.size();104if (0 < size) {105list.list.remove(size - 1);106}107return list;108}109110public T getLast() {111int size = this.list.size();112return (0 < size)113? this.list.get(size - 1)114: null;115}116117public boolean hasEntries() {118return 0 < this.list.size();119}120121public boolean equals(Object object) {122if (object instanceof ImmutableList) {123ImmutableList list = (ImmutableList) object;124return this.list.equals(list.list);125}126return false;127}128129public int hashCode() {130return this.list.hashCode();131}132133public String toString() {134return this.list.toString();135}136}137}138139140