• 生成代码的代码 之 POJO生成器


    我们在写Java代码时候,有时候需要写一些POJO类,也就是只有一些属性和get, set方法的类。例如,在写REST 服务时候,利用Jersery + Jackson,可以把输入的JSON字符串自动转换成一个POJO类,也可以自动把POJO类转换成JSON字符串而输出。在这样的情况下,我们往往需要写很多的POJO类,里面包含多个属性和对应的get, set方法,写起来比较麻烦。那么,我们有办法能很快的写出这些POJO类吗。
    实际上,这些POJO类的实质就是一个类名和几个属性的类型和名称。既然如此,我们可以只提供这几个信息,而通过一个程序来把这些POJO类生成出来。所以,我们要做的是一个生成代码的代码,用来生成POJO对象。
    要做这件事情,可以用各种编程语言来实现。笔者个人偏好JavaScript,而且JavaScript里面的object model比较适合做这件事,因此这里笔者通过Node.js来实现。代码如下,回头会把代码开源到GitHub.
    用法为, 把POJO的定义写到schema.js文件中,然后运行node createpojo.js

    // createpojo.js
    var fs = require('fs');
    var schema = require("./schema")
    
    var defaultindent = 2;
    
    var getIndent = function(i) {
      return getIndentCore(i, 0);
    };
    var getIndentCore = function(len, i) {
      if (i >= len) {
        return "";
      }
      return getIndentCore(len, i+1) + " ";
    };
    
    var createField = function(field, indentsize) {
      var i = indentsize == undefined ? 2 : indentsize;
      return getIndent(i) + "private " + field.type + " " + field.name + ";";
    };
    
    var createFields = function(fields, indentsize) {
      var fieldsstring = fields.map(function(f) {
        return createField(f, indentsize);
      });
      return fieldsstring.join("
    ");
    };
    
    var createEmptyConstructor = function(clz, indentsize) {
      return getIndent(indentsize) + "public " + clz + "() {
    
    " + getIndent(indentsize) + "}";
    };
    var createVariable = function(f) {
      return f.type + " " + f.name;
    };
    var createArglist = function(fields) {
      var args = fields.map(createVariable);
      return args.join(", ");
    };
    var createAssign = function(field, indentsize) {
      return getIndent(indentsize) + "this." + field.name + " = " + field.name + ";";
    };
    var createAssigns = function(fields, indentsize) {
      var assigns = fields.map(function(f) {
        return createAssign(f, indentsize);
      });
      return assigns.join("
    ");
    };
    var createConstructor = function(clz, fields, indentsize) {
      return getIndent(indentsize) + "public " + clz + "(" + createArglist(fields) + ") {
    " + createAssigns(fields, indentsize + defaultindent) + "
    " + getIndent(indentsize) + "}";
    };
    var createConstructors = function(clz, fields, indentsize) {
      return createEmptyConstructor(clz, indentsize) + "
    
    " + createConstructor(clz, fields, indentsize);
    };
    var getFuncName = function(name) {
      return name.substring(0,1).toUpperCase() + name.substring(1);
    };
    var createSetter = function(field, indentsize) {
      var line1 = getIndent(indentsize) + "public void set" + getFuncName(field.name) + "(" + createVariable(field) + ") {";
      var line2 = createAssign(field, indentsize + defaultindent);
      var line3 = getIndent(indentsize) + "}";
      var lines = [line1, line2, line3];
      return lines.join("
    ");
    };
    var createGetter = function(field, indentsize) {
      var line1 = getIndent(indentsize) + "public " + field.type + " get" + getFuncName(field.name) + "() {";
      var line2 = getIndent(indentsize + defaultindent) + "return this." + field.name + ";";
      var line3 = getIndent(indentsize) + "}";
      var lines = [line1, line2, line3];
      return lines.join("
    ");
    };
    var createAccessor = function(field, indentsize) {
      var access = [createSetter(field, indentsize), createGetter(field, indentsize)];
      return access.join("
    ");
    };
    var createAccessors = function(fields, indentsize) {
      var accesses = fields.map(function(f) {return createAccessor(f, indentsize);});
      return accesses.join("
    
    ");
    };
    var createClass = function(pkg, clz, fields) {
      var line1 = "package " + pkg + ";";
      var line2 = "public class " + clz + " {";
      var line3 = createFields(fields, defaultindent);
      var line4 = createConstructors(clz, fields, defaultindent);
      var line5 = createAccessors(fields, defaultindent);
      var line6 = "}";
      var lines = [line1, line2, line3, line4, line5, line6];
      return lines.join("
    
    ");
    };
    
    var classcontents = schema.map(function(s) {
      return createClass(s["package"], s["name"], s["fields"]);
    });
    
     
    for (var i = 0; i < schema.length; i++) {
      fs.writeFile(schema[i].name + ".java", classcontents[i], function(e){
        if (e) {
          console.log("error: " + e);
        }
      });
    }
    
    
    // schema.js
    var schemas = [
      {
        "name": "TestClass",
        "package": "example.com",
        "fields": [
          {"name": "name", "type": "String"},
          {"name": "value", "type": "String"}
        ]
      }
    ]
    
    module.exports = schemas;
    

    代码只是用来做工具用,所以没有写的很完善。如果读者觉得代码有改进之处,欢迎在代码开源之后提交更新。

  • 相关阅读:
    简单canvas五子棋
    javascript高级程序第三版学习笔记【执行环境、作用域】
    Error对象
    表单元素input、按钮、文字完美垂直居中对齐方法
    Firebug控制台详解
    JavaScript,JScript,ECMAScript及对应浏览器的版本
    valueOf和toString
    javascript高级程序第三版学习笔记【基本类型和引用类型】
    图的实现(邻接链表C#)
    基本排序算法(C)
  • 原文地址:https://www.cnblogs.com/l00l/p/4575201.html
Copyright © 2020-2023  润新知