Skip to content

Commit

Permalink
[Feature] add for new
Browse files Browse the repository at this point in the history
  • Loading branch information
houbb committed Mar 15, 2019
1 parent 9e37bb2 commit ed63d92
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 25 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

- 支持用户自定生成文档的类过滤器

## 新特性

- 添加字段类型别名,支持用户自定义

# 变更日志

> [变更日志](doc/CHANGELOG.md)
Expand All @@ -40,7 +44,7 @@ maven 3.x+
<plugin>
<groupId>com.github.houbb</groupId>
<artifactId>idoc-core</artifactId>
<version>0.0.2</version>
<version>0.1.0</version>
</plugin>
</plugins>
</build>
Expand Down Expand Up @@ -118,4 +122,6 @@ mvn com.github.houbb:idoc-core:0.0.2:idoc

[02-插件的参数配置](/doc/blog/文档生成-02-插件的参数配置.md)

[03-自定义生成文件过滤器](/doc/blog/文档生成-03-自定义生成文件过滤器.md)
[03-自定义生成文件过滤器](/doc/blog/文档生成-03-自定义生成文件过滤器.md)

[04-字段类型别名支持](/doc/blog/文档生成-04-字段类型别名支持.md)
8 changes: 7 additions & 1 deletion doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@
|:---|:---|:---|:---|:--|
| 1 | A | 添加包信息 | 2019-02-21 23:51:14 | |
| 2 | A | 初步添加方法的 @see @throw 的相关信息 | 2019-02-21 23:51:14 | |
| 3 | O | 文档的优化调整 | 2019-02-21 23:51:14 | |
| 3 | O | 文档的优化调整 | 2019-02-21 23:51:14 | |

# release_0.1.0

| 序号 | 变更类型 | 说明 | 时间 | 备注 |
|:---|:---|:---|:---|:--|
| 1 | A | 添加类型别名支持,支持自定义 | 2019-3-15 15:57:58 | |
10 changes: 10 additions & 0 deletions doc/blog/文档生成-00-项目概览.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,13 @@ java-doc 生成的文档又过于复杂。

一些使用案例可以参考这个模块,同时也为项目的质量提供一定的保证。

# 项目的格局

## 语言

后期希望支持 i18n,而不是拘泥于中文项目。

## 针对性

针对用户的自定义类型,而不是和 java-doc 做重复的事情。

123 changes: 123 additions & 0 deletions doc/blog/文档生成-04-字段类型别名支持.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# 字段类型别名支持

可以参考当前项目的 `idoc-test` 模块。

## 为什么需要

有时候页面显示类型,希望更加友好。

所以系统内置了一些别名显示,也同时支持自定义别名。

# 类型字段的别名

## 系统内置

系统当前版本提供了常见的别名。

详情见 `com.github.houbb.idoc.core.util.JavaTypeAliasUtil`

| 类型 | 别称 |
|:---|:---|
| java.lang.Float | 浮点型 |
| java.lang.Double | 浮点型 |
| java.util.Date | 日期 |
| java.time.LocalDateTime | 日期时间 |
| java.util.Currency | 货币 |
| float | 浮点型 |
| java.lang.Integer | 整型 |
| long | 长整型 |
| java.math.BigDecimal | 数字 |
| java.lang.Character | 字符 |
| java.lang.Long | 长整型 |
| java.lang.Short | 短整型 |
| java.util.Map | 映射 |
| java.time.LocalTime | 时间 |
| java.lang.Boolean | 布尔值 |
| java.math.BigInteger | 数字 |
| java.lang.String | 字符串 |
| java.lang.Byte | 字节 |
| double | 浮点型 |
| byte | 字节 |
| java.util.Collection | 集合 |
| int | 整型 |
| java.util.List | 列表 |
| boolean | 布尔值 |
| java.time.LocalDate | 日期 |
| char | 字符 |
| short | 短整型 |

## 自定义的方式

可以通过 typeAlias 指定自定义的字段别称。

