技术标签: java. 序列化 SerialVersionUID
我必须使用大量编译的Java类,该类没有明确指定串行VersionUID。因为它们的UID由编译器任意生成,所以许多需要序列化和反序列化的类最终导致异常,即使实际类定义匹配。 (当然,这是所有预期的行为。)
我要返回并修复所有第三方代码是不切实际的。
因此,我的问题是:有没有办法制作Java运行时 忽视 当结构存在实际差异时,序列Versialuids的差异,并且只能失去反序列化?
如果您可以访问代码库,可以使用 蚂蚁的Serialver任务 插入并修改 serialVersionUID 在Serializable类的源代码中,并为全部解决问题。
如果您不能,或者,如果这不是一个选项(例如,如果您已经序列化了某些需要反级化的对象),则一个解决方案将是扩展 ObjectInputStream。增加其行为来比较 serialVersionUID 流描述符与 serialVersionUID 在本地JVM中的类中,此描述符表示并在不匹配情况下使用本地类描述符。然后,只需使用此自定义类进行反序列化。这样的东西(信用 这条信息):
import java.io.IOException;
import java.io.InputStream;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DecompressibleInputStream extends ObjectInputStream {
private static Logger logger = LoggerFactory.getLogger(DecompressibleInputStream.class);
public DecompressibleInputStream(InputStream in) throws IOException {
super(in);
}
@Override
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
ObjectStreamClass resultClassDescriptor = super.readClassDescriptor(); // initially streams descriptor
Class localClass; // the class in the local JVM that this descriptor represents.
try {
localClass = Class.forName(resultClassDescriptor.getName());
} catch (ClassNotFoundException e) {
logger.error("No local class for " + resultClassDescriptor.getName(), e);
return resultClassDescriptor;
}
ObjectStreamClass localClassDescriptor = ObjectStreamClass.lookup(localClass);
if (localClassDescriptor != null) { // only if class implements serializable
final long localSUID = localClassDescriptor.getSerialVersionUID();
final long streamSUID = resultClassDescriptor.getSerialVersionUID();
if (streamSUID != localSUID) { // check for serialVersionUID mismatch.
final StringBuffer s = new StringBuffer("Overriding serialized class version mismatch: ");
s.append("local serialVersionUID = ").append(localSUID);
s.append(" stream serialVersionUID = ").append(streamSUID);
Exception e = new InvalidClassException(s.toString());
logger.error("Potentially Fatal Deserialization Operation.", e);
resultClassDescriptor = localClassDescriptor; // Use local class descriptor for deserialization
}
}
return resultClassDescriptor;
}
}
Runtime 表示的是运行时的状态对象。是一个封装的JVM类,就像单例一样! Runtime定义了三个方法:totalMemory()可以使用的内存、 maxMemory()最大 可用的内存、 freeMemory()当前空余的内存 Throwable:代表是可抛出的异常。 Error:代表的是严重的错误, Exception:代表的就是异常,分为两种派生类:一种是运行时异常(RuntimeEx...
File-settings- Inspection - Serializable class without ‘serialVersionUID’ (勾选此项) 当实现Serializable 接口的类没有申明serialVersionUID属性,编辑器就会给出提示,按alt + Enter 可以快速生成。...
java序列化serialVersionUID-的作用-代码详解 1.操作环境 2.serialVersionUID的作用 3.代码演示 3.1.反序列化-正常 3.2.反序列化-异常-InvalidClassException 3.3.反序列化-异常解决-添加serialVersionUID 【代码地址】 1.操作环境 JDK8 lombok-1.18.10 2.serialVersionUID...
序列化和SerialVersionUID始终是许多Java开发人员的难题。 我经常会看到类似此SerialVersionUID的问题,或者如果不在我的Serializable类中声明SerialVersionUID会发生什么情况? 除了涉及到的复杂性和罕见的使用之外,这些问题的另一个原因是Eclipse IDE发出警告,警告不要缺少SerialVersionUID,例如“ Serial...
在 IntelliJ IDEA Java类实现 Serializable 接口,没有生成 serialVersionUID ,也没有对serialVersionUID 进行代码检查。 打开 设置(File | Settings )搜索 serialVersionUID 设置路径:File | Settings | Editor | Inspec...
JUnitってなんぞや~? ワンパターンな入りですみません...。 JUnitとはJavaで開発されたプログラムにおいてユニットテスト(単体テスト)の自動化を行うためのフレームワークである。 JUnit - Wikipedia Eclipseには、標準でJUnitが導入されているようです。 それでは、実際に使っていきたいと思います。 JUnitを使ってみる まずは、プロジェ...
背景: redis字典(hash表)当数据越来越多的时候,就会发生扩容,也就是rehash 对比:java中的hashmap,当数据数量达到阈值的时候(0.75),就会发生rehash,hash表长度变为原来的二倍,将原hash表数据全部重新计算hash地址,重新分配位置,达到rehash目的 redis中的hash表采用的是渐进式hash的方式: 1、redis字典(hash表)底层有...
2019独角兽企业重金招聘Python工程师标准>>> 我们平时按下键盘上的‘回车键’,就能实现回车换行【我们在屏幕上所看到的就是光标移到了下一行的开头位置!!ps:不讨论软件实现的特殊功能,如word里的回车智能缩进】。因此对这个按键更准确说应该叫做‘回车换行键’ 就且将这种将光标移到下行开...
首先,在Windows操作系统下安装python,完成python环境的搭建。(我看有的博客需要配置环境变量,其实不必要,因为我们在安装的时候只要勾选如下图所示即可避免不必要的麻烦) 第二步,使用快捷键windows+R或者点击如下图所示,之后点击运行 在其中输入cmd即可进入dos命令。 第三步,输入以下命令 就会自动安装,等待安装完成即可。 第四步,检测django是否安装成功,如果出现如下图...
做过项目的童鞋可能都使用过日志功能,以便有异常错误能够快速跟踪、定位,Qt也提供的类似的机制。之前用Qt4做项目时使用的是Qt::qInstallMsgHandler(),到了Qt5,使用了新的Qt::qInstallMessageHandler()来替代,详情请查看Qt助手(C++ API changes)。 描述 助手中在C++ API ...
I have a dataframe and i want to calculate the sum of variables present in a vector in every row and make the sum in other variable after i want the name of new variable created to be from the name of...
I have a very rough project that done partially in zend framework (not ZF2). The 'application', 'library' and 'public' folders are on the same root. Now i need to create a library 'Anil' in the 'libra...
I want to hide some of the categories from magento home page. I have created some categories like New Arrivals, Special Offer Which I would like to show in some different way may be in left panel or r...
I am writing to an internal file in Android Studio The code lets me write lines of data split with commas. I am able to then go to another activity and read it all out at once. However I want to be ab...
I'm new to Ruby On Rails, I used the acts_as_votable gem to create Like and Unlike Buttons to make Users like and unlike Posts but I can't make them change from Like to Unlike (and viceversa) and upda...