mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-13 06:15:51 +00:00
fix[protocol]: record class can not initialize error
This commit is contained in:
1 parent
d17c81528b
commit
1e6c69e2c9
17 files changed
+90
-5
No files matched your search
@@ -135,17 +135,19 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
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];
|
||||
var field = fields[i];
|
||||
var index = originFields.indexOf(field);
|
||||
var packetFieldRegistration = fieldRegistrations[i];
|
||||
|
||||
// 协议向后兼容
|
||||
if (field.isAnnotationPresent(Compatible.class)) {
|
||||
if (length == -1 || byteBuf.readerIndex() - readIndex >= length) {
|
||||
break;
|
||||
constructorParams[index] = packetFieldRegistration.defaultValue();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
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);
|
||||
|
||||
@@ -22,6 +22,10 @@ import com.zfoo.protocol.serializer.reflect.ISerializer;
|
||||
*/
|
||||
public interface IFieldRegistration {
|
||||
|
||||
default Object defaultValue() {
|
||||
return serializer().defaultValue(this);
|
||||
}
|
||||
|
||||
ISerializer serializer();
|
||||
|
||||
}
|
||||
@@ -68,4 +68,10 @@ public class ArraySerializer implements ISerializer {
|
||||
return array;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
ArrayField arrayField = (ArrayField) fieldRegistration;
|
||||
return Array.newInstance(arrayField.getType(), 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,4 +33,9 @@ public class BooleanSerializer implements ISerializer {
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
return ByteBufUtils.readBooleanBox(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
}
|
||||
@@ -34,4 +34,9 @@ public class ByteSerializer implements ISerializer {
|
||||
return ByteBufUtils.readByteBox(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return Byte.valueOf((byte) 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,4 +33,9 @@ public class CharSerializer implements ISerializer {
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
return ByteBufUtils.readCharBox(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return Character.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import io.netty.buffer.ByteBuf;
|
||||
public class DoubleSerializer implements ISerializer {
|
||||
|
||||
public static final DoubleSerializer INSTANCE = new DoubleSerializer();
|
||||
public static final Double ZERO = Double.valueOf(0D);
|
||||
|
||||
@Override
|
||||
public void writeObject(ByteBuf buffer, Object object, IFieldRegistration fieldRegistration) {
|
||||
@@ -33,4 +34,10 @@ public class DoubleSerializer implements ISerializer {
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
return ByteBufUtils.readDoubleBox(buffer);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return ZERO;
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ import io.netty.buffer.ByteBuf;
|
||||
public class FloatSerializer implements ISerializer {
|
||||
|
||||
public static final FloatSerializer INSTANCE = new FloatSerializer();
|
||||
|
||||
public static final Float ZERO = Float.valueOf(0F);
|
||||
@Override
|
||||
public void writeObject(ByteBuf buffer, Object object, IFieldRegistration fieldRegistration) {
|
||||
ByteBufUtils.writeFloatBox(buffer, (Float) object);
|
||||
@@ -33,4 +33,9 @@ public class FloatSerializer implements ISerializer {
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
return ByteBufUtils.readFloatBox(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return ZERO;
|
||||
}
|
||||
}
|
||||
@@ -32,4 +32,6 @@ public interface ISerializer {
|
||||
|
||||
Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration);
|
||||
|
||||
Object defaultValue(IFieldRegistration fieldRegistration);
|
||||
|
||||
}
|
||||
@@ -33,4 +33,9 @@ public class IntSerializer implements ISerializer {
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
return ByteBufUtils.readIntBox(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return Integer.valueOf(0);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.registration.field.ListField;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -62,4 +63,9 @@ public class ListSerializer implements ISerializer {
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
@@ -33,4 +33,9 @@ public class LongSerializer implements ISerializer {
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
return ByteBufUtils.readLongBox(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return Long.valueOf(0);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.registration.field.MapField;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -69,4 +70,9 @@ public class MapSerializer implements ISerializer {
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
+5
@@ -44,4 +44,9 @@ public class ObjectProtocolSerializer implements ISerializer {
|
||||
IProtocolRegistration protocol = ProtocolManager.getProtocol(objectProtocolField.getProtocolId());
|
||||
return protocol.read(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.registration.field.SetField;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -64,4 +65,9 @@ public class SetSerializer implements ISerializer {
|
||||
return set;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return new HashSet<>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,4 +33,9 @@ public class ShortSerializer implements ISerializer {
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
return ByteBufUtils.readShortBox(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return Short.valueOf((short) 0);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ package com.zfoo.protocol.serializer.reflect;
|
||||
|
||||
import com.zfoo.protocol.buffer.ByteBufUtils;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
/**
|
||||
@@ -33,4 +34,9 @@ public class StringSerializer implements ISerializer {
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
return ByteBufUtils.readString(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user