gpt4 book ai didi

Java:反序列化已加载的类时会发生什么

转载 作者:行者123 更新时间:2023-12-02 06:27:58 25 4
gpt4 key购买 nike

假设类 com.Foo 是从 JAR 加载的,后来加载了一个具有相同名称 com.Foo 的类,但不同的定义(其他字段)被反序列化(例如,从数据库加载或从远程调用接收)。

可能会产生什么后果?新接收的类(class)会有影响吗?假设该类在应用程序的其他部分使用,保存在数据库中并稍后进行序列化/JSON 编码。

最佳答案

如果反序列化 Class<?> object 然后加载具有完全限定类名的类。如果它已经加载,您将获得对该类的引用。

我认为要获得完整的答案,您应该阅读 Java Object Serialization Specification

以下是我认为有趣的规范中的一些引文:

1.1 概述

Special handling is required for arrays, enum constants, and objects of type Class, ObjectStreamClass, and String. Other objects must implement either the Serializable or the Externalizable interface to be saved in or restored from a stream.

2。对象输出类

If the object is a Class, the corresponding ObjectStreamClass is written to the stream, a handle is assigned for the class, and writeObject returns.

3.对象输入类

If the object in the stream is a Class, read its ObjectStreamClass descriptor, add it and its handle to the set of known objects, and return the corresponding Class object.

关于Java:反序列化已加载的类时会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20347717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com