mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-14 14:27:38 +00:00
feat[enhance]: compatible field of inside protocol class
This commit is contained in:
@@ -35,6 +35,10 @@ import java.util.Set;
|
||||
*/
|
||||
public abstract class ByteBufUtils {
|
||||
|
||||
public static final Double ZERO_DOUBLE = Double.valueOf(0D);
|
||||
public static final Float ZERO_FLOAT = Float.valueOf(0F);
|
||||
|
||||
|
||||
public static void adjustPadding(ByteBuf byteBuf, int predictionLength, int beforeWriteIndex) {
|
||||
// 因为写入的是可变长的int,如果预留的位置过多,则清除多余的位置
|
||||
var currentWriteIndex = byteBuf.writerIndex();
|
||||
|
||||
@@ -27,11 +27,9 @@ import io.netty.buffer.ByteBuf;
|
||||
import javassist.*;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 对应于ProtocolRegistration
|
||||
@@ -78,7 +76,6 @@ public abstract class EnhanceUtils {
|
||||
tempEnhanceSerializerMap.put(LongSerializer.INSTANCE, new EnhanceLongSerializer());
|
||||
tempEnhanceSerializerMap.put(FloatSerializer.INSTANCE, new EnhanceFloatSerializer());
|
||||
tempEnhanceSerializerMap.put(DoubleSerializer.INSTANCE, new EnhanceDoubleSerializer());
|
||||
tempEnhanceSerializerMap.put(CharSerializer.INSTANCE, new EnhanceCharSerializer());
|
||||
tempEnhanceSerializerMap.put(StringSerializer.INSTANCE, new EnhanceStringSerializer());
|
||||
tempEnhanceSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new EnhanceObjectProtocolSerializer());
|
||||
tempEnhanceSerializerMap.put(ListSerializer.INSTANCE, new EnhanceListSerializer());
|
||||
@@ -185,8 +182,14 @@ public abstract class EnhanceUtils {
|
||||
var packetClazz = constructor.getDeclaringClass();
|
||||
|
||||
var builder = new StringBuilder();
|
||||
builder.append("{").append(packetClazz.getName() + " packet = (" + packetClazz.getName() + ")$2;");
|
||||
builder.append("if(ByteBufUtils.writePacketFlag($1, packet)){").append("return;}");
|
||||
builder.append("{");
|
||||
builder.append(StringUtils.format("{} packet = ({})$2;", packetClazz.getName(), packetClazz.getName()));
|
||||
if (registration.isCompatible()) {
|
||||
builder.append("int beforeWriteIndex = $1.writerIndex();");
|
||||
builder.append(StringUtils.format("{}.writeInt($1, {});", byteBufUtils, registration.getPredictionLength()));
|
||||
} else {
|
||||
builder.append("$1.writeByte(1);");
|
||||
}
|
||||
for (var i = 0; i < fields.length; i++) {
|
||||
var field = fields[i];
|
||||
var fieldRegistration = fieldRegistrations[i];
|
||||
@@ -199,6 +202,9 @@ public abstract class EnhanceUtils {
|
||||
.writeObject(builder, StringUtils.format("packet.{}()", FieldUtils.fieldToGetMethod(packetClazz, field)), field, fieldRegistration);
|
||||
}
|
||||
}
|
||||
if (registration.isCompatible()) {
|
||||
builder.append(StringUtils.format("{}.adjustPadding($1, {}, beforeWriteIndex);", byteBufUtils, registration.getPredictionLength()));
|
||||
}
|
||||
builder.append("}");
|
||||
return builder.toString();
|
||||
}
|
||||
@@ -209,7 +215,12 @@ public abstract class EnhanceUtils {
|
||||
var fieldRegistrations = registration.getFieldRegistrations();
|
||||
|
||||
var builder = new StringBuilder();
|
||||
builder.append("{").append("if(!" + EnhanceUtils.byteBufUtilsReadBoolean + "){").append("return null;}");
|
||||
builder.append("{");
|
||||
builder.append(StringUtils.format("int length = {}.readInt($1);", byteBufUtils));
|
||||
builder.append("if(length==0){return null;}");
|
||||
if (registration.isCompatible()) {
|
||||
builder.append("int readIndex = $1.readerIndex();");
|
||||
}
|
||||
var packetClazz = constructor.getDeclaringClass();
|
||||
if (packetClazz.isRecord()) {
|
||||
var fields = registration.getFields();
|
||||
@@ -238,11 +249,10 @@ public abstract class EnhanceUtils {
|
||||
var fieldRegistration = fieldRegistrations[i];
|
||||
// protocol backwards compatibility,协议向后兼容
|
||||
if (field.isAnnotationPresent(Compatible.class)) {
|
||||
builder.append("if(!$1.isReadable()){ return packet; }");
|
||||
builder.append("if (length == -1 || $1.readerIndex() >= length + readIndex) {");
|
||||
}
|
||||
|
||||
var readObject = enhanceSerializer(fieldRegistration.serializer()).readObject(builder, field, fieldRegistration);
|
||||
|
||||
if (Modifier.isPublic(field.getModifiers())) {
|
||||
builder.append(StringUtils.format("packet.{}={};", field.getName(), readObject));
|
||||
} else {
|
||||
|
||||
@@ -83,8 +83,6 @@ public class ProtocolAnalysis {
|
||||
baseSerializerMap.put(Float.class, FloatSerializer.INSTANCE);
|
||||
baseSerializerMap.put(double.class, DoubleSerializer.INSTANCE);
|
||||
baseSerializerMap.put(Double.class, DoubleSerializer.INSTANCE);
|
||||
baseSerializerMap.put(char.class, CharSerializer.INSTANCE);
|
||||
baseSerializerMap.put(Character.class, CharSerializer.INSTANCE);
|
||||
baseSerializerMap.put(String.class, StringSerializer.INSTANCE);
|
||||
}
|
||||
|
||||
|
||||
@@ -82,7 +82,8 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
if (compatible) {
|
||||
ByteBufUtils.writeInt(byteBuf, predictionLength);
|
||||
} else {
|
||||
ByteBufUtils.writeInt(byteBuf, -1);
|
||||
// equals with ByteBufUtils.writeInt(byteBuf, -1);
|
||||
byteBuf.writeByte(1);
|
||||
}
|
||||
|
||||
for (int i = 0, length = fields.length; i < length; i++) {
|
||||
@@ -95,29 +96,7 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
|
||||
if (compatible) {
|
||||
// 因为写入的是可变长的int,如果预留的位置过多,则清除多余的位置
|
||||
var currentWriteIndex = byteBuf.writerIndex();
|
||||
var predictionCount = ByteBufUtils.writeIntCount(predictionLength);
|
||||
var length = currentWriteIndex - beforeWriteIndex - predictionCount;
|
||||
var lengthCount = ByteBufUtils.writeIntCount(length);
|
||||
var padding = lengthCount - predictionCount;
|
||||
if (padding == 0) {
|
||||
byteBuf.writerIndex(beforeWriteIndex);
|
||||
ByteBufUtils.writeInt(byteBuf, length);
|
||||
byteBuf.writerIndex(currentWriteIndex);
|
||||
} else if (padding < 0) {
|
||||
var retainedByteBuf = byteBuf.retainedSlice(currentWriteIndex - length, length);
|
||||
byteBuf.writerIndex(beforeWriteIndex);
|
||||
ByteBufUtils.writeInt(byteBuf, length);
|
||||
byteBuf.writeBytes(retainedByteBuf);
|
||||
ReferenceCountUtil.release(retainedByteBuf);
|
||||
} else {
|
||||
var retainedByteBuf = byteBuf.retainedSlice(currentWriteIndex - length, length);
|
||||
var bytes = ByteBufUtils.readAllBytes(retainedByteBuf);
|
||||
byteBuf.writerIndex(beforeWriteIndex);
|
||||
ByteBufUtils.writeInt(byteBuf, length);
|
||||
byteBuf.writeBytes(bytes);
|
||||
ReferenceCountUtil.release(retainedByteBuf);
|
||||
}
|
||||
ByteBufUtils.adjustPadding(byteBuf, predictionLength, beforeWriteIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,7 +120,7 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
|
||||
// 协议向后兼容
|
||||
if (field.isAnnotationPresent(Compatible.class)) {
|
||||
if (length == -1 || byteBuf.readerIndex() - readIndex >= length) {
|
||||
if (length == -1 || byteBuf.readerIndex() >= length + readIndex) {
|
||||
constructorParams[index] = packetFieldRegistration.defaultValue();
|
||||
continue;
|
||||
}
|
||||
@@ -216,4 +195,19 @@ public class ProtocolRegistration implements IProtocolRegistration {
|
||||
this.constructor = constructor;
|
||||
}
|
||||
|
||||
public boolean isCompatible() {
|
||||
return compatible;
|
||||
}
|
||||
|
||||
public void setCompatible(boolean compatible) {
|
||||
this.compatible = compatible;
|
||||
}
|
||||
|
||||
public int getPredictionLength() {
|
||||
return predictionLength;
|
||||
}
|
||||
|
||||
public void setPredictionLength(int predictionLength) {
|
||||
this.predictionLength = predictionLength;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +71,6 @@ public abstract class GenerateCppUtils {
|
||||
cppSerializerMap.put(LongSerializer.INSTANCE, new CppLongSerializer());
|
||||
cppSerializerMap.put(FloatSerializer.INSTANCE, new CppFloatSerializer());
|
||||
cppSerializerMap.put(DoubleSerializer.INSTANCE, new CppDoubleSerializer());
|
||||
cppSerializerMap.put(CharSerializer.INSTANCE, new CppCharSerializer());
|
||||
cppSerializerMap.put(StringSerializer.INSTANCE, new CppStringSerializer());
|
||||
cppSerializerMap.put(ArraySerializer.INSTANCE, new CppArraySerializer());
|
||||
cppSerializerMap.put(ListSerializer.INSTANCE, new CppListSerializer());
|
||||
|
||||
@@ -65,7 +65,6 @@ public abstract class GenerateCsUtils {
|
||||
csSerializerMap.put(LongSerializer.INSTANCE, new CsLongSerializer());
|
||||
csSerializerMap.put(FloatSerializer.INSTANCE, new CsFloatSerializer());
|
||||
csSerializerMap.put(DoubleSerializer.INSTANCE, new CsDoubleSerializer());
|
||||
csSerializerMap.put(CharSerializer.INSTANCE, new CsCharSerializer());
|
||||
csSerializerMap.put(StringSerializer.INSTANCE, new CsStringSerializer());
|
||||
csSerializerMap.put(ArraySerializer.INSTANCE, new CsArraySerializer());
|
||||
csSerializerMap.put(ListSerializer.INSTANCE, new CsListSerializer());
|
||||
|
||||
+10
@@ -79,4 +79,14 @@ public class EnhanceArraySerializer implements IEnhanceSerializer {
|
||||
return array;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var arrayField = (ArrayField) fieldRegistration;
|
||||
var arrayName = CutDownArraySerializer.getInstance().getArrayClassName(arrayField);
|
||||
var array = "array" + GenerateProtocolFile.index.getAndIncrement();
|
||||
builder.append(StringUtils.format("{}[] {} = new {}[0];", arrayName, array, arrayName));
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+11
@@ -45,4 +45,15 @@ public class EnhanceBooleanSerializer implements IEnhanceSerializer {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
|
||||
if (isPrimitiveField(field)) {
|
||||
builder.append(StringUtils.format("boolean {} = false;", result));
|
||||
} else {
|
||||
builder.append(StringUtils.format("Boolean {} = Boolean.FALSE;", result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
@@ -45,4 +45,15 @@ public class EnhanceByteSerializer implements IEnhanceSerializer {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
|
||||
if (isPrimitiveField(field)) {
|
||||
builder.append(StringUtils.format("byte {} = 0;", result));
|
||||
} else {
|
||||
builder.append(StringUtils.format("Byte {} = Byte.valueOf((byte) 0);", result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-48
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.serializer.enhance;
|
||||
|
||||
import com.zfoo.protocol.generate.GenerateProtocolFile;
|
||||
import com.zfoo.protocol.registration.EnhanceUtils;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
public class EnhanceCharSerializer implements IEnhanceSerializer {
|
||||
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, Field field, IFieldRegistration fieldRegistration) {
|
||||
if (isPrimitiveField(field)) {
|
||||
builder.append(StringUtils.format("{}.writeChar($1,{});", EnhanceUtils.byteBufUtils, objectStr));
|
||||
} else {
|
||||
builder.append(StringUtils.format("{}.writeCharBox($1, (Character){});", EnhanceUtils.byteBufUtils, objectStr));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
|
||||
if (isPrimitiveField(field)) {
|
||||
builder.append(StringUtils.format("char {} = {}.readChar($1);", result, EnhanceUtils.byteBufUtils));
|
||||
} else {
|
||||
builder.append(StringUtils.format("Character {} = {}.readCharBox($1);", result, EnhanceUtils.byteBufUtils));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
+11
@@ -45,4 +45,15 @@ public class EnhanceDoubleSerializer implements IEnhanceSerializer {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
|
||||
if (isPrimitiveField(field)) {
|
||||
builder.append(StringUtils.format("double {} = 0D;", result));
|
||||
} else {
|
||||
builder.append(StringUtils.format("Double {} = {}.ZERO_DOUBLE;", result, EnhanceUtils.byteBufUtils));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+10
@@ -45,4 +45,14 @@ public class EnhanceFloatSerializer implements IEnhanceSerializer {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
|
||||
if (isPrimitiveField(field)) {
|
||||
builder.append(StringUtils.format("float {} = 0F;", result));
|
||||
} else {
|
||||
builder.append(StringUtils.format("Float {} = {}.ZERO_FLOAT;", result, EnhanceUtils.byteBufUtils));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,4 +45,15 @@ public class EnhanceIntSerializer implements IEnhanceSerializer {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
|
||||
if (isPrimitiveField(field)) {
|
||||
builder.append(StringUtils.format("int {} = 0;", result));
|
||||
} else {
|
||||
builder.append(StringUtils.format("Integer {} = Integer.valueOf(0);", result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -76,4 +76,12 @@ public class EnhanceListSerializer implements IEnhanceSerializer {
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var listField = (ListField) fieldRegistration;
|
||||
var list = "list" + GenerateProtocolFile.index.getAndIncrement();
|
||||
builder.append(StringUtils.format("List {} = CollectionUtils.newList(0);", list));
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
@@ -45,4 +45,15 @@ public class EnhanceLongSerializer implements IEnhanceSerializer {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
|
||||
if (isPrimitiveField(field)) {
|
||||
builder.append(StringUtils.format("long {} = 0;", result));
|
||||
} else {
|
||||
builder.append(StringUtils.format("Long {} = Long.valueOf(0);", result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -91,4 +91,12 @@ public class EnhanceMapSerializer implements IEnhanceSerializer {
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var mapField = (MapField) fieldRegistration;
|
||||
var map = "map" + GenerateProtocolFile.index.getAndIncrement();
|
||||
builder.append(StringUtils.format("Map {} = CollectionUtils.newMap(0);", map));
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+9
@@ -44,6 +44,15 @@ public class EnhanceObjectProtocolSerializer implements IEnhanceSerializer {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var objectProtocolField = (ObjectProtocolField) fieldRegistration;
|
||||
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
|
||||
var protocolName = getProtocolClassCanonicalName(objectProtocolField.getProtocolId());
|
||||
builder.append(StringUtils.format("{} {} = null;", protocolName, result));
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String getProtocolClassSimpleName(short protocolId) {
|
||||
return ProtocolManager.getProtocol(protocolId).protocolConstructor().getDeclaringClass().getSimpleName();
|
||||
}
|
||||
|
||||
@@ -74,4 +74,12 @@ public class EnhanceSetSerializer implements IEnhanceSerializer {
|
||||
return set;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var setField = (SetField) fieldRegistration;
|
||||
var set = "set" + GenerateProtocolFile.index.getAndIncrement();
|
||||
builder.append(StringUtils.format("Set {} = CollectionUtils.newSet(0);", set));
|
||||
return set;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
@@ -45,4 +45,15 @@ public class EnhanceShortSerializer implements IEnhanceSerializer {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
|
||||
if (isPrimitiveField(field)) {
|
||||
builder.append(StringUtils.format("short {} = 0;", result));
|
||||
} else {
|
||||
builder.append(StringUtils.format("Short {} = Short.valueOf((short) 0);", result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
@@ -37,4 +37,11 @@ public class EnhanceStringSerializer implements IEnhanceSerializer {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration) {
|
||||
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
|
||||
builder.append(StringUtils.format("String {} = \"\"", result));
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,4 +42,6 @@ public interface IEnhanceSerializer {
|
||||
*/
|
||||
String readObject(StringBuilder builder, Field field, IFieldRegistration fieldRegistration);
|
||||
|
||||
String defaultValue(StringBuilder builder, Field field, IFieldRegistration fieldRegistration);
|
||||
|
||||
}
|
||||
|
||||
@@ -76,7 +76,6 @@ public abstract class GenerateGdUtils {
|
||||
gdSerializerMap.put(LongSerializer.INSTANCE, new GdLongSerializer());
|
||||
gdSerializerMap.put(FloatSerializer.INSTANCE, new GdFloatSerializer());
|
||||
gdSerializerMap.put(DoubleSerializer.INSTANCE, new GdDoubleSerializer());
|
||||
gdSerializerMap.put(CharSerializer.INSTANCE, new GdCharSerializer());
|
||||
gdSerializerMap.put(StringSerializer.INSTANCE, new GdStringSerializer());
|
||||
gdSerializerMap.put(ArraySerializer.INSTANCE, new GdArraySerializer());
|
||||
gdSerializerMap.put(ListSerializer.INSTANCE, new GdListSerializer());
|
||||
|
||||
@@ -66,7 +66,6 @@ public abstract class GenerateGoUtils {
|
||||
goSerializerMap.put(LongSerializer.INSTANCE, new GoLongSerializer());
|
||||
goSerializerMap.put(FloatSerializer.INSTANCE, new GoFloatSerializer());
|
||||
goSerializerMap.put(DoubleSerializer.INSTANCE, new GoDoubleSerializer());
|
||||
goSerializerMap.put(CharSerializer.INSTANCE, new GoCharSerializer());
|
||||
goSerializerMap.put(StringSerializer.INSTANCE, new GoStringSerializer());
|
||||
goSerializerMap.put(ArraySerializer.INSTANCE, new GoArraySerializer());
|
||||
goSerializerMap.put(ListSerializer.INSTANCE, new GoListSerializer());
|
||||
|
||||
@@ -65,7 +65,6 @@ public abstract class GenerateJsUtils {
|
||||
jsSerializerMap.put(LongSerializer.INSTANCE, new JsLongSerializer());
|
||||
jsSerializerMap.put(FloatSerializer.INSTANCE, new JsFloatSerializer());
|
||||
jsSerializerMap.put(DoubleSerializer.INSTANCE, new JsDoubleSerializer());
|
||||
jsSerializerMap.put(CharSerializer.INSTANCE, new JsCharSerializer());
|
||||
jsSerializerMap.put(StringSerializer.INSTANCE, new JsStringSerializer());
|
||||
jsSerializerMap.put(ArraySerializer.INSTANCE, new JsArraySerializer());
|
||||
jsSerializerMap.put(ListSerializer.INSTANCE, new JsListSerializer());
|
||||
|
||||
@@ -64,7 +64,6 @@ public abstract class GenerateLuaUtils {
|
||||
luaSerializerMap.put(LongSerializer.INSTANCE, new LuaLongSerializer());
|
||||
luaSerializerMap.put(FloatSerializer.INSTANCE, new LuaFloatSerializer());
|
||||
luaSerializerMap.put(DoubleSerializer.INSTANCE, new LuaDoubleSerializer());
|
||||
luaSerializerMap.put(CharSerializer.INSTANCE, new LuaCharSerializer());
|
||||
luaSerializerMap.put(StringSerializer.INSTANCE, new LuaStringSerializer());
|
||||
luaSerializerMap.put(ArraySerializer.INSTANCE, new LuaArraySerializer());
|
||||
luaSerializerMap.put(ListSerializer.INSTANCE, new LuaListSerializer());
|
||||
|
||||
-2
@@ -283,8 +283,6 @@ public abstract class GenerateProtobufUtils {
|
||||
return "float";
|
||||
} else if (serializer instanceof DoubleSerializer) {
|
||||
return "double";
|
||||
} else if (serializer instanceof CharSerializer) {
|
||||
return "char";
|
||||
} else if (serializer instanceof StringSerializer) {
|
||||
return "string";
|
||||
}
|
||||
|
||||
@@ -66,7 +66,6 @@ public abstract class GeneratePyUtils {
|
||||
pySerializerMap.put(LongSerializer.INSTANCE, new PyLongSerializer());
|
||||
pySerializerMap.put(FloatSerializer.INSTANCE, new PyFloatSerializer());
|
||||
pySerializerMap.put(DoubleSerializer.INSTANCE, new PyDoubleSerializer());
|
||||
pySerializerMap.put(CharSerializer.INSTANCE, new PyCharSerializer());
|
||||
pySerializerMap.put(StringSerializer.INSTANCE, new PyStringSerializer());
|
||||
pySerializerMap.put(ArraySerializer.INSTANCE, new PyArraySerializer());
|
||||
pySerializerMap.put(ListSerializer.INSTANCE, new PyListSerializer());
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.serializer.reflect;
|
||||
|
||||
import com.zfoo.protocol.buffer.ByteBufUtils;
|
||||
import com.zfoo.protocol.registration.field.IFieldRegistration;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
public class CharSerializer implements ISerializer {
|
||||
|
||||
public static final CharSerializer INSTANCE = new CharSerializer();
|
||||
|
||||
@Override
|
||||
public void writeObject(ByteBuf buffer, Object object, IFieldRegistration fieldRegistration) {
|
||||
ByteBufUtils.writeCharBox(buffer, (Character) object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object readObject(ByteBuf buffer, IFieldRegistration fieldRegistration) {
|
||||
return ByteBufUtils.readCharBox(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return Character.MIN_VALUE;
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,6 @@ 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) {
|
||||
@@ -38,6 +37,6 @@ public class DoubleSerializer implements ISerializer {
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return ZERO;
|
||||
return ByteBufUtils.ZERO_DOUBLE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
@@ -36,6 +36,6 @@ public class FloatSerializer implements ISerializer {
|
||||
|
||||
@Override
|
||||
public Object defaultValue(IFieldRegistration fieldRegistration) {
|
||||
return ZERO;
|
||||
return ByteBufUtils.ZERO_FLOAT;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,6 @@ public abstract class GenerateTsUtils {
|
||||
tsSerializerMap.put(LongSerializer.INSTANCE, new TsLongSerializer());
|
||||
tsSerializerMap.put(FloatSerializer.INSTANCE, new TsFloatSerializer());
|
||||
tsSerializerMap.put(DoubleSerializer.INSTANCE, new TsDoubleSerializer());
|
||||
tsSerializerMap.put(CharSerializer.INSTANCE, new TsCharSerializer());
|
||||
tsSerializerMap.put(StringSerializer.INSTANCE, new TsStringSerializer());
|
||||
tsSerializerMap.put(ArraySerializer.INSTANCE, new TsArraySerializer());
|
||||
tsSerializerMap.put(ListSerializer.INSTANCE, new TsListSerializer());
|
||||
|
||||
@@ -319,8 +319,6 @@ public class BenchmarkTesting {
|
||||
public static final long longValue = 9999999999999999L;
|
||||
public static final float floatValue = 99999999.9F;
|
||||
public static final double doubleValue = 99999999.9D;
|
||||
public static final char charValue = 'c';
|
||||
public static final String charValueString = "c";
|
||||
public static final String stringValue = "hello";
|
||||
|
||||
// 数组类型
|
||||
@@ -408,10 +406,6 @@ public class BenchmarkTesting {
|
||||
complexObject.setGg(true);
|
||||
complexObject.setGgg(booleanArray);
|
||||
complexObject.setGggg(booleanBoxArray);
|
||||
complexObject.setH(charValue);
|
||||
complexObject.setHh(charValue);
|
||||
complexObject.setHhh(charArray);
|
||||
complexObject.setHhhh(charBoxArray);
|
||||
complexObject.setJj(stringValue);
|
||||
complexObject.setJjj(stringArray);
|
||||
complexObject.setKk(objectA);
|
||||
@@ -536,8 +530,6 @@ public class BenchmarkTesting {
|
||||
protobufComplexBuilder.setGg(true);
|
||||
protobufComplexBuilder.addAllGgg(listWithBoolean);
|
||||
protobufComplexBuilder.addAllGggg(listWithBoolean);
|
||||
protobufComplexBuilder.setH(charValueString);
|
||||
protobufComplexBuilder.setHh(charValueString);
|
||||
protobufComplexBuilder.addAllHhh(listWithString);
|
||||
protobufComplexBuilder.addAllHhhh(listWithString);
|
||||
protobufComplexBuilder.setJj(stringValue);
|
||||
|
||||
@@ -63,10 +63,6 @@ public class ComplexObject {
|
||||
private boolean[] ggg;
|
||||
private Boolean[] gggg;
|
||||
|
||||
private char h;
|
||||
private Character hh;
|
||||
private char[] hhh;
|
||||
private Character[] hhhh;
|
||||
|
||||
private String jj;
|
||||
private String[] jjj;
|
||||
@@ -323,38 +319,6 @@ public class ComplexObject {
|
||||
this.gggg = gggg;
|
||||
}
|
||||
|
||||
public char getH() {
|
||||
return h;
|
||||
}
|
||||
|
||||
public void setH(char h) {
|
||||
this.h = h;
|
||||
}
|
||||
|
||||
public Character getHh() {
|
||||
return hh;
|
||||
}
|
||||
|
||||
public void setHh(Character hh) {
|
||||
this.hh = hh;
|
||||
}
|
||||
|
||||
public char[] getHhh() {
|
||||
return hhh;
|
||||
}
|
||||
|
||||
public void setHhh(char[] hhh) {
|
||||
this.hhh = hhh;
|
||||
}
|
||||
|
||||
public Character[] getHhhh() {
|
||||
return hhhh;
|
||||
}
|
||||
|
||||
public void setHhhh(Character[] hhhh) {
|
||||
this.hhhh = hhhh;
|
||||
}
|
||||
|
||||
public String getJj() {
|
||||
return jj;
|
||||
}
|
||||
@@ -535,7 +499,6 @@ public class ComplexObject {
|
||||
Float.compare(that.e, e) == 0 &&
|
||||
Double.compare(that.f, f) == 0 &&
|
||||
g == that.g &&
|
||||
h == that.h &&
|
||||
Objects.equals(aa, that.aa) &&
|
||||
Arrays.equals(aaa, that.aaa) &&
|
||||
Arrays.equals(aaaa, that.aaaa) &&
|
||||
@@ -557,9 +520,6 @@ public class ComplexObject {
|
||||
Objects.equals(gg, that.gg) &&
|
||||
Arrays.equals(ggg, that.ggg) &&
|
||||
Arrays.equals(gggg, that.gggg) &&
|
||||
Objects.equals(hh, that.hh) &&
|
||||
Arrays.equals(hhh, that.hhh) &&
|
||||
Arrays.equals(hhhh, that.hhhh) &&
|
||||
Objects.equals(jj, that.jj) &&
|
||||
Arrays.equals(jjj, that.jjj) &&
|
||||
Objects.equals(kk, that.kk) &&
|
||||
@@ -583,7 +543,7 @@ public class ComplexObject {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = Objects.hash(a, aa, b, bb, c, cc, d, dd, e, ee, f, ff, g, gg, h, hh, jj, kk, l, ll, lll, llll, lllll, m, mm, mmm, mmmm, mmmmm, s, ss, sss, ssss, sssss);
|
||||
int result = Objects.hash(a, aa, b, bb, c, cc, d, dd, e, ee, f, ff, g, gg, jj, kk, l, ll, lll, llll, lllll, m, mm, mmm, mmmm, mmmmm, s, ss, sss, ssss, sssss);
|
||||
result = 31 * result + Arrays.hashCode(aaa);
|
||||
result = 31 * result + Arrays.hashCode(aaaa);
|
||||
result = 31 * result + Arrays.hashCode(bbb);
|
||||
@@ -598,8 +558,6 @@ public class ComplexObject {
|
||||
result = 31 * result + Arrays.hashCode(ffff);
|
||||
result = 31 * result + Arrays.hashCode(ggg);
|
||||
result = 31 * result + Arrays.hashCode(gggg);
|
||||
result = 31 * result + Arrays.hashCode(hhh);
|
||||
result = 31 * result + Arrays.hashCode(hhhh);
|
||||
result = 31 * result + Arrays.hashCode(jjj);
|
||||
result = 31 * result + Arrays.hashCode(kkk);
|
||||
return result;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user