objectstreamclass.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 2,026 行 · 第 1/5 页

JAVA
2,026
字号
	    // field mismatches impossible when matching local fields vs. self	    throw new InternalError();	}	//	if (cons == null) { // - Back-ported from JDK 1.4 	if (initClass == null) {	    deserializeEx = new InvalidClassException(		name, "no valid constructor");	}	for (int i = 0; i < fields.length; i++) {	    if (fields[i].getField() == null) {		defaultSerializeEx = new InvalidClassException(		    name, "unmatched serializable field(s) declared");	    }	}    }    /**     * Creates blank class descriptor which should be initialized via a     * subsequent call to initProxy(), initNonProxy() or readNonProxy().     */    ObjectStreamClass() {    }        /**     * Initializes class descriptor representing a proxy class.     */    void initProxy(Class cl, 		   ClassNotFoundException resolveEx, 		   ObjectStreamClass superDesc) 	throws InvalidClassException    {	this.cl = cl;	this.resolveEx = resolveEx;	this.superDesc = superDesc;	isProxy = true;	serializable = true;	suid = new Long(0);	fields = NO_FIELDS;		if (cl != null) {	    localDesc = lookup(cl, true);	    if (!localDesc.isProxy) {		throw new InvalidClassException(		    "cannot bind proxy descriptor to a non-proxy class");	    }	    name = localDesc.name;	    externalizable = localDesc.externalizable;	    // cons = localDesc.cons; // - Back-ported from JDK 1.4 	    initClass = localDesc.initClass;	    writeReplaceMethod = localDesc.writeReplaceMethod;	    readResolveMethod = localDesc.readResolveMethod;	    deserializeEx = localDesc.deserializeEx;	}	fieldRefl = getReflector(fields, localDesc);    }        /**     * Initializes class descriptor representing a non-proxy class.     */    void initNonProxy(ObjectStreamClass model, 		      Class cl, 		      ClassNotFoundException resolveEx,		      ObjectStreamClass superDesc)	throws InvalidClassException    {	this.cl = cl;	this.resolveEx = resolveEx;	this.superDesc = superDesc;	name = model.name;	suid = new Long(model.getSerialVersionUID());	isProxy = false;	serializable = model.serializable;	externalizable = model.externalizable;	hasBlockExternalData = model.hasBlockExternalData;	hasWriteObjectData = model.hasWriteObjectData;	fields = model.fields;	primDataSize = model.primDataSize;	numObjFields = model.numObjFields;		if (cl != null) {	    localDesc = lookup(cl, true);	    if (localDesc.isProxy) {		throw new InvalidClassException(		    "cannot bind non-proxy descriptor to a proxy class");	    }	    	    if (serializable == localDesc.serializable &&		!cl.isArray() &&		suid.longValue() != localDesc.getSerialVersionUID())	    {		throw new InvalidClassException(localDesc.name, 		    "local class incompatible: " +		    "stream classdesc serialVersionUID = " + suid +		    ", local class serialVersionUID = " +		    localDesc.getSerialVersionUID());	    }			    if (!classNamesEqual(name, localDesc.name)) {		throw new InvalidClassException(localDesc.name,		    "local class name incompatible with stream class " +		    "name \"" + name + "\"");	    }	    	    if ((serializable == localDesc.serializable) &&		(externalizable != localDesc.externalizable))	    {		throw new InvalidClassException(localDesc.name, 		    "Serializable incompatible with Externalizable");	    }	    if ((serializable != localDesc.serializable) ||		(externalizable != localDesc.externalizable) ||		!(serializable || externalizable))	    {		deserializeEx = new InvalidClassException(localDesc.name,		    "class invalid for deserialization");	    }	    	    // cons = localDesc.cons;  // - Back-ported from JDK 1.4 	    initClass = localDesc.initClass;	    writeObjectMethod = localDesc.writeObjectMethod;	    readObjectMethod = localDesc.readObjectMethod;	    readObjectNoDataMethod = localDesc.readObjectNoDataMethod;	    writeReplaceMethod = localDesc.writeReplaceMethod;	    readResolveMethod = localDesc.readResolveMethod;	    if (deserializeEx == null) {		deserializeEx = localDesc.deserializeEx;	    }	}	fieldRefl = getReflector(fields, localDesc);	// reassign to matched fields so as to reflect local unshared settings	fields = fieldRefl.getFields();    }        /**     * Reads non-proxy class descriptor information from given input stream.     * The resulting class descriptor is not fully functional; it can only be     * used as input to the ObjectInputStream.resolveClass() and     * ObjectStreamClass.initNonProxy() methods.     */    void readNonProxy(ObjectInputStream in) 	throws IOException, ClassNotFoundException    {	name = in.readUTF();	suid = new Long(in.readLong());	isProxy = false;	byte flags = in.readByte();	externalizable = 	    ((flags & ObjectStreamConstants.SC_EXTERNALIZABLE) != 0);	serializable = (externalizable ||	    ((flags & ObjectStreamConstants.SC_SERIALIZABLE) != 0));	hasWriteObjectData = 	    ((flags & ObjectStreamConstants.SC_WRITE_METHOD) != 0);	hasBlockExternalData = 	    ((flags & ObjectStreamConstants.SC_BLOCK_DATA) != 0);		int numFields = in.readShort();	fields = (numFields > 0) ? 	    new ObjectStreamField[numFields] : NO_FIELDS;	for (int i = 0; i < numFields; i++) {	    char tcode = (char) in.readByte();	    String name = in.readUTF();	    String signature = ((tcode == 'L') || (tcode == '[')) ?		in.readTypeString() : new String(new char[] { tcode });	    fields[i] = new ObjectStreamField(name, signature, false);	}	computeFieldOffsets();    }    /**     * Writes non-proxy class descriptor information to given output stream.     */    void writeNonProxy(ObjectOutputStream out) throws IOException {	out.writeUTF(name);	out.writeLong(getSerialVersionUID());	byte flags = 0;	if (externalizable) {	    flags |= ObjectStreamConstants.SC_EXTERNALIZABLE;	    int protocol = out.getProtocolVersion();	    if (protocol != ObjectStreamConstants.PROTOCOL_VERSION_1) {		flags |= ObjectStreamConstants.SC_BLOCK_DATA;	    }	} else if (serializable) {	    flags |= ObjectStreamConstants.SC_SERIALIZABLE;	}	if (hasWriteObjectData) {	    flags |= ObjectStreamConstants.SC_WRITE_METHOD;	}	out.writeByte(flags);		out.writeShort(fields.length);	for (int i = 0; i < fields.length; i++) {	    ObjectStreamField f = fields[i];	    out.writeByte(f.getTypeCode());	    out.writeUTF(f.getName());	    if (!f.isPrimitive()) {		out.writeTypeString(f.getTypeString());	    }	}    }        /**     * Returns ClassNotFoundException (if any) thrown while attempting to     * resolve local class corresponding to this class descriptor.     */    ClassNotFoundException getResolveException() {	return resolveEx;    }    /**     * Throws an InvalidClassException if object instances referencing this     * class descriptor should not be allowed to deserialize.     */    void checkDeserialize() throws InvalidClassException {	if (deserializeEx != null) {	    throw deserializeEx;	}    }        /**     * Throws an InvalidClassException if objects whose class is represented by     * this descriptor should not be permitted to use default serialization     * (e.g., if the class declares serializable fields that do not correspond     * to actual fields, and hence must use the GetField API).     */    void checkDefaultSerialize() throws InvalidClassException {	if (defaultSerializeEx != null) {	    throw defaultSerializeEx;	}    }    /**     * Returns superclass descriptor.  Note that on the receiving side, the     * superclass descriptor may be bound to a class that is not a superclass     * of the subclass descriptor's bound class.     */    ObjectStreamClass getSuperDesc() {	return superDesc;    }        /**     * Returns the "local" class descriptor for the class associated with this     * class descriptor (i.e., the result of     * ObjectStreamClass.lookup(this.forClass())) or null if there is no class     * associated with this descriptor.     */    ObjectStreamClass getLocalDesc() {	return localDesc;    }    /**     * Returns arrays of ObjectStreamFields representing the serializable     * fields of the represented class.  If copy is true, a clone of this class     * descriptor's field array is returned, otherwise the array itself is     * returned.     */    ObjectStreamField[] getFields(boolean copy) {	return copy ? (ObjectStreamField[]) fields.clone() : fields;    }        /**     * Looks up a serializable field of the represented class by name and type.     * A specified type of null matches all types, Object.class matches all     * non-primitive types, and any other non-null type matches assignable     * types only.  Returns matching field, or null if no match found.     */    ObjectStreamField getField(String name, Class type) {	for (int i = 0; i < fields.length; i++) {	    ObjectStreamField f = fields[i];	    if (f.getName().equals(name)) {		if (type == null) 		{		    return f;                } else if (type == Object.class) {                    if (!f.isPrimitive()) {                        return f;                    }		} else {		    Class ftype = f.getType();		    if (ftype != null && type.isAssignableFrom(ftype)) {		        return f;		    }                }	    }	}	return null;    }    /**     * Returns true if class descriptor represents a dynamic proxy class, false     * otherwise.     */    boolean isProxy() {	return isProxy;    }        /**     * Returns true if represented class implements Externalizable, false     * otherwise.     */    boolean isExternalizable() {	return externalizable;    }        /**     * Returns true if represented class implements Serializable, false     * otherwise.     */    boolean isSerializable() {	return serializable;    }    /**     * Returns true if class descriptor represents externalizable class that     * has written its data in 1.2 (block data) format, false otherwise.     */    boolean hasBlockExternalData() {	return hasBlockExternalData;    }        /**     * Returns true if class descriptor represents serializable (but not     * externalizable) class which has written its data via a custom     * writeObject() method, false otherwise.     */    boolean hasWriteObjectData() {	return hasWriteObjectData;    }        /**     * Returns true if represented class is serializable/externalizable and can     * be instantiated by the serialization runtime--i.e., if it is     * externalizable and defines a public no-arg constructor, or if it is     * non-externalizable and its first non-serializable superclass defines an     * accessible no-arg constructor.  Otherwise, returns false.     */    boolean isInstantiable() {      // return (cons != null); // - Back-ported from JDK 1.4       return (initClass != null);    }        /**     * Returns true if represented class is serializable (but not     * externalizable) and defines a conformant writeObject method.  Otherwise,     * returns false.     */    boolean hasWriteObjectMethod() {	return (writeObjectMethod != null);    }        /**     * Returns true if represented class is serializable (but not     * externalizable) and defines a conformant readObject method.  Otherwise,     * returns false.     */    boolean hasReadObjectMethod() {	return (readObjectMethod != null);    }        /**     * Returns true if represented class is serializable (but not     * externalizable) and defines a conformant readObjectNoData method.     * Otherwise, returns false.     */    boolean hasReadObjectNoDataMethod() {	return (readObjectNoDataMethod != null);    }        /**     * Returns true if represented class is serializable or externalizable and     * defines a conformant writeReplace method.  Otherwise, returns false.     */    boolean hasWriteReplaceMethod() {	return (writeReplaceMethod != null);    }        /**     * Returns true if represented class is serializable or externalizable and     * defines a conformant readResolve method.  Otherwise, returns false.     */    boolean hasReadResolveMethod() {	return (readResolveMethod != null);    }        /**     * Creates a new instance of the represented class.  If the class is     * externalizable, invokes its public no-arg constructor; otherwise, if the     * class is serializable, invokes the no-arg constructor of the first     * non-serializable superclass.  Throws UnsupportedOperationException if     * this class descriptor is not associated with a class, if the associated     * class is non-serializable or if the appropriate no-arg constructor is     * inaccessible/unavailable.     */    Object newInstance()	throws InstantiationException, InvocationTargetException,	       UnsupportedOperationException, IllegalAccessException     {      /* // - Back-ported from JDK 1.4 	if (cons != null) {	    try {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?