java源码使用_使用molicode处理Java源代码

使用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());

==============模板输出结束 =================

操作步骤:

f18fb90c00405e95963a9d46ec6efb8d.png

场景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 ( 分页查询数据)

==============模板输出结束 =================

操作步骤:

3d95124c8c96981184395b2c11c0b802.png

场景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可能会有属性遗漏,或者字段类型映射错误。

* 需要您手动进行部分调整,特别说明一下。

*/

%><?xml version="1.0" encoding="UTF-8" ?>

dbTableName='${tableNameUtil.convertToDbNames(data.className)}'>

def columnNameList = []

data.fieldList.each { it ->

def columnName = tableNameUtil.convertToDbNames(it.dataName).toLowerCase();

columnNameList.add(columnName)

%>

columnType='${tableNameUtil.transToColumnType(it.fieldClass)}'

canBeNull='false' readonly='false' isPK='false' jspTag='TEXT' comment='${it.dataName}'/>

}

def columnNames = StringUtils.join(columnNameList, ",");

%>

id

created

modified

key='queryList'>${columnNames}

key='addList'>${columnNames}

key='updateList'>${columnNames}

key='viewList'>${columnNames}

${columnNames}

key='allColumn'>${columnNames}

输出内容:

dbTableName='AC_CONFIG'>

columnType='TINYINT'

canBeNull='false' readonly='false' isPK='false' jspTag='TEXT' comment='type'/>

columnType='VARCHAR'

canBeNull='false' readonly='false' isPK='false' jspTag='TEXT' comment='projectKey'/>

columnType='TINYINT'

canBeNull='false' readonly='false' isPK='false' jspTag='TEXT' comment='scope'/>

columnType='VARCHAR'

canBeNull='false' readonly='false' isPK='false' jspTag='TEXT' comment='configKey'/>

columnType='VARCHAR'

canBeNull='false' readonly='false' isPK='false' jspTag='TEXT' comment='configValue'/>

columnType='VARCHAR'

canBeNull='false' readonly='false' isPK='false' jspTag='TEXT' comment='ext1'/>

columnType='VARCHAR'

canBeNull='false' readonly='false' isPK='false' jspTag='TEXT' comment='ext2'/>

columnType='VARCHAR'

canBeNull='false' readonly='false' isPK='false' jspTag='TEXT' comment='ext3'/>

columnType='VARCHAR'

canBeNull='false' readonly='false' isPK='false' jspTag='TEXT' comment='creator'/>

id

created

modified

key='queryList'>type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator

key='addList'>type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator

key='updateList'>type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator

key='viewList'>type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator

type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator

key='allColumn'>type,project_key,scope,config_key,config_value,ext1,ext2,ext3,creator

操作步骤:

3269864226698f9cdd56655f033767ce.png

以上示例模板工程可以从git仓库中下载使用:

参考study02目录下的模板文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值