Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/beans/XMLEncoder/Test6187118.java
41149 views
1
/*
2
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 6187118
27
* @summary Tests encoding of immutable list that creates itself
28
* @run main/othervm -Djava.security.manager=allow Test6187118
29
* @author Sergey Malenkov
30
*/
31
32
import java.beans.Encoder;
33
import java.beans.Expression;
34
import java.beans.PersistenceDelegate;
35
import java.beans.XMLEncoder;
36
37
import java.util.ArrayList;
38
import java.util.Collections;
39
import java.util.Iterator;
40
import java.util.List;
41
42
public final class Test6187118 extends AbstractTest {
43
public static void main(String[] args) {
44
new Test6187118().test(true);
45
}
46
47
protected ImmutableList<String> getObject() {
48
return new ImmutableList<String>();
49
}
50
51
protected ImmutableList<String> getAnotherObject() {
52
return new ImmutableList<String>().add("1").add("2").add("3").add("4");
53
}
54
55
protected void initialize(XMLEncoder encoder) {
56
encoder.setPersistenceDelegate(
57
ImmutableList.class,
58
new PersistenceDelegate() {
59
protected boolean mutatesTo(Object oldInstance, Object newInstance) {
60
return oldInstance.equals(newInstance);
61
}
62
63
protected Expression instantiate(Object oldInstance, Encoder out) {
64
ImmutableList list = (ImmutableList) oldInstance;
65
if (!list.hasEntries()) {
66
return getExpression(oldInstance, ImmutableList.class, "new");
67
}
68
Object object = list.getLast();
69
ImmutableList shortenedList = list.removeLast();
70
return getExpression(oldInstance, shortenedList, "add", object);
71
}
72
73
private Expression getExpression(Object value, Object target, String method, Object... args) {
74
return new Expression(value, target, method, args);
75
}
76
}
77
);
78
}
79
80
public static final class ImmutableList<T> implements Iterable {
81
private final List<T> list = new ArrayList<T>();
82
83
public ImmutableList() {
84
}
85
86
private ImmutableList(Iterable<T> iterable) {
87
for (T object : iterable) {
88
this.list.add(object);
89
}
90
}
91
92
public Iterator<T> iterator() {
93
return Collections.unmodifiableList(this.list).iterator();
94
}
95
96
public ImmutableList<T> add(T object) {
97
ImmutableList<T> list = new ImmutableList<T>(this.list);
98
list.list.add(object);
99
return list;
100
}
101
102
public ImmutableList<T> removeLast() {
103
ImmutableList<T> list = new ImmutableList<T>(this.list);
104
int size = list.list.size();
105
if (0 < size) {
106
list.list.remove(size - 1);
107
}
108
return list;
109
}
110
111
public T getLast() {
112
int size = this.list.size();
113
return (0 < size)
114
? this.list.get(size - 1)
115
: null;
116
}
117
118
public boolean hasEntries() {
119
return 0 < this.list.size();
120
}
121
122
public boolean equals(Object object) {
123
if (object instanceof ImmutableList) {
124
ImmutableList list = (ImmutableList) object;
125
return this.list.equals(list.list);
126
}
127
return false;
128
}
129
130
public int hashCode() {
131
return this.list.hashCode();
132
}
133
134
public String toString() {
135
return this.list.toString();
136
}
137
}
138
}
139
140