```xml
<configuration>
<generateFilters>
<generateFilter>com.github.houbb.idoc.test.filter.MyGenerateFilter</generateFilter>
</generateFilters>
<isAllInOne>true</isAllInOne>
<typeAliases>
<typeAlias>
<key>java.lang.String</key>
<value>String自定义说明</value>
</typeAlias>
</typeAliases>
</configuration>
```

## 优先级

用户自定义的字段别名优先级高于系统默认。

后面定义的别名会直接覆盖前面的别名。

# 测试代码演示

## 对象定义

```java
/**
* 别名测试
* @author binbin.hou
* @since 0.0.1
*/
public class TypeAliasSimpleBean {

/**
* 名称
*/
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
```

## 测试日志

运行测试日志如下:

```
{"comment":"别名测试","docAnnotationList":[],"docFieldList":[{"comment":"名称","name":"name","type":"java.lang.String","typeAlias":"String自定义说明"}],"docMethodList":[{"docMethodParameterList":[],"docMethodReturn":{"fullName":"java.lang.String","name":"String","packageName":"java.lang"},"docTagList":[],"exceptionList":[],"modifiers":["public"],"name":"getName","seeList":[],"signature":"getName()"},{"docMethodParameterList":[{"docAnnotationList":[],"name":"name","type":"java.lang.String","typeAlias":"String自定义说明"}],"docMethodReturn":{},"docTagList":[],"exceptionList":[],"modifiers":["public"],"name":"setName","seeList":[],"signature":"setName(name)"}],"docTagList":[{"lineNum":5,"name":"author","parameters":["binbin.hou"],"value":"binbin.hou"},{"lineNum":6,"name":"since","parameters":["0.0.1"],"value":"0.0.1"}],"fullName":"com.github.houbb.idoc.test.model.TypeAliasSimpleBean","modifiers":["public"],"name":"TypeAliasSimpleBean","packageName":"com.github.houbb.idoc.test.model"}
```

其中 typeAlias 就是字段类型的别名,我们可以用来更加友好的显示字段信息。

# 其他的思考

## 自定义方式的便利性

自定义的方式采用基于 xml 的方式是比较方便。

但是数量比较多的时候就没有那么方便,本来考虑添加对应的配置属性接口,权衡下还是使用了 xml 配置的方式。

## i18n

到目前为止,本项目的设计都过于拘泥于中文。

