mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-25 03:37:26 +00:00
feat[protocol]: reflect serialization support record class
This commit is contained in:
@@ -213,8 +213,8 @@ public abstract class EnhanceUtils {
|
||||
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];
|
||||
@@ -225,11 +225,10 @@ public abstract class EnhanceUtils {
|
||||
}
|
||||
|
||||
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() + "();");
|
||||
|
||||
@@ -126,21 +126,43 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
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) {
|
||||
|
||||
@@ -198,7 +198,7 @@ public abstract class ReflectionUtils {
|
||||
try {
|
||||
return newInstance(clazz.getDeclaredConstructor());
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RunException("[{}]无法被实例化", clazz);
|
||||
throw new RunException("[{}] cannot be instantiated", clazz);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,7 +206,15 @@ public abstract class ReflectionUtils {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,12 +344,5 @@ public abstract class ReflectionUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T newInstance(Constructor<T> constructor, Object[] params) {
|
||||
try {
|
||||
return constructor.newInstance(params);
|
||||
} catch (Exception e) {
|
||||
throw new RunException("[{}]无法被实例化", constructor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user