java生成convert类

原创 2017年01月04日 00:45:08
public static void main(String[] args) {
        FileOutputStream fop = null;
        File file;

        try {

            Class clazz = Class.forName("demo.model.User");//根据类名获得其对应的Class对象 写上你想要的类名就是了 注意是全名 如果有包的话要加上 比如java.Lang.String
            Class clazzB = Class.forName("demo.model.UserRo");
            file = new File("D:/liangPlus/Hello-World/demo/src/main/java/demo/convert/" + firstUpper(clazz.getSimpleName()) + "Convert.java");
            fop = new FileOutputStream(file);

            // if file doesnt exists, then create it
            if (file.exists()) {
                file.delete();
                file.createNewFile();
            }else {
                file.createNewFile();
            }
            String clazzSimpleName = clazz.getSimpleName();
            String clazzBSimpleName = clazzB.getSimpleName();
            String clazzBName = firstLower(clazzB.getSimpleName());
            String clazzName = firstLower(clazz.getSimpleName());


            Field[] fields = clazz.getDeclaredFields();//根据Class对象获得属性 私有的也可以获得
            Set<String> importSet = new HashSet<String>();

            StringBuffer content = new StringBuffer("");

            content.append("package demo.convert;\n");

            importSet.add(clazz.getName());
            importSet.add(clazzB.getName());
            importSet.add("java.util.List");
            importSet.add("java.util.ArrayList");
            for(Field f : fields) {
                importSet.add(f.getType().getName());
            }

            for (String s : importSet) {
                content.append("import " + s + ";\n");
            }

            content.append("/**\ncreate by liangmeiwei's tool\n*/\n");

            //classToClassB
            content.append("public class " + clazzSimpleName + "Convert {" + "\n");
            content.append("public static " + clazzSimpleName + " " + clazzBName + "To" + clazzSimpleName + "(" + clazzBSimpleName + " " + clazzBName + ")" +"{\n");
            content.append("if("+ clazzBName +" == null){\n");
            content.append("return null;\n");
            content.append("}\n");
            content.append(clazzSimpleName + " " + clazzName  + " = new " + clazzSimpleName + "();\n");
            for(Field f : fields) {
                content.append(clazzName + ".set" + firstUpper(f.getName()) + "(" + clazzBName+".get" + firstUpper(f.getName())  +"());\n");
            }
            content.append("return " + clazzName + ";\n");
            content.append("}\n");

            //classBToClass
            content.append("public static " + clazzBSimpleName + " " + clazzName + "To" + clazzBSimpleName + "(" + clazzSimpleName + " " + clazzName + ")" +"{\n");
            content.append("if("+ clazzName +" == null){\n");
            content.append("return null;\n");
            content.append("}\n");
            content.append(clazzBSimpleName + " " + clazzBName  + " = new " + clazzBSimpleName + "();\n");
            for(Field f : fields) {
                content.append(clazzBName + ".set" + firstUpper(f.getName()) + "(" + clazzName+".get" + firstUpper(f.getName())  +"());\n");
            }
            content.append("return " + clazzBName + ";\n");
            content.append("}\n");

            //classListToClassBList
            content.append("public static List<" + clazzBSimpleName + ">" + clazzName + "ListTo" + clazzBSimpleName + "List(" + "List<" + clazzSimpleName +"> " + clazzName  +"List){\n");
            content.append("if(" + clazzName + "List == null" +"){\n");
            content.append("return null;\n");
            content.append("}\n");
            content.append("List<" + clazzBSimpleName + ">" + clazzBName + "List = new ArrayList();\n");
            content.append("for(" + clazzSimpleName + " " + clazzName + " : " + clazzName + "List" + "){\n");
            content.append(clazzBName + "List.add(" + clazzName + "To" + clazzBSimpleName + "(" + clazzName +")" + ");\n");
            content.append("}\n");
            content.append("return " + clazzBName + "List;\n");
            content.append("}\n");


            //classBListToClassList
            content.append("public static List<" + clazzSimpleName + ">" + clazzBName + "ListTo" + clazzSimpleName + "List(" + "List<" + clazzBSimpleName +"> " + clazzBName  +"List){\n");
            content.append("if(" + clazzBName + "List == null" +"){\n");
            content.append("return null;\n");
            content.append("}\n");
            content.append("List<" + clazzSimpleName + ">" + clazzName + "List = new ArrayList();\n");
            content.append("for(" + clazzBSimpleName + " " + clazzBName + " : " + clazzBName + "List" + "){\n");
            content.append(clazzName + "List.add(" + clazzBName + "To" + clazzSimpleName + "(" + clazzBName +")" + ");\n");
            content.append("}\n");
            content.append("return " + clazzName + "List;\n");

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

            // get the content in bytes
            byte[] contentInBytes = content.toString().getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();

            System.out.println("Done");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fop != null) {
                    fop.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String firstUpper(String str){
        if(StringUtils.isEmpty(str)){
            return str;
        }
        return str.substring(0,1).toUpperCase() + str.substring(1);
    }

    public static String firstLower(String str){
        if(StringUtils.isEmpty(str)){
            return str;
        }
        return str.substring(0,1).toLowerCase() + str.substring(1);
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

SpringMVC 之类型转换 Converter

1.1      目录 1.1       目录 1.2       前言 1.3       Converter 接口 1.4       ConversionService 接口 1.5...

Java类型转换

string和int之间的转换? 字符串转换成数据 Java代码   String MyNumber ="1234";   int MyInt = Integer.par...
  • oyzl68
  • oyzl68
  • 2012年09月27日 00:46
  • 2785

SpringMVC之类型转换Converter

1.1     目录 1.1      目录 1.2      前言 1.3      Converter接口 1.4      ConversionService接口 1.5      C...

配置JAVAGDK环境变量

安装完JDK后配置环境变量  计算机→属性→高级系统设置→高级→环境变量 2:系统变量→新建 JAVA_HOME 变量 。 变量值填写jdk的安装目录(本人是 D:\Java\jdk1..0)...

java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12问题

完整报错信息如下:09-12 02:41:27.588: E/AndroidRuntime(445): FATAL EXCEPTION: main 09-12 02:41:27.588: E/Andr...

java OpenOffice把word转html(Convert word to html )

1. 下载安装OpenOffice,网址http://download.openoffice.org/index.html。   2. 下载第三方工具包JODConverter,http://ww...

java.lang.IllegalArgumentException: Can't convert argument: null

今天在开发的时候,碰到一个问题 ,错误如下 java.lang.IllegalArgumentException: Can't convert argument: null at org.apac...

java int and string convert

java int and string convert

【LeetCode-面试算法经典-Java实现】【108-Convert Sorted Array to Binary Search Tree(排序数组转变为平衡二叉树)】

【108-Convert Sorted Array to Binary Search Tree(排序数组转变为平衡二叉树)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java生成convert类
举报原因:
原因补充:

(最多只允许输入30个字)