`

JDK中反序列化对象的过程(ObjectInputStream#readObject)

    博客分类:
  • java
阅读更多

         此处,对象描述信息即ObjectStreamClass的实例

1、java ObjectInputStream#readObject的时候,先从输入流读入对象,读取对象信息,如果在读取过程中出现异常,则通过markDependency处理;处理完后还会调用注册进来的callback

 

2、读取对象的过程,先读取对象,然后再java.io.ObjectInputStream.checkResolve(java.lang.Object);读取对象的过程是,先从被反序列化的输入流中读取对象的标识,其中0x73是Object,然后会调用java.io.ObjectInputStream.readOrdinaryObject(boolean)读取对象,这里会先读取类描述信息ObjectStreamClass,然后实例化。

 

3、读取元数据信息过程:

     java.io.ObjectInputStream.readClassDescriptor()先读取InputStream中数据解析出ObjectStreamClass信息,如name、uuid、isProxy、hasWriteObjectData、externalizable、ObjectStreamFields。然后

java.io.ObjectStreamClass.initNonProxy(java.io.ObjectStreamClass, java.lang.Class<?>, java.lang.ClassNotFoundException, java.io.ObjectStreamClass)

会根据从输入流中解析出来的ObjectStreamClass再构造一个新的ObjectStreamClass对象,在构造方法里边会查找本地(找不到就构造)一个本地对象的描述信息。在此之前,这两个ObjectStreamClass描述对象都是远程对象的信息,即每个远程对象描述信息都有一个关联的本地对象描述信息,但是他们都指向本地vm的一个Class对象。在创建本地对象描述信息对象的时候,会递归创建超类的描述信息对象。

     ObjectStreamClass这个实例还包括一些信息:超类描述信息ObjectStreamClass、构造函数、私有的writeObject、私有的readObject、私有的readObjectNoData等信息、writeReplace、readResolve方法。

     其中,在获取构造方法的时候,是获取当前类的 [最顶层实现了Serializable的祖先类的超类][即自上而下连续的最后一个未实现Serizable接口的类]的构造函数,被反序列化的类的实例也是通过这个构造函数创建的。eg,DTO 继承BaseDTO 实现Serializable接口,则反序列化的时候,是拿到了BaseDTO的构造函数来创建的实例。如果BaseDTO同时实现了Serializable接口,则返回BaseDTO的超类的构造函数(Object)。

    /**
     * Returns subclass-accessible no-arg constructor of first non-serializable
     * superclass, or null if none found.  Access checks are disabled on the
     * returned constructor (if any).
     */     
      private static Constructor<?> getSerializableConstructor(Class<?> cl) {
        Class<?> initCl = cl;
        //initCl是继承体系中,第一个实现了Serializable接口的类的超类
        while (Serializable.class.isAssignableFrom(initCl)) {
            if ((initCl = initCl.getSuperclass()) == null) {
                return null;
            }
        }
        try {
            Constructor<?> cons = initCl.getDeclaredConstructor((Class<?>[]) null);
            int mods = cons.getModifiers();
            if ((mods & Modifier.PRIVATE) != 0 ||
                ((mods & (Modifier.PUBLIC | Modifier.PROTECTED)) == 0 &&
                 !packageEquals(cl, initCl)))
            {
                return null;
            }
            cons = reflFactory.newConstructorForSerialization(cl, cons);
            cons.setAccessible(true);
            return cons;
        } catch (NoSuchMethodException ex) {
            return null;
        }
    }

4、实例化正在被反序列化的对象

     调用ObjectStreamClass里边构造出来的构造方法cons,创建对象实例。前边提到这个构造方法cons是当前类 [最顶层实现了Serializable的祖先类的超类]的构造函数,那newInstance出来的,岂不是非当前类型实例?这个实现在java.io.ObjectStreamClass.getSerializableConstructor(java.lang.Class<?>)里边,在拿到了当前类 [最顶层实现了Serializable的祖先类的超类]的构造函数后,会再调用sun.reflect.ReflectionFactory.newConstructorForSerialization(java.lang.Class<?>, java.lang.reflect.Constructor<?>)生成一个新的构造函数

public class sun.reflect.GeneratedSerializationConstructorAccessor1 extends sun.reflect.SerializationConstructorAccessorImpl {
  
  // Method descriptor #26 ()V
  // Stack: 1, Locals: 1
  public GeneratedSerializationConstructorAccessor1();
    0  aload_0 [this]
    1  invokespecial sun.reflect.SerializationConstructorAccessorImpl() [36]
    4  return

  // Method descriptor #14 ([Ljava/lang/Object;)Ljava/lang/Object;
  // Stack: 6, Locals: 2
  public java.lang.Object newInstance(java.lang.Object[] arg0) throws java.lang.reflect.InvocationTargetException;
  //这里是创建DTO对象,但是后边不调DTO的构造函数
     0  new com.tmall.buy.serializable.DTO [6] 
     3  dup
     4  aload_1 [arg0]
     5  ifnull 24
     8  aload_1 [arg0]
     9  arraylength
    10  sipush 0
    13  if_icmpeq 24
    16  new java.lang.IllegalArgumentException [22]
    19  dup
    20  invokespecial java.lang.IllegalArgumentException() [29]
    23  athrow
   //这里是调用BaseDTO的构造函数,即用DTO的对象调用了BaseDTO类的构造方法,相当于super()
    24  invokespecial com.tmall.buy.serializable.BaseDTO() [12]
    27  areturn
    28  invokespecial java.lang.Object.toString() : java.lang.String [42]
    31  new java.lang.IllegalArgumentException [22]
    34  dup_x1
    35  swap
    36  invokespecial java.lang.IllegalArgumentException(java.lang.String) [32]
    39  athrow
    40  new java.lang.reflect.InvocationTargetException [24]
    43  dup_x1
    44  swap
    45  invokespecial java.lang.reflect.InvocationTargetException(java.lang.Throwable) [35]
    48  athrow
      Exception Table:
        [pc: 0, pc: 24] -> 28 when : java.lang.ClassCastException
        [pc: 0, pc: 24] -> 28 when : java.lang.NullPointerException
        [pc: 24, pc: 27] -> 40 when : java.lang.Throwable
}

     调用java.lang.reflect.Constructor.newInstance(java.lang.Object[])

5、读取反序列化对象的值java.io.ObjectInputStream#readSerialData(java.lang.Object, java.io.ObjectStreamClass)

     先把ObjectStreamClass的继承信息扁平化,搞成ClassDataSlot数组。如果继承体系中的描述信息,在输入流中没有相关类型,则其hasData=false。对于hasData==false的ClassDataSlot,会调用这个slot相关ObjectStreamClass相关类型的readObjectNoData方法(如果有)。

     如果hasData=true && 当前被反序列化的类有readObject方法,就会调用readObject方法;这样的话,就读取不到输入流中需要反序列化的对象的字段的值了,如果想调用自己的readObject方法,同时想读取输入流中的值,在readObject方法中显式调用java.io.ObjectInputStream#defaultReadObject方法即可,此方法也是转调的java.io.ObjectInputStream.defaultReadFields(java.lang.Object, java.io.ObjectStreamClass)。

     否则如果hasData=true,则调用java.io.ObjectInputStream.defaultReadFields(java.lang.Object, java.io.ObjectStreamClass)方法为实例对象赋值。

 

6、反序列化的时候赋值java.io.ObjectInputStream.defaultReadFields(java.lang.Object, java.io.ObjectStreamClass)

     该方法首先根据对象描述信息对象读取要反序列化对象的字段信息ObjectStreamField——此处的字段信息是远程对象的字段值,可能跟本地对象字段内容不一致,然后继续从输入流中读取相关字段值。最后通过ObjectStreamClass的FieldReflector这个引用把值设置到最终要被反序列化的对象上。

     

7、4中生成sun.reflect.GeneratedSerializationConstructorAccessor1 类中,子类DTO实例调用超类BaseDTO方法为何可以成功?方法调用指令有5种,4中调用BaseDTO方法的时候,是invokespecial指令,这个指令跟invokevirtual不同,不会根据当前对象实例动态分派。所以是可以调用BaseDTO#<init>方法的,但是这个是否符合jvm的规范,能否通过jvm的校验还需要再查资料。

   * invokevirtual invokes an instance method of an object, dispatching on the (virtual) type of the object. This is the normal method dispatch in the Java programming language.
   * invokeinterface invokes an interface method, searching the methods implemented by the particular run-time object to find the appropriate method.
   * invokespecial invokes an instance method requiring special handling,whether an instance initialization method (§2.9), a private method, or a superclass method.
   * invokestatic invokes a class (static) method in a named class.
   * invokedynamic invokes the method which is the target of the call site object bound to the invokedynamic instruction. The call site object was bound to a specific lexical occurrence of the invokedynamic instruction by the Java Virtual Machine as a result of running a bootstrap method before the first execution of the instruction. Therefore, each occurrence of an invokedynamic instruction has a unique linkage state, unlike the other instructions which invoke methods. 

8、这个点Hessian2是如何做的呢?com.caucho.hessian.io.UnsafeDeserializer.instantiate()

  protected Object instantiate() throws Exception{
    return _unsafe.allocateInstance(_type);
  }

而_unsafe.allocateInstance这个方法的功能见注释:

Allocate an instance but do not run any constructor. Initializes the class if it has not yet been.
分享到:
评论
1 楼 妖人不要跑 2016-01-22  
   

相关推荐

Global site tag (gtag.js) - Google Analytics