Skip to content

Commit

Permalink
修复空指针异常
Browse files Browse the repository at this point in the history
文档名称和标题支持从注释获取
  • Loading branch information
liuzhihang committed Jul 19, 2021
1 parent 3dc6e1d commit c562033
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.liuzhihang'
version '1.1.3'
version '1.1.4'

JavaVersion.VERSION_11

Expand Down
10 changes: 6 additions & 4 deletions parts/changeNotes.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@

<h4>English introduction</h4>
<ul>
<li>1.1.3
<li>1.1.4
<ol>
<li>Solve the bug that parameter recursion causes exception</li>
<li>Document name and title can be obtained from comments</li>
<li>Fix null pointer exception</li>
</ol>
</li>
<li><a href="https://github.com/liuzhihang/doc-view/blob/master/doc/ChangeNotes.md">More records</a></li>
</ul>
<h4>中文介绍</h4>
<ul>
<li>1.1.3
<li>1.1.4
<ol>
<li>解决参数递归导致异常的 bug</li>
<li>文档名称和标题支持从注释获取</li>
<li>修复空指针异常</li>
</ol>
</li>
<li><a href="https://github.com/liuzhihang/doc-view/blob/master/doc/ChangeNotes.md">更多记录</a></li>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/liuzhihang/doc/view/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Settings implements PersistentStateComponent<Settings> {
*/
private String titleTag = "DocView.Title";
private Boolean titleUseCommentTag = true;
private Boolean titleClassComment = true;
private Boolean titleUseFullClassName = true;
private Boolean titleUseSimpleClassName = true;

Expand All @@ -44,6 +45,7 @@ public class Settings implements PersistentStateComponent<Settings> {
private Boolean nameUseSwagger3 = true;
private Boolean nameUseSwagger = true;
private Boolean nameUseCommentTag = true;
private Boolean nameMethodComment = true;

/**
* 文档描述
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/liuzhihang/doc/view/dto/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@


import com.intellij.psi.PsiElement;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.LinkedList;
import java.util.List;
Expand All @@ -11,7 +12,8 @@
* @author liuzhihang
* @date 2020/2/27 16:39
*/
@Data
@Getter
@Setter
public class Body {

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/liuzhihang/doc/view/dto/DocView.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ public class DocView {
/**
* 请求参数
*/
private Body reqRootBody;
private Body reqRootBody = new Body();

/**
* 返回参数
*/
private Body respRootBody;
private Body respRootBody = new Body();


/**
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/com/liuzhihang/doc/view/ui/PreviewForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.MouseEvent;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
* @author liuzhihang
Expand Down Expand Up @@ -492,9 +493,23 @@ public void actionPerformed(@NotNull AnActionEvent e) {

private void buildDoc() {

docViewMap = docViewList.stream().collect(Collectors.toMap(DocView::getName, docView -> docView));
// 取注释之后,可能会重复名字,不能用 stream
docViewMap = new HashMap<>();
Vector<String> nameVector = new Vector<>();

for (DocView docView : docViewList) {

if (nameVector.contains(docView.getName())) {
String name = docView.getName() + "-" + LocalTime.now().getNano();
docViewMap.put(name, docView);
nameVector.add(name);
} else {
docViewMap.put(docView.getName(), docView);
nameVector.add(docView.getName());
}

}

Vector<String> nameVector = docViewList.stream().map(DocView::getName).collect(Collectors.toCollection(Vector::new));

catalogList.setListData(nameVector);

Expand Down
30 changes: 23 additions & 7 deletions src/main/java/com/liuzhihang/doc/view/ui/Settings.form
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</component>
</children>
</grid>
<grid id="99bd7" binding="namePanel" layout-manager="GridLayoutManager" row-count="1" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="99bd7" binding="namePanel" layout-manager="GridLayoutManager" row-count="1" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
Expand All @@ -50,7 +50,7 @@
</component>
<hspacer id="d0a9c">
<constraints>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="5" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="6a932" class="javax.swing.JCheckBox" binding="nameSwaggerCheckBox" default-binding="true">
Expand All @@ -73,14 +73,22 @@
</component>
<component id="47bb1" class="javax.swing.JCheckBox" default-binding="true">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="false"/>
<selected value="true"/>
<text resource-bundle="messages/DocViewBundle" key="settings.doc.name.method"/>
</properties>
</component>
<component id="dafe8" class="javax.swing.JCheckBox" binding="nameMethodCommentCheckBox" default-binding="true">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/DocViewBundle" key="settings.doc.name.method.comment"/>
</properties>
</component>
</children>
</grid>
<grid id="702ad" binding="docDescPanel" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
Expand Down Expand Up @@ -244,7 +252,7 @@
</component>
</children>
</grid>
<grid id="c2db5" binding="titlePanel" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="c2db5" binding="titlePanel" layout-manager="GridLayoutManager" row-count="1" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
Expand All @@ -263,25 +271,33 @@
</component>
<hspacer id="bc8ad">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="1cc88" class="javax.swing.JCheckBox" binding="titleFullClassNameCheckBox" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/DocViewBundle" key="settings.doc.title.full"/>
</properties>
</component>
<component id="68b8c" class="javax.swing.JCheckBox" binding="titleSimpleClassNameCheckBox" default-binding="true">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/DocViewBundle" key="settings.doc.title.simple"/>
</properties>
</component>
<component id="9a331" class="javax.swing.JCheckBox" binding="titleClassCommentCheckBox" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/DocViewBundle" key="settings.doc.title.class.comment"/>
</properties>
</component>
</children>
</grid>
<vspacer id="1ebad">
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/liuzhihang/doc/view/ui/SettingsForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public class SettingsForm {
private JCheckBox titleCommentTagCheckBox;
private JCheckBox titleFullClassNameCheckBox;
private JCheckBox titleSimpleClassNameCheckBox;
private JCheckBox titleClassCommentCheckBox;

private JPanel namePanel;
private JCheckBox nameSwagger3CheckBox;
private JCheckBox nameSwaggerCheckBox;
private JCheckBox nameCommentTagCheckBox;
private JCheckBox nameMethodCommentCheckBox;

private JPanel docDescPanel;
private JCheckBox descSwagger3CheckBox;
Expand All @@ -46,6 +48,7 @@ public class SettingsForm {
private JPanel requirePanel;
private JCheckBox requireCommentTagCheckBox;


public SettingsForm(@NotNull Project project) {

this.project = project;
Expand Down Expand Up @@ -91,9 +94,11 @@ public boolean isModified() {
return titleCommentTagCheckBox.isSelected() != settings.getTitleUseCommentTag()
|| titleFullClassNameCheckBox.isSelected() != settings.getTitleUseFullClassName()
|| titleSimpleClassNameCheckBox.isSelected() != settings.getTitleUseSimpleClassName()
|| titleClassCommentCheckBox.isSelected() != settings.getTitleClassComment()
|| nameSwagger3CheckBox.isSelected() != settings.getNameUseSwagger3()
|| nameSwaggerCheckBox.isSelected() != settings.getNameUseSwagger()
|| nameCommentTagCheckBox.isSelected() != settings.getNameUseCommentTag()
|| nameMethodCommentCheckBox.isSelected() != settings.getNameMethodComment()
|| descSwagger3CheckBox.isSelected() != settings.getDescUseSwagger3()
|| descSwaggerCheckBox.isSelected() != settings.getDescUseSwagger()
|| requireCommentTagCheckBox.isSelected() != settings.getRequiredUseCommentTag();
Expand All @@ -105,9 +110,11 @@ public void apply() {
settings.setTitleUseCommentTag(titleCommentTagCheckBox.isSelected());
settings.setTitleUseFullClassName(titleFullClassNameCheckBox.isSelected());
settings.setTitleUseSimpleClassName(titleSimpleClassNameCheckBox.isSelected());
settings.setTitleClassComment(titleClassCommentCheckBox.isSelected());
settings.setNameUseSwagger3(nameSwagger3CheckBox.isSelected());
settings.setNameUseSwagger(nameSwaggerCheckBox.isSelected());
settings.setNameUseCommentTag(nameCommentTagCheckBox.isSelected());
settings.setNameMethodComment(nameMethodCommentCheckBox.isSelected());
settings.setDescUseSwagger3(descSwagger3CheckBox.isSelected());
settings.setDescUseSwagger(descSwaggerCheckBox.isSelected());
settings.setRequiredUseCommentTag(requireCommentTagCheckBox.isSelected());
Expand All @@ -117,11 +124,13 @@ public void apply() {
public void reset() {
Settings settings = Settings.getInstance(project);
titleCommentTagCheckBox.setSelected(settings.getTitleUseCommentTag());
titleClassCommentCheckBox.setSelected(settings.getTitleClassComment());
titleFullClassNameCheckBox.setSelected(settings.getTitleUseFullClassName());
titleSimpleClassNameCheckBox.setSelected(settings.getTitleUseSimpleClassName());
nameSwagger3CheckBox.setSelected(settings.getNameUseSwagger3());
nameSwaggerCheckBox.setSelected(settings.getNameUseSwagger());
nameCommentTagCheckBox.setSelected(settings.getNameUseCommentTag());
nameMethodCommentCheckBox.setSelected(settings.getNameMethodComment());
descSwagger3CheckBox.setSelected(settings.getDescUseSwagger3());
descSwaggerCheckBox.setSelected(settings.getDescUseSwagger());
requireCommentTagCheckBox.setSelected(settings.getRequiredUseCommentTag());
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/liuzhihang/doc/view/utils/DocViewUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public static String getTitle(@NotNull PsiClass psiClass) {
}
}

if (settings.getTitleClassComment()) {
// 获取类注释

String comment = CustomPsiCommentUtils.getComment(psiClass.getDocComment());

if (StringUtils.isNotBlank(comment)) {
return comment;
}
}

if (settings.getTitleUseFullClassName()) {
// 获取全类名
String fullClassName = psiClass.getQualifiedName();
Expand Down Expand Up @@ -110,6 +120,17 @@ public static String getName(@NotNull PsiMethod psiMethod) {
return comment;
}
}

if (settings.getNameMethodComment()) {
// 获取类注释

String comment = CustomPsiCommentUtils.getComment(psiMethod.getDocComment());

if (StringUtils.isNotBlank(comment)) {
return comment;
}
}

return psiMethod.getName();
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages/DocViewBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ settings.doc.title=\u6587\u6863\u6807\u9898
settings.doc.title.tip=\u5F39\u51FA\u7A97\u53E3\u7684\u6807\u9898, \u4F1A\u6309\u7167\u987A\u5E8F\u4F9D\u6B21\u83B7\u53D6
settings.doc.title.comment.tag=@DocView.Title
settings.doc.title.comment.tag.tip=\u7C7B\u6CE8\u91CA\u4E2D\u7684\u6807\u7B7E
settings.doc.title.class.comment=\u7C7B\u6CE8\u91CA
settings.doc.title.full=\u5168\u7C7B\u540D
settings.doc.title.simple=\u7C7B\u540D
settings.doc.name=\u63A5\u53E3\u540D
Expand All @@ -71,6 +72,7 @@ settings.doc.name.swagger=@ApiOperation
settings.doc.name.swagger.tip=Swagger @ApiOperation \u6CE8\u89E3\u7684 value \u5C5E\u6027
settings.doc.name.comment.tag=@DocView.Name
settings.doc.name.comment.tag.tip=\u65B9\u6CD5\u6CE8\u91CA\u4E2D\u7684\u6807\u7B7E
settings.doc.name.method.comment=\u65B9\u6CD5\u6CE8\u91CA
settings.doc.name.method=\u65B9\u6CD5\u540D
settings.doc.desc=\u63A5\u53E3\u63CF\u8FF0
settings.doc.desc.tip=\u63A5\u53E3\u63CF\u8FF0: \u6309\u7167\u987A\u5E8F\u4F9D\u6B21\u83B7\u53D6
Expand Down

0 comments on commit c562033

Please sign in to comment.