最近在搞写前端,由于模型太多,定义typescript的结构太复杂,网上没找到对应的工具,自己做了一个转换工具,还挺好用。
只不过目前不能带上注释。

java代码

package com.xiaomi.nr.order.management.api.dto.orderdeatil;


import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import java.lang.reflect.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 神奇转换器,获得typescript interface结构
 */
@Slf4j
public class TypeScriptInterFactory {

    public static class DemoClass {
        private boolean oriBoolVar;
        private int oriIntVar;
        private long oriLongVar;

        private Boolean boolVar;
        private Integer intVar;
        private Long longVar;

        private String strVar;
        private Date dateVar;

        private String[] arrayStrVar;
        private String[][] array2StrVar;
        private String[][][] array3StrVar;

        private List<String> listStrVar;
        private List<Integer> listIntVar;
        private List<Long> listLongVar;

        private Map<String, String> mapStrVar;
        private Map<Integer, String> mapIntVar;
        private Map<Long, Boolean> mapLongVar;

        private List<List<String>> listListStrVar;

        private List<DemoClass2> listDemoClass;
        private List<Map<String, String>> listMapStringString;
    }
    public static class DemoClass2 {
        private boolean oriBoolVar;
        private int oriIntVar;
        private long oriLongVar;
    }

    @Test
    public void run(){
        System.out.println(factory(DemoClass.class));
    }

    public static String factory(Class clazz){
        //得到所有属性
        Field[] fields = clazz.getDeclaredFields();
        StringBuilder resp = new StringBuilder();
        resp.append("interface ").append(clazz.getName()).append("{\n");
        for (int i=0;i<fields.length;i++){
            //得到属性
            Field field = fields[i];
            //获取属性值
            resp.append("  ")
                .append(field.getName())
                .append(": ")
                .append(fieldFactory(field))
                .append("\n");
        }
        resp.append("}\n\n");

        return resp.toString();
    }

    public static String fieldFactory(Field field) {
        return AnnotatedTypeFactory(field.getAnnotatedType());
    }
    public static String AnnotatedTypeFactory(AnnotatedType type) {
        if (type instanceof AnnotatedArrayType) {
            return AnnotatedTypeFactory(((AnnotatedArrayType) type).getAnnotatedGenericComponentType()) + "[]";
        }
        return typeFactory(type.getType());
    }
    public static String typeFactory(Type type) {
        if (type instanceof ParameterizedType) {
            ParameterizedType pType = (ParameterizedType) type;
            if (pType.getRawType().equals(List.class)) {
                return typeFactory(pType.getActualTypeArguments()[0]) + "[]";
            }
        }
        if (type.equals(String.class)) {
            return "string";
        }
        if (type.equals(Integer.class) || type.equals(Long.class) || type.equals(int.class) || type.equals(long.class)) {
            return "number";
        }
        if (type.equals(BigDecimal.class) || type.equals(Byte.class) || type.equals(Double.class) || type.equals(Float.class) || type.equals(Short.class)) {
            return "number";
        }
        if (type.equals(Boolean.class) || type.equals(boolean.class)) {
            return "boolean";
        }
        if (type.equals(Map.class)) {
            return "object";
        }
        if (type.equals(Date.class)) {
            return "any";
        }
        return "object";
    }
}

输出内容

运行是这个效果

interface com.xiaomi.nr.order.management.api.dto.orderdeatil.TypeScriptInterFactory$DemoClass{
  oriBoolVar: boolean
  oriIntVar: number
  oriLongVar: number
  boolVar: boolean
  intVar: number
  longVar: number
  strVar: string
  dateVar: any
  arrayStrVar: string[]
  array2StrVar: string[][]
  array3StrVar: string[][][]
  listStrVar: string[]
  listIntVar: number[]
  listLongVar: number[]
  mapStrVar: object
  mapIntVar: object
  mapLongVar: object
  listListStrVar: string[][]
  listDemoClass: object[]
  listMapStringString: object[]
}



进程已结束,退出代码为 0
最后修改:2022 年 01 月 26 日
如果觉得我的文章对你有用,请随意赞赏