实体类生成get和set以及toString hashcode和equals方法


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;

/**
 * @author hongtao.hao
 * @date 2019/7/1
 */
public class JavaTest {
     
    public static void main(String[] args) throws IOException {
     
        //实体类生成get和set以及toString hashcode和equals方法
        getMethod(ArchResponse.class);
    }

    private static final String pathTest;

    static {
     
        String test = new JavaTest().test();
        test = test.substring(0, test.indexOf("target"));
        pathTest = test + "\\src\\test\\java\\";
    }

    public String test() {
     
        File f = new File(this.getClass().getResource("/").getPath());
        return f.getPath();
    }

    //读取某个类下所有变量的名称
    public static String getMethod(Class clz) throws IOException {
     

        StringBuffer sb = null;
        //单个失败不影响其他
        try {
     
            sb = new StringBuffer();

            String name = clz.getName();

            name = name.substring(0, name.indexOf(clz.getSimpleName()) - 1);
            //获取项目路径

            //拼接测试类所在的目录
            String path = pathTest + name.replace(".", "\\");


            File catalog = new File(path);
            if (!catalog.exists()) {
     
                catalog.mkdirs();
            }

            path = path + "\\" + clz.getSimpleName() + "Test.java";
            File txt = new File(path);
            txt.createNewFile();


            //package
            sb.append("package ").append(name).append(";\n\n");
            //import
            sb.append("import org.junit.Test;\n" +
                    "import java.util.Date;\n");
            sb.append("import " + clz.getName() + ";\n\n");

            String simpleName = clz.getSimpleName();

            //类
            sb.append("public class " + simpleName + "Test {\n");


            String fileName = "";

            String object = simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1);

            //常用的一些基本数据类型
            sb.append(simpleName + " " + object + "= new " + simpleName + "();\n");
            sb.append("String s = \"s\";\n");
            sb.append("Integer i = 1;\n");
            sb.append("Long l = 1L;\n");
            sb.append("Date date = new  Date();\n");
            sb.append("\n\n");


            //get和set
            // 获取f对象对应类中的所有属性域
            Field[] fields = clz.getDeclaredFields();
            for (int i = 0, len = fields.length; i < len; i++) {
     
                String varName = fields[i].getName();
                varName = varName.substring(0, 1).toUpperCase() + varName.substring(1);


                sb.append("@Test\n" +
                        "    public void " + "set" + varName + "(){\n");

                // 对于每个属性,获取属性名
                Class<?> type = fields[i].getType();

                if ("String".equals(type.getSimpleName())) {
     
                    sb.append(object)
                            .append(".")
                            .append("set")
                            .append(varName)
                            .append("(")
                            .append("s")
                            .append(");");
                } else if ("Integer".equals(type.getSimpleName()) || "int".equals(type.getSimpleName())) {
     
                    sb.append(object)
                            .append(".")
                            .append("set")
                            .append(varName)
                            .append("(")
                            .append("i")
                            .append(");");

                } else if ("Date".equals(type.getSimpleName())) {
     
                    sb.append(object)
                            .append(".")
                            .append("set")
                            .append(varName)
                            .append("(")
                            .append("date")
                            .append(");");

                } else if ("Long".equals(type.getSimpleName()) || "long".equals(type.getSimpleName())) {
     
                    sb.append(object)
                            .append(".")
                            .append("set")
                            .append(varName)
                            .append("(")
                            .append("l")
                            .append(");");

                } else {
     
                    sb.append(object)
                            .append(".")
                            .append("set")
                            .append(varName)
                            .append("(")
                            .append(");");

                }

                sb.append("\n}\n\n");

                sb.append("@Test\n" +
                        "    public void " + "get" + varName + "(){\n");
                sb.append(object)
                        .append(".")
                        .append("get")
                        .append(varName)
                        .append("(")
                        .append(");");
                sb.append("\n}\n\n");

            }

            //toString
            sb.append("@Test\n" +
                    "    public void " + "toStringTest" + "(){\n");
            sb.append(object)
                    .append(".")
                    .append("toString")
                    .append("(")
                    .append(");");
            sb.append("\n}\n\n");

            //hashCode
            sb.append("@Test\n" +
                    "    public void " + "hashCodeTest" + "(){\n");
            sb.append(object)
                    .append(".")
                    .append("hashCode")
                    .append("(")
                    .append(");");
            sb.append("\n}\n\n");

            //equals
            sb.append("@Test\n" +
                    "    public void " + "equalsTest" + "(){\n");
            sb.append(object)
                    .append(".")
                    .append("equals")
                    .append("(1")
                    .append(");");
            sb.append("\n}\n\n");

            sb.append("}");


            //写入文件
            FileOutputStream fileOutputStream = new FileOutputStream(txt);
            fileOutputStream.write(sb.toString().getBytes());
            fileOutputStream.close();
        } catch (IOException e) {
     
            e.printStackTrace();
        } catch (SecurityException e) {
     
            e.printStackTrace();
        }
        return sb.toString();
    }

}

你可能感兴趣的:(java)