使用molicode处理Java源代码
背景描述
在日常工作中,我们常常需要直接分析Java源码,用来生成文档,或者转换为其它数据模型来使用。
场景1 获取Java源码中的属性列表
数据源:
package com.shareyi.user.center.domain.config;import com.shareyi.user.center.domain.BasicDomain;import java.io.Serializable;/** * 配置信息 Domain 类 * * @author MoliCode * @date 2019-04-14 */public class AcConfig extends BasicDomain implements Serializable { private static final long serialVersionUID = 5405437785373882510L; /** 类型 */ private Integer type; /** 项目key */ private String projectKey; /** 范围 */ private Integer scope; /** 配置key */ private String configKey; /** 配置值 */ private String configValue; /** 扩展1 */ private String ext1; /** 扩展2 */ private String ext2; /** 扩展3 */ private String ext3; /** 创建人 */ private String creator; public void setType(Integer type) { this.type = type; } public Integer getType() { return this.type; } public void setProjectKey(String projectKey) { this.projectKey = projectKey; } public String getProjectKey() { return this.projectKey; } public void setScope(Integer scope) { this.scope = scope; } public Integer getScope() { return this.scope; } public void setConfigKey(String configKey) { this.configKey = configKey; } public String getConfigKey() { return this.configKey; } public void setConfigValue(String configValue) { this.configValue = configValue; } public String getConfigValue() { return this.configValue; } public void setExt1(String ext1) { this.ext1 = ext1; } public String getExt1() { return this.ext1; } public void setExt2(String ext2) { this.ext2 = ext2; } public String getExt2() { return this.ext2; } public void setExt3(String ext3) { this.ext3 = ext3; } public String getExt3() { return this.ext3; } public void setCreator(String creator) { this.creator = creator; } public String getCreator() { return this.creator; }}
模板:
<% //获取Java源码属性列表,并进行属性复制 data.fieldList.each { it -> def upperFistName = tableNameUtil.upperFirst(it.dataName) println """ dest.set${upperFistName}(src.get${upperFistName}());""" }%>
输出内容:
2019-04-14 10:47:49,073 [http-nio-8098-exec-7] INFO (frontConsole:-1) - [Java源码复制属性]模板执行成功,输出到前台,应输出路径:/study02_output/copyBean.txt==============模板输出开始 ================= dest.setType(src.getType()); dest.setProjectKey(src.getProjectKey()); dest.setScope(src.getScope()); dest.setConfigKey(src.getConfigKey()); dest.setConfigValue(src.getConfigValue()); dest.setExt1(src.getExt1()); dest.setExt2(src.getExt2()); dest.setExt3(src.getExt3()); dest.setCreator(src.getCreator());==============模板输出结束 =================
操作步骤:
场景2 获取Java源码中的方法列表
数据源:
package com.shareyi.user.center.service;import com.shareyi.user.center.common.web.CommonResult;import com.shareyi.user.center.common.web.PageQuery;import com.shareyi.user.center.domain.BasicDomain;import java.util.List;/** * 基础的服务类 service * * @author david * @date 2018/8/21 */public interface BaseService{ /** * 添加操作 * * @param t * @return */ CommonResult add(T t); /** * 修改操作 * * @param t * @return */ CommonResult update(T t); /** * 根据主键删除 */ CommonResult deleteByPk(Long primaryKey); /** * 通过主键ID查询数据 * * @param primaryKey * @return */ CommonResult getByPk(Long primaryKey); /** * 分页查询数据 * * @param pageQuery * @return */ CommonResult
> queryByPage(PageQuery pageQuery);}
模板: study02/javaSourceMethodList.gsp
<% / * * 闭包,获取方法的注释信息 */ def getMethodComment = { javaDocComment -> def comment = ""; if (javaDocComment.isPresent()) { comment = javaDocComment.get().getContent() } def commentLines = comment.split("\n"); for (def commentLine in commentLines) { def cLine = commentLine.trim().replaceAll("\\*", ""); if (cLine.length() > 1) { return cLine; } } return comment; } //遍历方法列表,并输出方法名称+注释 data.methodDeclarationList.each { method -> def javaDocComment = method.getJavadocComment() def comment = getMethodComment(javaDocComment) println """${method.name} (${comment})""" }%>
输出内容:
2019-04-14 10:42:51,616 [http-nio-8098-exec-6] INFO (frontConsole:-1) - [Java源码获取方法列表]模板执行成功,输出到前台,应输出路径:/study02_output/methodList.txt==============模板输出开始 =================add ( 添加操作)update ( 修改操作)deleteByPk ( 根据主键删除)getByPk ( 通过主键ID查询数据)queryByPage ( 分页查询数据)==============模板输出结束 =================
操作步骤:
场景3 通过Java源码生成tableModel模型xml
数据源:
package com.shareyi.user.center.domain.config;import com.shareyi.user.center.domain.BasicDomain;import java.io.Serializable;/** * 配置信息 Domain 类 * * @author MoliCode * @date 2019-04-14 */public class AcConfig extends BasicDomain implements Serializable { private static final long serialVersionUID = 5405437785373882510L; /** 类型 */ private Integer type; /** 项目key */ private String projectKey; /** 范围 */ private Integer scope; /** 配置key */ private String configKey; /** 配置值 */ private String configValue; /** 扩展1 */ private String ext1; /** 扩展2 */ private String ext2; /** 扩展3 */ private String ext3; /** 创建人 */ private String creator; public void setType(Integer type) { this.type = type; } public Integer getType() { return this.type; } public void setProjectKey(String projectKey) { this.projectKey = projectKey; } public String getProjectKey() { return this.projectKey; } public void setScope(Integer scope) { this.scope = scope; } public Integer getScope() { return this.scope; } public void setConfigKey(String configKey) { this.configKey = configKey; } public String getConfigKey() { return this.configKey; } public void setConfigValue(String configValue) { this.configValue = configValue; } public String getConfigValue() { return this.configValue; } public void setExt1(String ext1) { this.ext1 = ext1; } public String getExt1() { return this.ext1; } public void setExt2(String ext2) { this.ext2 = ext2; } public String getExt2() { return this.ext2; } public void setExt3(String ext3) { this.ext3 = ext3; } public String getExt3() { return this.ext3; } public void setCreator(String creator) { this.creator = creator; } public String getCreator() { return this.creator; }}
模板:
<% /** * 本模板用于将Java源码(Java bean),反向转换为tableModel, 用于生成业务代码。 * 需要特别注意的是,这个方式生成的tableModel可能会有属性遗漏,或者字段类型映射错误。 * 需要您手动进行部分调整,特别说明一下。 */%><% def columnNameList = [] data.fieldList.each { it -> def columnName = tableNameUtil.convertToDbNames(it.dataName).toLowerCase(); columnNameList.add(columnName) %> <% } def columnNames = StringUtils.join(columnNameList, ","); %> id created modified ${columnNames} ${columnNames} ${columnNames} ${columnNames} ${columnNames} ${columnNames}
输出内容:
id created modified type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator
操作步骤:
以上示例模板工程可以从git仓库中下载使用:
参考study02目录下的模板文件。