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