From b6b6a6f9324eade22b050418a143f67fe5c42c21 Mon Sep 17 00:00:00 2001 From: godotg Date: Thu, 12 Oct 2023 22:40:58 +0800 Subject: [PATCH] chore[enhance]: compatible comment --- .../src/main/java/com/zfoo/protocol/anno/Compatible.java | 4 ++++ .../java/com/zfoo/protocol/registration/EnhanceUtils.java | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/anno/Compatible.java b/protocol/src/main/java/com/zfoo/protocol/anno/Compatible.java index 76e8d028f..a71d29806 100644 --- a/protocol/src/main/java/com/zfoo/protocol/anno/Compatible.java +++ b/protocol/src/main/java/com/zfoo/protocol/anno/Compatible.java @@ -25,6 +25,10 @@ @Target({ElementType.FIELD}) public @interface Compatible { + /** + * EN: new compatible field value must be always greater than old compatible field value + * CN: 新的兼容字段值必须始终大于旧的兼容字段 + */ int value(); } diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java b/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java index 138532c79..e072c00de 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java @@ -177,6 +177,7 @@ public static IProtocolRegistration createProtocolRegistration(ProtocolRegistrat private static String writeMethodBody(ProtocolRegistration registration) { var constructor = registration.getConstructor(); var fields = registration.getFields(); + var compatible = registration.isCompatible(); var fieldRegistrations = registration.getFieldRegistrations(); var packetClazz = constructor.getDeclaringClass(); @@ -185,7 +186,7 @@ private static String writeMethodBody(ProtocolRegistration registration) { builder.append("{"); builder.append("if ($2 == null) { $1.writeByte(0); return; }"); builder.append(StringUtils.format("{} packet = ({})$2;", packetClazz.getName(), packetClazz.getName())); - if (registration.isCompatible()) { + if (compatible) { builder.append("int beforeWriteIndex = $1.writerIndex();"); builder.append(StringUtils.format("{}.writeInt($1, {});", byteBufUtils, registration.getPredictionLength())); } else { @@ -203,7 +204,7 @@ private static String writeMethodBody(ProtocolRegistration registration) { .writeObject(builder, StringUtils.format("packet.{}()", FieldUtils.fieldToGetMethod(packetClazz, field)), field, fieldRegistration); } } - if (registration.isCompatible()) { + if (compatible) { builder.append(StringUtils.format("{}.adjustPadding($1, {}, beforeWriteIndex);", byteBufUtils, registration.getPredictionLength())); } builder.append("}");