diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/java/CodeGenerateJava.java b/protocol/src/main/java/com/zfoo/protocol/serializer/java/CodeGenerateJava.java index 2f44a58b5..c8fffd6df 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/java/CodeGenerateJava.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/java/CodeGenerateJava.java @@ -90,7 +90,7 @@ public void mergerProtocol(List registrations) throws IOEx for (var registration : registrations) { var protocol_id = registration.protocolId(); var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); - protocol_manager_registrations.append(StringUtils.format("protocols[{}] = Protocols.registration{};", protocol_id, protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocols[{}] = new Protocols.{}Registration();", protocol_id, protocol_name)).append(LS); protocol_manager_registrations.append(StringUtils.format("protocolIdMap.put(Protocols.{}.class, (short){});", protocol_name, protocol_id)).append(LS); } @@ -109,7 +109,7 @@ public void mergerProtocol(List registrations) throws IOEx // protocol protocol_class.append(protocol_class(registration)).append(LS); // registration - protocol_registration.append(protocol_registration(registration)).append(LS); + protocol_registration.append("public static " + protocol_registration(registration)).append(LS); } var protocolTemplate = ClassUtils.getFileFromClassPathToString("java/ProtocolsTemplate.java"); var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( @@ -135,7 +135,7 @@ public void foldProtocol(List registrations) throws IOExce var protocol_id = registration.protocolId(); var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); protocol_imports.append(StringUtils.format("import {}.{}.{};", protocolPackage, GenerateProtocolPath.protocolPathPeriod(protocol_id), protocol_name)).append(LS); - protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}.registration{};", protocol_id, protocol_name, protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}.{}Registration;", protocol_id, protocol_name, protocol_name)).append(LS); protocol_manager_registrations.append(StringUtils.format("protocolIdMap.put({}.class, (short){});", protocol_name, protocol_id)).append(LS); } @@ -155,9 +155,7 @@ public void foldProtocol(List registrations) throws IOExce var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( CodeTemplatePlaceholder.protocol_root_path, protocol_root_path , CodeTemplatePlaceholder.protocol_imports, protocol_imports_fold(registration) - , CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.Java) - , CodeTemplatePlaceholder.protocol_name, protocol_name - , CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration) + , CodeTemplatePlaceholder.protocol_class, protocol_class_fold(registration) , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) )); var outputPath = StringUtils.format("{}/{}/{}.java", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name); @@ -177,7 +175,7 @@ public void defaultProtocol(List registrations) throws IOE for (var registration : registrations) { var protocol_id = registration.protocolId(); var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); - protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}.registration{};", protocol_id, protocol_name, protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("protocols[{}] = new {}Registration();", protocol_id, protocol_name)).append(LS); protocol_manager_registrations.append(StringUtils.format("protocolIdMap.put({}.class, (short){});", protocol_name, protocol_id)).append(LS); } @@ -196,9 +194,7 @@ public void defaultProtocol(List registrations) throws IOE var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( CodeTemplatePlaceholder.protocol_root_path, protocol_root_path , CodeTemplatePlaceholder.protocol_imports, StringUtils.EMPTY - , CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.Java) - , CodeTemplatePlaceholder.protocol_name, protocol_name - , CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration) + , CodeTemplatePlaceholder.protocol_class, protocol_class(registration) , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) )); var outputPath = StringUtils.format("{}/{}.java", protocolOutputPath, protocol_name); @@ -224,6 +220,19 @@ private void createTemplateFile() { } } + private String protocol_class_fold(ProtocolRegistration registration) { + var protocol_id = registration.protocolId(); + var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + var protocolTemplate = ClassUtils.getFileFromClassPathToString("java/ProtocolClassFoldTemplate.java"); + var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( + CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.Java) + , CodeTemplatePlaceholder.protocol_name, protocol_name + , CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id) + , CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration) + )); + return formatProtocolTemplate; + } + private String protocol_class(ProtocolRegistration registration) { var protocol_id = registration.protocolId(); var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); @@ -233,7 +242,6 @@ private String protocol_class(ProtocolRegistration registration) { , CodeTemplatePlaceholder.protocol_name, protocol_name , CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id) , CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration) - , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) )); return formatProtocolTemplate; } diff --git a/protocol/src/main/resources/java/ProtocolClassFoldTemplate.java b/protocol/src/main/resources/java/ProtocolClassFoldTemplate.java new file mode 100644 index 000000000..5881538e0 --- /dev/null +++ b/protocol/src/main/resources/java/ProtocolClassFoldTemplate.java @@ -0,0 +1,5 @@ +${protocol_note} +public class ${protocol_name} { + ${protocol_field_definition} + public static final IProtocolRegistration ${protocol_name}Registration = new ${protocol_name}Registration(); +} \ No newline at end of file diff --git a/protocol/src/main/resources/java/ProtocolRegistrationTemplate.java b/protocol/src/main/resources/java/ProtocolRegistrationTemplate.java index 000ded95d..956fa17e9 100644 --- a/protocol/src/main/resources/java/ProtocolRegistrationTemplate.java +++ b/protocol/src/main/resources/java/ProtocolRegistrationTemplate.java @@ -1,4 +1,4 @@ -public static final IProtocolRegistration registration${protocol_name} = new IProtocolRegistration() { +class ${protocol_name}Registration implements IProtocolRegistration { @Override public short protocolId() { return ${protocol_id}; diff --git a/protocol/src/main/resources/java/ProtocolTemplate.java b/protocol/src/main/resources/java/ProtocolTemplate.java index 081db0bc0..ab3b00b0a 100644 --- a/protocol/src/main/resources/java/ProtocolTemplate.java +++ b/protocol/src/main/resources/java/ProtocolTemplate.java @@ -1,9 +1,7 @@ ${protocol_root_path} ${protocol_imports} import java.util.*; -${protocol_note} -public class ${protocol_name} { - ${protocol_field_definition} - ${protocol_registration} -} \ No newline at end of file +${protocol_class} + +${protocol_registration} \ No newline at end of file