后期如果考虑到扩展的话,将考虑添加 i18n 支持。
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public final class DocMethodParameter extends BaseDoc {
*/
private String type;

/**
* 参数类型别名
* @since 0.1.0
*/
private String typeAlias;

/**
* 当前入参下面的字段信息
*/
Expand All @@ -35,4 +41,11 @@ public void setDocFieldList(List<DocField> docFieldList) {
this.docFieldList = docFieldList;
}

public String getTypeAlias() {
return typeAlias;
}

public void setTypeAlias(String typeAlias) {
this.typeAlias = typeAlias;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public SimplifyDocField handle(DocMethodParameter docMethodParameter) {
simplifyDocField.setComment(docMethodParameter.getComment());
simplifyDocField.setRemark(docMethodParameter.getRemark());
simplifyDocField.setType(docMethodParameter.getType());
simplifyDocField.setTypeAlias(docMethodParameter.getTypeAlias());

// 入参为特殊参数列表
List<DocField> docFieldList = docMethodParameter.getDocFieldList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected DocClass doHandle(JavaClass javaClass) {
docClass.setFullName(javaClass.getFullyQualifiedName());

// java 默认的字段
// TODO: 这里直接 return 应该是为了避免死循环和额外处理
if (JavaClassUtil.isPrimitiveOrJdk(javaClass.asType())) {
return docClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaField;
import com.thoughtworks.qdox.model.Type;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

Expand All @@ -38,7 +39,7 @@ protected DocField doHandle(JavaField javaField) {
docField.setName(javaField.getName());
final String type = javaField.getType().getFullyQualifiedName();
docField.setType(type);
final String alias = getTypeAlias(type);
final String alias = StringUtils.defaultIfEmpty(docConfig.getTypeAliases().get(type), type);
docField.setTypeAlias(alias);
docField.setComment(javaField.getComment());
// 使用 doclet,缺点:严格的 java-doc 会报错
Expand Down Expand Up @@ -68,17 +69,4 @@ protected DocField doHandle(JavaField javaField) {
return docField;
}

/**
* 获取类型别名
* @param type 类型
* @return 别称
*/
private String getTypeAlias(final String type) {
final String alias = docConfig.getTypeAliases().get(type);
if(StringUtil.isNotEmpty(alias)) {
return alias;
}
return type;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.thoughtworks.qdox.model.JavaField;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaParameter;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

Expand Down Expand Up @@ -50,7 +51,10 @@ public DocMethodParameter handle(JavaParameter javaParameter) {
docMethodParameter.setName(paramName);
docMethodParameter.setDocAnnotationList(MetadataDocUtil
.buildDocAnnotationList(javaParameter.getAnnotations()));
docMethodParameter.setType(javaParameter.getType().getFullyQualifiedName());
final String type = javaParameter.getType().getFullyQualifiedName();
final String typeAlias = StringUtils.defaultIfEmpty(docConfig.getTypeAliases().get(type), type);
docMethodParameter.setType(type);
docMethodParameter.setTypeAlias(typeAlias);
// 基础类型和非基础类型
// 初期版本可以做的比较简单,固定写死常见字段类型。
// 8大基本类型+Number 类型。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private JavaTypeAliasUtil(){}

TYPE_ALIAS_MAP.put("java.util.Collection", "集合");
TYPE_ALIAS_MAP.put("java.util.List", "列表");
TYPE_ALIAS_MAP.put("java.util.Map", "映射");
}

/**
Expand Down
10 changes: 10 additions & 0 deletions idoc-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<artifactId>idoc-core</artifactId>
<version>${project.version}</version>
<configuration>
<generateFilters>
<generateFilter>com.github.houbb.idoc.test.filter.MyGenerateAliasFilter</generateFilter>
</generateFilters>
<isAllInOne>true</isAllInOne>
<typeAliases>
<typeAlias>
Expand All @@ -42,6 +45,13 @@
</typeAlias>
</typeAliases>
</configuration>
<dependencies>
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>idoc-test</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.houbb.idoc.test.filter;

import com.github.houbb.idoc.api.core.filter.IDocGenerateFilter;
import com.github.houbb.idoc.api.model.metadata.DocClass;

/**
* 自定义生成过滤器
* @author binbin.hou
* @since 0.1.0
*/
public class MyGenerateAliasFilter implements IDocGenerateFilter {

@Override
public boolean include(DocClass docClass) {
if("TypeAliasSimpleBean".equalsIgnoreCase(docClass.getName())) {
return true;
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import java.util.Map;

/**
* 用户信息
* 类型别名测试对象
* @author binbin.hou
* @since 0.0.1
* @since 0.1.0
*/
public class TypeAliasBean {

Expand Down Expand Up @@ -46,7 +46,10 @@ public class TypeAliasBean {

private List<String> stringList;

private String[] strings;
/**
* TODO: 数组的类型为什么是 String
*/
private String[] stringArray;

private Collection<String> stringCollection;

Expand Down Expand Up @@ -188,12 +191,12 @@ public void setStringList(List<String> stringList) {
this.stringList = stringList;
}

public String[] getStrings() {
return strings;
public String[] getStringArray() {
return stringArray;
}

public void setStrings(String[] strings) {
this.strings = strings;
public void setStringArray(String[] stringArray) {
this.stringArray = stringArray;
}

public Collection<String> getStringCollection() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.houbb.idoc.test.model;

/**
* 别名测试
* @author binbin.hou
* @since 0.0.1
*/
public class TypeAliasSimpleBean {

/**
* 名称
*/
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

0 comments on commit ed63d92

Please sign in to comment.