Skip to content

Commit

Permalink
ref[java]: refactor java protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 7, 2024
1 parent 572d0bf commit 6ad98bb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void mergerProtocol(List<ProtocolRegistration> 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);
}

Expand All @@ -109,7 +109,7 @@ public void mergerProtocol(List<ProtocolRegistration> 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(
Expand All @@ -135,7 +135,7 @@ public void foldProtocol(List<ProtocolRegistration> 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);
}

Expand All @@ -155,9 +155,7 @@ public void foldProtocol(List<ProtocolRegistration> 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);
Expand All @@ -177,7 +175,7 @@ public void defaultProtocol(List<ProtocolRegistration> 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);
}

Expand All @@ -196,9 +194,7 @@ public void defaultProtocol(List<ProtocolRegistration> 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);
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
${protocol_note}
public class ${protocol_name} {
${protocol_field_definition}
public static final IProtocolRegistration ${protocol_name}Registration = new ${protocol_name}Registration();
}
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
8 changes: 3 additions & 5 deletions protocol/src/main/resources/java/ProtocolTemplate.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
${protocol_root_path}
${protocol_imports}
import java.util.*;
${protocol_note}
public class ${protocol_name} {
${protocol_field_definition}

${protocol_registration}
}
${protocol_class}

${protocol_registration}

0 comments on commit 6ad98bb

Please sign in to comment.