代码生成枚举实践

1)ftl

package com.elex.service.conf.helper.buffattribute.enums;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 永久属性类型枚举(41xxx)
 * 这个类是自动生成的,不要在里面写逻辑,写了也会被覆盖掉!!!
 */
@Slf4j
public enum EBuffAttributeType {
    NULL(0, false),                                     // 无效值

<#list results as r>
    ${r.en}(${r.attributeType?c}, <#if r.fightType==1>true<#else>false, <#if r.needCache==1>true<#else>false), // ${r.comment}


    ;

    /*** 属性类型*/
    @Getter
    private int attributeType;

    /*** 是否是战斗属性*/
    @Getter
    private boolean fightType = false;

    /*** 标记为true的可以通过UserEffectAndAttributeManager的getAttributeCacheValue方法从缓存中获取属性, 只有参数中类型全是1的才能标记为true,标记错误起服时就检查会抛出异常*/
    @Getter
    private boolean needCache = false;

    EBuffAttributeType(int attributeType, boolean fightType) {
        this.attributeType = attributeType;
        this.fightType = fightType;
    }

    EBuffAttributeType(int attributeType, boolean fightType, boolean needCache) {
        this(attributeType, fightType);
        this.needCache = needCache;
    }

    // 检索
    private static final Map typeMap;

    static {
        Map map = new HashMap<>();
        for (EBuffAttributeType type : values()) {
            if (map.putIfAbsent(type.attributeType, type) != null) {
                log.error("重复的attributeType={}", type.attributeType);
            }
        }
        typeMap = map;
    }

    public static EBuffAttributeType find(int attributeType) {
        return typeMap.get(attributeType);
    }
}

注意:

?c是生成的数字不要带','

2)代码

    public boolean genAttributeCode() throws Exception {

        Collection list = ConfigServiceImpl.getInstant().getConfig(Attribute_formulaConfig.class).getAll();
        List> results = Lists.newArrayList();

        for (Attribute_formulaSpec spec : list) {
            // 英文名
            String en = spec.getName_en();
            // 属性类型
            int attributeType = spec.getId();
            // 注释
            String comment = spec.getName();

            int fightType = spec.getFightType();
            int needCache = spec.getNeedCache();


            results.add(Map.of(
                    "en", en,
                    "attributeType", attributeType,
                    "comment", comment,
                    "fightType", fightType,
                    "needCache", needCache
            ));
        }

        // 排序下
        results.sort(Comparator.comparingInt(c -> (int) c.get("attributeType")));

        // 参数准备
        String PATH = System.getProperty("user.dir");
        final String ENCODING = "UTF-8";

        Map valueMap = Maps.newHashMap();
        valueMap.put("results", results);

        String useTemplatePath = Path.of(PATH, "trunk", "icefire-game", "src", "main", "resources", "template").toString();
        String useTemplateName = "EBuffAttributeType.ftl";
        String outPath = Path.of(PATH, "trunk", "icefire-config", "src", "main", "java").toString();
        String genPackageName = "com.elex.service.conf.helper.buffattribute.enums";
        String genFileName = "EBuffAttributeType.java";

        // 代码生成
        TemplateLoader loader = new FileTemplateLoader(new File(useTemplatePath));
        Configuration cfg = new Configuration();
        cfg.setTemplateLoader(loader);
        cfg.setEncoding(Locale.getDefault(), ENCODING);

        Template template = cfg.getTemplate(useTemplateName, ENCODING);
        template.setOutputEncoding(ENCODING);

        File filePath = new File(outPath, genPackageName.replace(".", File.separator));
        if (!filePath.exists()) {
            filePath.mkdir();
        }

        File targetFile = new File(filePath, genFileName);
        if (targetFile.exists()) {
            targetFile.delete();
        }

        template.process(valueMap, new OutputStreamWriter(new FileOutputStream(targetFile, false), ENCODING));

        return true;
    }

笔记:

System.getProperty获取当前工程路径。

Path.of用于处理路径的连接。

你可能感兴趣的:(#,Freemaker代码生成,windows)