Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sql/rowset/serial/SerialJavaObject.java
38918 views
/*1* Copyright (c) 2003, 2013, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sql.rowset.serial;2627import java.io.*;28import java.lang.reflect.*;29import java.util.Arrays;30import java.util.Vector;31import javax.sql.rowset.RowSetWarning;32import sun.reflect.CallerSensitive;33import sun.reflect.Reflection;34import sun.reflect.misc.ReflectUtil;3536/**37* A serializable mapping in the Java programming language of an SQL38* <code>JAVA_OBJECT</code> value. Assuming the Java object39* implements the <code>Serializable</code> interface, this class simply wraps the40* serialization process.41* <P>42* If however, the serialization is not possible because43* the Java object is not immediately serializable, this class will44* attempt to serialize all non-static members to permit the object45* state to be serialized.46* Static or transient fields cannot be serialized; an attempt to serialize47* them will result in a <code>SerialException</code> object being thrown.48*49* <h3> Thread safety </h3>50*51* A SerialJavaObject is not safe for use by multiple concurrent threads. If a52* SerialJavaObject is to be used by more than one thread then access to the53* SerialJavaObject should be controlled by appropriate synchronization.54*55* @author Jonathan Bruce56*/57public class SerialJavaObject implements Serializable, Cloneable {5859/**60* Placeholder for object to be serialized.61*/62private Object obj;636465/**66* Placeholder for all fields in the <code>JavaObject</code> being serialized.67*/68private transient Field[] fields;6970/**71* Constructor for <code>SerialJavaObject</code> helper class.72* <p>73*74* @param obj the Java <code>Object</code> to be serialized75* @throws SerialException if the object is found not to be serializable76*/77public SerialJavaObject(Object obj) throws SerialException {7879// if any static fields are found, an exception80// should be thrown818283// get Class. Object instance should always be available84Class<?> c = obj.getClass();8586// determine if object implements Serializable i/f87if (!(obj instanceof java.io.Serializable)) {88setWarning(new RowSetWarning("Warning, the object passed to the constructor does not implement Serializable"));89}9091// can only determine public fields (obviously). If92// any of these are static, this should invalidate93// the action of attempting to persist these fields94// in a serialized form95fields = c.getFields();9697if (hasStaticFields(fields)) {98throw new SerialException("Located static fields in " +99"object instance. Cannot serialize");100}101102this.obj = obj;103}104105/**106* Returns an <code>Object</code> that is a copy of this <code>SerialJavaObject</code>107* object.108*109* @return a copy of this <code>SerialJavaObject</code> object as an110* <code>Object</code> in the Java programming language111* @throws SerialException if the instance is corrupt112*/113public Object getObject() throws SerialException {114return this.obj;115}116117/**118* Returns an array of <code>Field</code> objects that contains each119* field of the object that this helper class is serializing.120*121* @return an array of <code>Field</code> objects122* @throws SerialException if an error is encountered accessing123* the serialized object124* @throws SecurityException If a security manager, <i>s</i>, is present125* and the caller's class loader is not the same as or an126* ancestor of the class loader for the class of the127* {@linkplain #getObject object} being serialized128* and invocation of {@link SecurityManager#checkPackageAccess129* s.checkPackageAccess()} denies access to the package130* of that class.131* @see Class#getFields132*/133@CallerSensitive134public Field[] getFields() throws SerialException {135if (fields != null) {136Class<?> c = this.obj.getClass();137SecurityManager sm = System.getSecurityManager();138if (sm != null) {139/*140* Check if the caller is allowed to access the specified class's package.141* If access is denied, throw a SecurityException.142*/143Class<?> caller = sun.reflect.Reflection.getCallerClass();144if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),145c.getClassLoader())) {146ReflectUtil.checkPackageAccess(c);147}148}149return c.getFields();150} else {151throw new SerialException("SerialJavaObject does not contain" +152" a serialized object instance");153}154}155156/**157* The identifier that assists in the serialization of this158* <code>SerialJavaObject</code> object.159*/160static final long serialVersionUID = -1465795139032831023L;161162/**163* A container for the warnings issued on this <code>SerialJavaObject</code>164* object. When there are multiple warnings, each warning is chained to the165* previous warning.166*/167Vector<RowSetWarning> chain;168169/**170* Compares this SerialJavaObject to the specified object.171* The result is {@code true} if and only if the argument172* is not {@code null} and is a {@code SerialJavaObject}173* object that is identical to this object174*175* @param o The object to compare this {@code SerialJavaObject} against176*177* @return {@code true} if the given object represents a {@code SerialJavaObject}178* equivalent to this SerialJavaObject, {@code false} otherwise179*180*/181public boolean equals(Object o) {182if (this == o) {183return true;184}185if (o instanceof SerialJavaObject) {186SerialJavaObject sjo = (SerialJavaObject) o;187return obj.equals(sjo.obj);188}189return false;190}191192/**193* Returns a hash code for this SerialJavaObject. The hash code for a194* {@code SerialJavaObject} object is taken as the hash code of195* the {@code Object} it stores196*197* @return a hash code value for this object.198*/199public int hashCode() {200return 31 + obj.hashCode();201}202203/**204* Returns a clone of this {@code SerialJavaObject}.205*206* @return a clone of this SerialJavaObject207*/208209public Object clone() {210try {211SerialJavaObject sjo = (SerialJavaObject) super.clone();212sjo.fields = Arrays.copyOf(fields, fields.length);213if (chain != null)214sjo.chain = new Vector<>(chain);215return sjo;216} catch (CloneNotSupportedException ex) {217// this shouldn't happen, since we are Cloneable218throw new InternalError();219}220}221222/**223* Registers the given warning.224*/225private void setWarning(RowSetWarning e) {226if (chain == null) {227chain = new Vector<>();228}229chain.add(e);230}231232/**233* readObject is called to restore the state of the {@code SerialJavaObject}234* from a stream.235*/236private void readObject(ObjectInputStream s)237throws IOException, ClassNotFoundException {238239ObjectInputStream.GetField fields1 = s.readFields();240@SuppressWarnings("unchecked")241Vector<RowSetWarning> tmp = (Vector<RowSetWarning>)fields1.get("chain", null);242if (tmp != null)243chain = new Vector<>(tmp);244245obj = fields1.get("obj", null);246if (obj != null) {247fields = obj.getClass().getFields();248if(hasStaticFields(fields))249throw new IOException("Located static fields in " +250"object instance. Cannot serialize");251} else {252throw new IOException("Object cannot be null!");253}254255}256257/**258* writeObject is called to save the state of the {@code SerialJavaObject}259* to a stream.260*/261private void writeObject(ObjectOutputStream s)262throws IOException {263ObjectOutputStream.PutField fields = s.putFields();264fields.put("obj", obj);265fields.put("chain", chain);266s.writeFields();267}268269/*270* Check to see if there are any Static Fields in this object271*/272private static boolean hasStaticFields(Field[] fields) {273for (Field field : fields) {274if ( field.getModifiers() == Modifier.STATIC) {275return true;276}277}278return false;279}280}281282283