Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.sql.rowset/share/classes/javax/sql/rowset/serial/SerialJavaObject.java
40948 views
1
/*
2
* Copyright (c) 2003, 2021, 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
26
package javax.sql.rowset.serial;
27
28
import java.io.*;
29
import java.lang.reflect.*;
30
import java.util.Arrays;
31
import java.util.Vector;
32
import javax.sql.rowset.RowSetWarning;
33
import jdk.internal.reflect.CallerSensitive;
34
import jdk.internal.reflect.Reflection;
35
import sun.reflect.misc.ReflectUtil;
36
37
/**
38
* A serializable mapping in the Java programming language of an SQL
39
* <code>JAVA_OBJECT</code> value. Assuming the Java object
40
* implements the <code>Serializable</code> interface, this class simply wraps the
41
* serialization process.
42
* <P>
43
* If however, the serialization is not possible because
44
* the Java object is not immediately serializable, this class will
45
* attempt to serialize all non-static members to permit the object
46
* state to be serialized.
47
* Static or transient fields cannot be serialized; an attempt to serialize
48
* them will result in a <code>SerialException</code> object being thrown.
49
*
50
* <h2> Thread safety </h2>
51
*
52
* A SerialJavaObject is not safe for use by multiple concurrent threads. If a
53
* SerialJavaObject is to be used by more than one thread then access to the
54
* SerialJavaObject should be controlled by appropriate synchronization.
55
*
56
* @author Jonathan Bruce
57
* @since 1.5
58
*/
59
public class SerialJavaObject implements Serializable, Cloneable {
60
61
/**
62
* Placeholder for object to be serialized.
63
*/
64
@SuppressWarnings("serial") // Not statically typed as Serializable
65
private Object obj;
66
67
68
/**
69
* Placeholder for all fields in the <code>JavaObject</code> being serialized.
70
*/
71
private transient Field[] fields;
72
73
/**
74
* Constructor for <code>SerialJavaObject</code> helper class.
75
*
76
* @param obj the Java <code>Object</code> to be serialized
77
* @throws SerialException if the object is found not to be serializable
78
*/
79
public SerialJavaObject(Object obj) throws SerialException {
80
81
// if any static fields are found, an exception
82
// should be thrown
83
84
85
// get Class. Object instance should always be available
86
Class<?> c = obj.getClass();
87
88
// determine if object implements Serializable i/f
89
if (!(obj instanceof java.io.Serializable)) {
90
setWarning(new RowSetWarning("Warning, the object passed to the constructor does not implement Serializable"));
91
}
92
93
// can only determine public fields (obviously). If
94
// any of these are static, this should invalidate
95
// the action of attempting to persist these fields
96
// in a serialized form
97
fields = c.getFields();
98
99
if (hasStaticFields(fields)) {
100
throw new SerialException("Located static fields in " +
101
"object instance. Cannot serialize");
102
}
103
104
this.obj = obj;
105
}
106
107
/**
108
* Returns an <code>Object</code> that is a copy of this <code>SerialJavaObject</code>
109
* object.
110
*
111
* @return a copy of this <code>SerialJavaObject</code> object as an
112
* <code>Object</code> in the Java programming language
113
* @throws SerialException if the instance is corrupt
114
*/
115
public Object getObject() throws SerialException {
116
return this.obj;
117
}
118
119
/**
120
* Returns an array of <code>Field</code> objects that contains each
121
* field of the object that this helper class is serializing.
122
*
123
* @return an array of <code>Field</code> objects
124
* @throws SerialException if an error is encountered accessing
125
* the serialized object
126
* @throws SecurityException If a security manager, <i>s</i>, is present
127
* and the caller's class loader is not the same as or an
128
* ancestor of the class loader for the class of the
129
* {@linkplain #getObject object} being serialized
130
* and invocation of {@link SecurityManager#checkPackageAccess
131
* s.checkPackageAccess()} denies access to the package
132
* of that class.
133
* @see Class#getFields
134
*/
135
@CallerSensitive
136
public Field[] getFields() throws SerialException {
137
if (fields != null) {
138
Class<?> c = this.obj.getClass();
139
@SuppressWarnings("removal")
140
SecurityManager sm = System.getSecurityManager();
141
if (sm != null) {
142
/*
143
* Check if the caller is allowed to access the specified class's package.
144
* If access is denied, throw a SecurityException.
145
*/
146
Class<?> caller = Reflection.getCallerClass();
147
if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),
148
c.getClassLoader())) {
149
ReflectUtil.checkPackageAccess(c);
150
}
151
}
152
return c.getFields();
153
} else {
154
throw new SerialException("SerialJavaObject does not contain" +
155
" a serialized object instance");
156
}
157
}
158
159
/**
160
* The identifier that assists in the serialization of this
161
* <code>SerialJavaObject</code> object.
162
*/
163
static final long serialVersionUID = -1465795139032831023L;
164
165
/**
166
* A container for the warnings issued on this <code>SerialJavaObject</code>
167
* object. When there are multiple warnings, each warning is chained to the
168
* previous warning.
169
*/
170
Vector<RowSetWarning> chain;
171
172
/**
173
* Compares this SerialJavaObject to the specified object.
174
* The result is {@code true} if and only if the argument
175
* is not {@code null} and is a {@code SerialJavaObject}
176
* object that is identical to this object
177
*
178
* @param o The object to compare this {@code SerialJavaObject} against
179
*
180
* @return {@code true} if the given object represents a {@code SerialJavaObject}
181
* equivalent to this SerialJavaObject, {@code false} otherwise
182
*
183
*/
184
public boolean equals(Object o) {
185
if (this == o) {
186
return true;
187
}
188
if (o instanceof SerialJavaObject) {
189
SerialJavaObject sjo = (SerialJavaObject) o;
190
return obj.equals(sjo.obj);
191
}
192
return false;
193
}
194
195
/**
196
* Returns a hash code for this SerialJavaObject. The hash code for a
197
* {@code SerialJavaObject} object is taken as the hash code of
198
* the {@code Object} it stores
199
*
200
* @return a hash code value for this object.
201
*/
202
public int hashCode() {
203
return 31 + obj.hashCode();
204
}
205
206
/**
207
* Returns a clone of this {@code SerialJavaObject}.
208
*
209
* @return a clone of this SerialJavaObject
210
*/
211
212
public Object clone() {
213
try {
214
SerialJavaObject sjo = (SerialJavaObject) super.clone();
215
sjo.fields = Arrays.copyOf(fields, fields.length);
216
if (chain != null)
217
sjo.chain = new Vector<>(chain);
218
return sjo;
219
} catch (CloneNotSupportedException ex) {
220
// this shouldn't happen, since we are Cloneable
221
throw new InternalError();
222
}
223
}
224
225
/**
226
* Registers the given warning.
227
*/
228
private void setWarning(RowSetWarning e) {
229
if (chain == null) {
230
chain = new Vector<>();
231
}
232
chain.add(e);
233
}
234
235
/**
236
* readObject is called to restore the state of the {@code SerialJavaObject}
237
* from a stream.
238
* @param s the {@code ObjectInputStream} to read from.
239
*
240
* @throws ClassNotFoundException if the class of a serialized object
241
* could not be found.
242
* @throws IOException if an I/O error occurs.
243
*/
244
private void readObject(ObjectInputStream s)
245
throws IOException, ClassNotFoundException {
246
247
ObjectInputStream.GetField fields1 = s.readFields();
248
@SuppressWarnings("unchecked")
249
Vector<RowSetWarning> tmp = (Vector<RowSetWarning>)fields1.get("chain", null);
250
if (tmp != null)
251
chain = new Vector<>(tmp);
252
253
obj = fields1.get("obj", null);
254
if (obj != null) {
255
fields = obj.getClass().getFields();
256
if(hasStaticFields(fields))
257
throw new IOException("Located static fields in " +
258
"object instance. Cannot serialize");
259
} else {
260
throw new IOException("Object cannot be null!");
261
}
262
263
}
264
265
/**
266
* writeObject is called to save the state of the {@code SerialJavaObject}
267
* to a stream.
268
* @param s the {@code ObjectOutputStream} to write to.
269
* @throws IOException if an I/O error occurs.
270
*/
271
private void writeObject(ObjectOutputStream s)
272
throws IOException {
273
ObjectOutputStream.PutField fields = s.putFields();
274
fields.put("obj", obj);
275
fields.put("chain", chain);
276
s.writeFields();
277
}
278
279
/*
280
* Check to see if there are any Static Fields in this object
281
*/
282
private static boolean hasStaticFields(Field[] fields) {
283
for (Field field : fields) {
284
if ( field.getModifiers() == Modifier.STATIC) {
285
return true;
286
}
287
}
288
return false;
289
}
290
}
291
292