Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/java/beans/Encoder.java
41152 views
1
/*
2
* Copyright (c) 2000, 2020, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package java.beans;
26
27
import com.sun.beans.finder.PersistenceDelegateFinder;
28
29
import java.util.HashMap;
30
import java.util.IdentityHashMap;
31
import java.util.Map;
32
33
/**
34
* An {@code Encoder} is a class which can be used to create
35
* files or streams that encode the state of a collection of
36
* JavaBeans in terms of their public APIs. The {@code Encoder},
37
* in conjunction with its persistence delegates, is responsible for
38
* breaking the object graph down into a series of {@code Statement}s
39
* and {@code Expression}s which can be used to create it.
40
* A subclass typically provides a syntax for these expressions
41
* using some human readable form - like Java source code or XML.
42
*
43
* @since 1.4
44
*
45
* @author Philip Milne
46
*/
47
48
public class Encoder {
49
private final PersistenceDelegateFinder finder = new PersistenceDelegateFinder();
50
private Map<Object, Expression> bindings = new IdentityHashMap<>();
51
private ExceptionListener exceptionListener;
52
boolean executeStatements = true;
53
private Map<Object, Object> attributes;
54
55
/**
56
* Constructs an {@code Encoder}.
57
*/
58
public Encoder() {}
59
60
/**
61
* Write the specified object to the output stream.
62
* The serialized form will denote a series of
63
* expressions, the combined effect of which will create
64
* an equivalent object when the input stream is read.
65
* By default, the object is assumed to be a <em>JavaBean</em>
66
* with a nullary constructor, whose state is defined by
67
* the matching pairs of "setter" and "getter" methods
68
* returned by the Introspector.
69
*
70
* @param o The object to be written to the stream.
71
*
72
* @see XMLDecoder#readObject
73
*/
74
protected void writeObject(Object o) {
75
if (o == this) {
76
return;
77
}
78
PersistenceDelegate info = getPersistenceDelegate(o == null ? null : o.getClass());
79
info.writeObject(o, this);
80
}
81
82
/**
83
* Sets the exception handler for this stream to {@code exceptionListener}.
84
* The exception handler is notified when this stream catches recoverable
85
* exceptions.
86
*
87
* @param exceptionListener The exception handler for this stream;
88
* if {@code null} the default exception listener will be used.
89
*
90
* @see #getExceptionListener
91
*/
92
public void setExceptionListener(ExceptionListener exceptionListener) {
93
this.exceptionListener = exceptionListener;
94
}
95
96
/**
97
* Gets the exception handler for this stream.
98
*
99
* @return The exception handler for this stream;
100
* Will return the default exception listener if this has not explicitly been set.
101
*
102
* @see #setExceptionListener
103
*/
104
public ExceptionListener getExceptionListener() {
105
return (exceptionListener != null) ? exceptionListener : Statement.defaultExceptionListener;
106
}
107
108
Object getValue(Expression exp) {
109
try {
110
return (exp == null) ? null : exp.getValue();
111
}
112
catch (Exception e) {
113
getExceptionListener().exceptionThrown(e);
114
throw new RuntimeException("failed to evaluate: " + exp.toString());
115
}
116
}
117
118
/**
119
* Returns the persistence delegate for the given type.
120
* The persistence delegate is calculated by applying
121
* the following rules in order:
122
* <ol>
123
* <li>
124
* If a persistence delegate is associated with the given type
125
* by using the {@link #setPersistenceDelegate} method
126
* it is returned.
127
* <li>
128
* A persistence delegate is then looked up by the name
129
* composed of the fully qualified name of the given type
130
* and the "PersistenceDelegate" postfix.
131
* For example, a persistence delegate for the {@code Bean} class
132
* should be named {@code BeanPersistenceDelegate}
133
* and located in the same package.
134
* <pre>
135
* public class Bean { ... }
136
* public class BeanPersistenceDelegate { ... }</pre>
137
* The instance of the {@code BeanPersistenceDelegate} class
138
* is returned for the {@code Bean} class.
139
* <li>
140
* If the type is {@code null},
141
* a shared internal persistence delegate is returned
142
* that encodes {@code null} value.
143
* <li>
144
* If the type is an {@code enum} declaration,
145
* a shared internal persistence delegate is returned
146
* that encodes constants of this enumeration
147
* by their names.
148
* <li>
149
* If the type is a primitive type or the corresponding wrapper,
150
* a shared internal persistence delegate is returned
151
* that encodes values of the given type.
152
* <li>
153
* If the type is an array,
154
* a shared internal persistence delegate is returned
155
* that encodes an array of the appropriate type and length,
156
* and each of its elements as if they are properties.
157
* <li>
158
* If the type is a proxy,
159
* a shared internal persistence delegate is returned
160
* that encodes a proxy instance by using
161
* the {@link java.lang.reflect.Proxy#newProxyInstance} method.
162
* <li>
163
* If the {@link BeanInfo} for this type has a {@link BeanDescriptor}
164
* which defined a "persistenceDelegate" attribute,
165
* the value of this named attribute is returned.
166
* <li>
167
* In all other cases the default persistence delegate is returned.
168
* The default persistence delegate assumes the type is a <em>JavaBean</em>,
169
* implying that it has a default constructor and that its state
170
* may be characterized by the matching pairs of "setter" and "getter"
171
* methods returned by the {@link Introspector} class.
172
* The default constructor is the constructor with the greatest number
173
* of parameters that has the {@link ConstructorProperties} annotation.
174
* If none of the constructors has the {@code ConstructorProperties} annotation,
175
* then the nullary constructor (constructor with no parameters) will be used.
176
* For example, in the following code fragment, the nullary constructor
177
* for the {@code Foo} class will be used,
178
* while the two-parameter constructor
179
* for the {@code Bar} class will be used.
180
* <pre>
181
* public class Foo {
182
* public Foo() { ... }
183
* public Foo(int x) { ... }
184
* }
185
* public class Bar {
186
* public Bar() { ... }
187
* &#64;ConstructorProperties({"x"})
188
* public Bar(int x) { ... }
189
* &#64;ConstructorProperties({"x", "y"})
190
* public Bar(int x, int y) { ... }
191
* }</pre>
192
* </ol>
193
*
194
* @param type the class of the objects
195
* @return the persistence delegate for the given type
196
*
197
* @see #setPersistenceDelegate
198
* @see java.beans.Introspector#getBeanInfo
199
* @see java.beans.BeanInfo#getBeanDescriptor
200
*/
201
public PersistenceDelegate getPersistenceDelegate(Class<?> type) {
202
PersistenceDelegate pd = this.finder.find(type);
203
if (pd == null) {
204
pd = MetaData.getPersistenceDelegate(type);
205
if (pd != null) {
206
this.finder.register(type, pd);
207
}
208
}
209
return pd;
210
}
211
212
/**
213
* Associates the specified persistence delegate with the given type.
214
*
215
* @param type the class of objects that the specified persistence delegate applies to
216
* @param delegate the persistence delegate for instances of the given type
217
*
218
* @see #getPersistenceDelegate
219
* @see java.beans.Introspector#getBeanInfo
220
* @see java.beans.BeanInfo#getBeanDescriptor
221
*/
222
public void setPersistenceDelegate(Class<?> type, PersistenceDelegate delegate) {
223
this.finder.register(type, delegate);
224
}
225
226
/**
227
* Removes the entry for this instance, returning the old entry.
228
*
229
* @param oldInstance The entry that should be removed.
230
* @return The entry that was removed.
231
*
232
* @see #get
233
*/
234
public Object remove(Object oldInstance) {
235
Expression exp = bindings.remove(oldInstance);
236
return getValue(exp);
237
}
238
239
/**
240
* Returns a tentative value for {@code oldInstance} in
241
* the environment created by this stream. A persistence
242
* delegate can use its {@code mutatesTo} method to
243
* determine whether this value may be initialized to
244
* form the equivalent object at the output or whether
245
* a new object must be instantiated afresh. If the
246
* stream has not yet seen this value, null is returned.
247
*
248
* @param oldInstance The instance to be looked up.
249
* @return The object, null if the object has not been seen before.
250
*/
251
public Object get(Object oldInstance) {
252
if (oldInstance == null || oldInstance == this ||
253
oldInstance.getClass() == String.class) {
254
return oldInstance;
255
}
256
Expression exp = bindings.get(oldInstance);
257
return getValue(exp);
258
}
259
260
private Object writeObject1(Object oldInstance) {
261
Object o = get(oldInstance);
262
if (o == null) {
263
writeObject(oldInstance);
264
o = get(oldInstance);
265
}
266
return o;
267
}
268
269
private Statement cloneStatement(Statement oldExp) {
270
Object oldTarget = oldExp.getTarget();
271
Object newTarget = writeObject1(oldTarget);
272
273
Object[] oldArgs = oldExp.getArguments();
274
Object[] newArgs = new Object[oldArgs.length];
275
for (int i = 0; i < oldArgs.length; i++) {
276
newArgs[i] = writeObject1(oldArgs[i]);
277
}
278
Statement newExp = Statement.class.equals(oldExp.getClass())
279
? new Statement(newTarget, oldExp.getMethodName(), newArgs)
280
: new Expression(newTarget, oldExp.getMethodName(), newArgs);
281
newExp.loader = oldExp.loader;
282
return newExp;
283
}
284
285
/**
286
* Writes statement {@code oldStm} to the stream.
287
* The {@code oldStm} should be written entirely
288
* in terms of the callers environment, i.e. the
289
* target and all arguments should be part of the
290
* object graph being written. These expressions
291
* represent a series of "what happened" expressions
292
* which tell the output stream how to produce an
293
* object graph like the original.
294
* <p>
295
* The implementation of this method will produce
296
* a second expression to represent the same expression in
297
* an environment that will exist when the stream is read.
298
* This is achieved simply by calling {@code writeObject}
299
* on the target and all the arguments and building a new
300
* expression with the results.
301
*
302
* @param oldStm The expression to be written to the stream.
303
*/
304
public void writeStatement(Statement oldStm) {
305
// System.out.println("writeStatement: " + oldExp);
306
Statement newStm = cloneStatement(oldStm);
307
if (oldStm.getTarget() != this && executeStatements) {
308
try {
309
newStm.execute();
310
} catch (Exception e) {
311
getExceptionListener().exceptionThrown(new Exception("Encoder: discarding statement "
312
+ newStm, e));
313
}
314
}
315
}
316
317
/**
318
* The implementation first checks to see if an
319
* expression with this value has already been written.
320
* If not, the expression is cloned, using
321
* the same procedure as {@code writeStatement},
322
* and the value of this expression is reconciled
323
* with the value of the cloned expression
324
* by calling {@code writeObject}.
325
*
326
* @param oldExp The expression to be written to the stream.
327
*/
328
public void writeExpression(Expression oldExp) {
329
// System.out.println("Encoder::writeExpression: " + oldExp);
330
Object oldValue = getValue(oldExp);
331
if (get(oldValue) != null) {
332
return;
333
}
334
bindings.put(oldValue, (Expression)cloneStatement(oldExp));
335
writeObject(oldValue);
336
}
337
338
void clear() {
339
bindings.clear();
340
}
341
342
// Package private method for setting an attributes table for the encoder
343
void setAttribute(Object key, Object value) {
344
if (attributes == null) {
345
attributes = new HashMap<>();
346
}
347
attributes.put(key, value);
348
}
349
350
Object getAttribute(Object key) {
351
if (attributes == null) {
352
return null;
353
}
354
return attributes.get(key);
355
}
356
}
357
358