Skip to content

Commit

Permalink
feat[protocol]: reflect serialization support record class
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Sep 26, 2023
1 parent 7aa46b0 commit 792cdfb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ private static String readMethodBody(ProtocolRegistration registration) {
var packetClazz = constructor.getDeclaringClass();
if (packetClazz.isRecord()) {
var fields = registration.getFields();
var fieldNames = ProtocolAnalysis.getFields(packetClazz).stream().map(Field::getName).toList();
List<String> constructorParam = fieldNames.stream().collect(Collectors.toList());
var originFields = ProtocolAnalysis.getFields(packetClazz);
var constructorParams = new String[originFields.size()];

for (var i = 0; i < fields.length; i++) {
var field = fields[i];
Expand All @@ -225,11 +225,10 @@ private static String readMethodBody(ProtocolRegistration registration) {
}

var readObject = enhanceSerializer(fieldRegistration.serializer()).readObject(builder, field, fieldRegistration);
int index = fieldNames.indexOf(field.getName());
constructorParam.set(index, readObject);
int index = originFields.indexOf(field);
constructorParams[index] = readObject;
}

builder.append(packetClazz.getName() + " packet=new " + packetClazz.getName() + "(" + constructorParam.stream().collect(Collectors.joining(StringUtils.COMMA)) + ");");
builder.append(packetClazz.getName() + " packet=new " + packetClazz.getName() + "(" + String.join(StringUtils.COMMA, constructorParams) + ");");
} else {
var fields = registration.getFields();
builder.append(packetClazz.getName() + " packet=new " + packetClazz.getName() + "();");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,43 @@ public Object read(ByteBuf byteBuf) {
if (length == 0) {
return null;
}
Object object = ReflectionUtils.newInstance(constructor);
Object object = null;

var readIndex = byteBuf.readerIndex();
for (int i = 0, j = fields.length; i < j; i++) {
Field field = fields[i];
// 协议向后兼容
if (field.isAnnotationPresent(Compatible.class)) {
if (length == -1 || byteBuf.readerIndex() - readIndex >= length) {
break;
var packetClazz = constructor.getDeclaringClass();
if (packetClazz.isRecord()) {
var originFields = ProtocolAnalysis.getFields(packetClazz);
var constructorParams = new Object[originFields.size()];
for (int i = 0, j = fields.length; i < j; i++) {
Field field = fields[i];
// 协议向后兼容
if (field.isAnnotationPresent(Compatible.class)) {
if (length == -1 || byteBuf.readerIndex() - readIndex >= length) {
break;
}
}
IFieldRegistration packetFieldRegistration = fieldRegistrations[i];
ISerializer serializer = packetFieldRegistration.serializer();
Object fieldValue = serializer.readObject(byteBuf, packetFieldRegistration);
var index = originFields.indexOf(field);
constructorParams[index] = fieldValue;
}
object = ReflectionUtils.newInstance(constructor, constructorParams);
} else {
object = ReflectionUtils.newInstance(constructor);
for (int i = 0, j = fields.length; i < j; i++) {
Field field = fields[i];
// 协议向后兼容
if (field.isAnnotationPresent(Compatible.class)) {
if (length == -1 || byteBuf.readerIndex() - readIndex >= length) {
break;
}
}
IFieldRegistration packetFieldRegistration = fieldRegistrations[i];
ISerializer serializer = packetFieldRegistration.serializer();
Object fieldValue = serializer.readObject(byteBuf, packetFieldRegistration);
ReflectionUtils.setField(field, object, fieldValue);
}
IFieldRegistration packetFieldRegistration = fieldRegistrations[i];
ISerializer serializer = packetFieldRegistration.serializer();
Object fieldValue = serializer.readObject(byteBuf, packetFieldRegistration);
ReflectionUtils.setField(field, object, fieldValue);
}

if (length > 0) {
Expand Down
19 changes: 10 additions & 9 deletions protocol/src/main/java/com/zfoo/protocol/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,23 @@ public static <T> T newInstance(Class<T> clazz) {
try {
return newInstance(clazz.getDeclaredConstructor());
} catch (NoSuchMethodException e) {
throw new RunException("[{}]无法被实例化", clazz);
throw new RunException("[{}] cannot be instantiated", clazz);
}
}

public static <T> T newInstance(Constructor<T> constructor) {
try {
return constructor.newInstance();
} catch (Exception e) {
throw new RunException("[{}]无法被实例化", constructor);
throw new RunException("[{}] cannot be instantiated", constructor);
}
}

public static <T> T newInstance(Constructor<T> constructor, Object ... initargs) {
try {
return constructor.newInstance(initargs);
} catch (Exception e) {
throw new RunException("[{}] cannot be instantiated", constructor);
}
}

Expand Down Expand Up @@ -336,12 +344,5 @@ public static <T> Constructor<T> getConstructor(Class<T> clazz, Class[] params)
}
}

public static <T> T newInstance(Constructor<T> constructor, Object[] params) {
try {
return constructor.newInstance(params);
} catch (Exception e) {
throw new RunException("[{}]无法被实例化", constructor);
}
}
}

0 comments on commit 792cdfb

Please sign in to comment.