Java Record 的一些思考 - 序列化相关,相关资料参考

}

}

复制代码

再次执行代码 DeSerializationTest,我们会发现有报错,但是 UserClass 被反序列化出来了:

UserClass(id=1, age=-1)

Exception in thread “main” java.io.InvalidObjectException: id and age should be larger than 0

at java.base/java.io.ObjectInputStream.readRecord(ObjectInputStream.java:2348)

at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2236)

at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1742)

at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:514)

at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:472)

at DeSerializationTest.main(DeSerializationTest.java:13)

Caused by: java.lang.IllegalArgumentException: id and age should be larger than 0

at UserRecord.(UserRecord.java:6)

at java.base/java.io.ObjectInputStream.readRecord(ObjectInputStream.java:2346)

… 5 more

复制代码

兼容性测试


我们再来看如果删除一个字段会怎么样:

@Data

public class UserClass implements Serializable {

private final int age;

}

public record UserRecord(int age) implements Serializable {

}

复制代码

执行代码,读取 UserClass 的时候就会报错,这也是符合预期的,因为这在普通类对象的反序列化说明中就说这种是不兼容修改。将 UserClass 的字段恢复,重新执行代码,发现成功:

UserClass(id=1, age=-1)

UserRecord[age=-1]

复制代码

也就是说,Record 是默认兼容缺失字段的反序列化的

我们将字段恢复,再来看多一个字段会怎么样:

@Data

public class UserClass implements Serializable {

private final int id;

private final int sex;

private final int age;

}

public record UserRecord(int id, int sex, int age) implements Serializable {

}

复制代码

执行代码,读取 UserClass 的时候就会报错,这也是符合预期的。将 UserClass 的字段恢复,重新执行代码,发现成功:

UserClass(id=1, age=-1)

UserRecord[id=2, sex=0, age=-1]

复制代码

也就是说,Record 是默认兼容字段变多的反序列化的

最后测试一下 Record 的 field 类型如果变了呢:

public record UserRecord(int id, Integer age) implements Serializable {

}

复制代码

执行代码发现失败,因为类型不匹配了(就算是包装类也不行):

UserClass(id=1, age=-1)

Exception in thread “main” java.io.InvalidClassException: UserRecord; incompatible types for field age

at java.base/java.io.ObjectStreamClass.matchFields(ObjectStreamClass.java:2391)

at java.base/java.io.ObjectStreamClass.getReflector(ObjectStreamClass.java:2286)

at java.base/java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:788)

at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:2060)

at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1907)

at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2209)

at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1742)

at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:514)

at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:472)

at DeSerializationTest.main(DeSerializationTest.java:13)

复制代码

一些主流的序列化框架的兼容

=============

由于 Record 限制了序列化与反序列化的唯一方式,所以其实兼容起来很简单,比起 Java Class 改个结构,加个特性导致的序列化框架更改来说还要简单。

这三个框架中实现对于 Record 的兼容思路都很类似,也比较简单,即:

  1. 实现一个针对 Record 的专用的 Serializer 以及Deserializer。

  2. 通过反射(Java Reflection)或者句柄(Java MethodHandle)验证当前版本的 Java 是否支持 Record,以及获取 Record 的规范构造函数(canonical constructor)以及各种 field 的 getter 进行反序列化和序列化。给大家两个工具类进行参考,分别是使用反射(Java Reflection)和句柄(Java MethodHandle)实现:

import java.lang.reflect.Constructor;

import java.lang.reflect.Method;

import java.util.Arrays;

import java.util.Comparator;

import common.RecComponent;

/**

  • Utility methods for record serialization, using Java Core Reflection.

*/

public class ReflectUtils {

private static final Method IS_RECORD;

private static final Method GET_RECORD_COMPONENTS;

private static final Method GET_NAME;

private static final Method GET_TYPE;

static {

Method isRecord;

Method getRecordComponents;

Method getName;

Method getType;

try {

// reflective machinery required to access the record components

// without a static dependency on Java SE 14 APIs

Class<?> c = Class.forName(“java.lang.reflect.RecordComponent”);

isRecord = Class.class.getDeclaredMethod(“isRecord”);

getRecordComponents = Class.class.getMethod(“getRecordComponents”);

getName = c.getMethod(“getName”);

getType = c.getMethod(“getType”);

} catch (ClassNotFoundException | NoSuchMethodException e) {

// pre-Java-14

isRecord = null;

getRecordComponents = null;

getName = null;

getType = null;

}

IS_RECORD = isRecord;

GET_RECORD_COMPONENTS = getRecordComponents;

GET_NAME = getName;

GET_TYPE = getType;

}

/** Returns true if, and only if, the given class is a record class. */

static boolean isRecord(Class<?> type) {

try {

return (boolean) IS_RECORD.invoke(type);

} catch (Throwable t) {

throw new RuntimeException(“Could not determine type (” + type + “)”);

}

}

/**

  • Returns an ordered array of the record components for the given record

  • class. The order is imposed by the given comparator. If the given

  • comparator is null, the order is that of the record components in the

  • record attribute of the class file.

*/

static RecComponent[] recordComponents(Class type,

Comparator comparator) {

try {

Object[] rawComponents = (Object[]) GET_RECORD_COMPONENTS.invoke(type);

RecComponent[] recordComponents = new RecComponent[rawComponents.length];

for (int i = 0; i < rawComponents.length; i++) {

final Object comp = rawComponents[i];

recordComponents[i] = new RecComponent(

(String) GET_NAME.invoke(comp),

(Class<?>) GET_TYPE.invoke(comp), i);

}

if (comparator != null) Arrays.sort(recordComponents, comparator);

return recordComponents;

} catch (Throwable t) {

throw new RuntimeException(“Could not retrieve record components (” + type.getName() + “)”);

}

}

/** Retrieves the value of the record component for the given record object. */

static Object componentValue(Object recordObject,

RecComponent recordComponent) {

try {

Method get = recordObject.getClass().getDeclaredMethod(recordComponent.name());

return get.invoke(recordObject);

} catch (Throwable t) {

throw new RuntimeException(“Could not retrieve record components (”

  • recordObject.getClass().getName() + “)”);

}

}

/**

  • Invokes the canonical constructor of a record class with the

  • given argument values.

*/

static T invokeCanonicalConstructor(Class recordType,

RecComponent[] recordComponents,

Object[] args) {

try {

Class<?>[] paramTypes = Arrays.stream(recordComponents)

.map(RecComponent::type)

.toArray(Class<?>[]::new);

Constructor canonicalConstructor = recordType.getConstructor(paramTypes);

return canonicalConstructor.newInstance(args);

} catch (Throwable t) {

throw new RuntimeException(“Could not construct type (” + recordType.getName() + “)”);

}

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
img

最后

学习视频:

大厂面试真题:

136)]
[外链图片转存中…(img-y6m2Nt7H-1711892098136)]
[外链图片转存中…(img-2iGJ1aG6-1711892098137)]
[外链图片转存中…(img-sD6ks7Op-1711892098137)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-CWbdyfPr-1711892098138)]

最后

学习视频:

[外链图片转存中…(img-wmTQBtRO-1711892098138)]

大厂面试真题:

[外链图片转存中…(img-1YBVo9sT-1711892098138)]

  • 27
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值