美文网首页
JDK源码-ObjectStreamClass

JDK源码-ObjectStreamClass

作者: 薛云龙 | 来源:发表于2017-08-25 17:18 被阅读56次

ObjectStreamClass

  • 类的序列化描述符,它存储了类的序列化版本号和类名.可以通过lookup()方法来查找/创建在这个JVM中加载的特定的ObjectStreamClass对象。
  • lookup方法可以获取到被序列化的类的信息.下图中的蓝色部分,但是加了transient关键字的变量就不能获取到了.
Paste_Image.png ObjectStreamClass.lookup(SerializablePerson.class) ObjectStreamClass.lookup(UnSerializablePerson.class)
  • lookupAny方法可以获取即使没有序列化的类的信息.但是只能获取到类名
ObjectStreamClass.lookupAny(UnSerializablePerson.class)
  • 强力分析一波ObjectStreamClass lookup(Class<?> cl, boolean all)方法.我擦,这个方法台生涩了,这里先留个坑,回头再剖析吧,,,,
/**
     * Looks up and returns class descriptor for given class, or null if class
     * is non-serializable and "all" is set to false.
     *
     * @param   cl class to look up
     * @param   all if true, return descriptors for all classes; if false, only
     *          return descriptors for serializable classes
     */
    static ObjectStreamClass lookup(Class<?> cl, boolean all) {
        if (!(all || Serializable.class.isAssignableFrom(cl))) {
            return null;
        }
        processQueue(Caches.localDescsQueue, Caches.localDescs);
        WeakClassKey key = new WeakClassKey(cl, Caches.localDescsQueue);
        Reference<?> ref = Caches.localDescs.get(key);
        Object entry = null;
        if (ref != null) {
            entry = ref.get();
        }
        EntryFuture future = null;
        if (entry == null) {
            EntryFuture newEntry = new EntryFuture();
            Reference<?> newRef = new SoftReference<>(newEntry);
            do {
                if (ref != null) {
                    Caches.localDescs.remove(key, ref);
                }
                ref = Caches.localDescs.putIfAbsent(key, newRef);
                if (ref != null) {
                    entry = ref.get();
                }
            } while (ref != null && entry == null);
            if (entry == null) {
                future = newEntry;
            }
        }

        if (entry instanceof ObjectStreamClass) {  // check common case first
            return (ObjectStreamClass) entry;
        }
        if (entry instanceof EntryFuture) {
            future = (EntryFuture) entry;
            if (future.getOwner() == Thread.currentThread()) {
                /*
                 * Handle nested call situation described by 4803747: waiting
                 * for future value to be set by a lookup() call further up the
                 * stack will result in deadlock, so calculate and set the
                 * future value here instead.
                 */
                entry = null;
            } else {
                entry = future.get();
            }
        }
        if (entry == null) {
            try {
                entry = new ObjectStreamClass(cl);
            } catch (Throwable th) {
                entry = th;
            }
            if (future.set(entry)) {
                Caches.localDescs.put(key, new SoftReference<Object>(entry));
            } else {
                // nested lookup call already set future
                entry = future.get();
            }
        }

        if (entry instanceof ObjectStreamClass) {
            return (ObjectStreamClass) entry;
        } else if (entry instanceof RuntimeException) {
            throw (RuntimeException) entry;
        } else if (entry instanceof Error) {
            throw (Error) entry;
        } else {
            throw new InternalError("unexpected entry: " + entry);
        }
    }

相关文章

网友评论

      本文标题:JDK源码-ObjectStreamClass

      本文链接:https://www.haomeiwen.com/subject/przudxtx.html