该类是内部类,无法在包外引用,且该类为final,无法被继承
该类无引入包和继承类
该类的类头注释如下:
/** * Context during upcalls from object stream to class-defined * readObject/writeObject methods. * Holds object currently being deserialized and descriptor for current class. * * This context keeps track of the thread it was constructed on, and allows * only a single call of defaultReadObject, readFields, defaultWriteObject * or writeFields which must be invoked on the same thread before the class's * readObject/writeObject method has returned. * If not set to the current thread, the getObj method throws NotActiveException. */
大意如下:
从对象流到类上下文定义的readObject和writeObject方法会调用该类
存储当前被反序列化的类和该类的描述符
这个上下文跟踪它构建的线程,并允许只调用defaultReadObject,readFields,defaultWriteObject或writeFields,它们必须在类之前在同一个线程上调用 readObject / writeObject方法已经返回
如果没设置当前线程,getOj方法会抛出NotActiveException
该类含有如下的成员变量:
被操作的Object:
private final Object obj;
内含的对象流类:
private final ObjectStreamClass desc;
当前操作线程
private Thread thread;
该类含有如下的成员方法:
构造函数(指定操作类和操作流类
public SerialCallbackContext(Object obj, ObjectStreamClass desc) { this.obj = obj; this.desc = desc; this.thread = Thread.currentThread(); }
获取当前操作对象
public Object getObj() throws NotActiveException { checkAndSetUsed(); return obj; }
获取当前对象流类描述符
public ObjectStreamClass getDesc() { return desc; }
检测当前操作线程可用性
public void check() throws NotActiveException { if (thread != null && thread != Thread.currentThread()) {//仅当当前线程不为空切非当前操作线程时抛出异常 throw new NotActiveException( "expected thread: " + thread + ", but got: " + Thread.currentThread()); } }
检测当前线程是否为当前操作线程,如果是则置空,否则抛异常
private void checkAndSetUsed() throws NotActiveException { if (thread != Thread.currentThread()) { throw new NotActiveException( "not in readObject invocation or fields already read"); } thread = null; }
将当前操作线程置空
public void setUsed() { thread = null; }
该类主要用于序列化操作时对对象,对象类的记录,我们编程无法调用该类,看看即可