Skip to content

Commit

Permalink
feature-version 2.0 supports nested objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Rookie authored and Rookie committed Jun 4, 2022
1 parent 6e2c875 commit 7e657ec
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 74 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,43 @@ BeanMappingKey 就是为了解决繁琐的手动对象转换而开发的,它
- 选中一个变量,使用快捷键 `Ctrl+M` 或者右键点击 BeanMappingKey,就能自动生成转换代码到粘贴板。
- 选中一个类,使用快捷键 `Ctrl+M` 或者右键点击 BeanMappingKey,就能自动生成转换代码到粘贴板。

以上生成逻辑中,若有 `Builder` 内部类则优先生成 `Builder` 代码,其次生成 `set` 型代码。
以上生成逻辑中,若有 `Builder` 内部类则优先生成 `Builder` 代码,其次生成 `Set` 型代码。

Tip: 在最新的 2.X 版本中,它连嵌套对象也可以生成了,比如一个 `User` 对象中包含了一个 `Address` 对象,
那么它将会将这两个对象都生成出来,并且自动赋值。

<br/>

## 代码生成指南

仔细阅读代码生成指南可以帮助你理解本插件生成代码的逻辑,倘若你在使用中遇到问题可以更好的判断是插件问题还是用法问题。


**Q1: 怎么判断一个类会生成 `Builder` 代码还是 `Set` 代码 ?**

A: 如果此类中有一个以 `Builder` 结尾的内部类则会生成 `Builder` 代码,倘若没有则会生成 `Set` 型代码,
`Set` 型代码则是通过寻找类中以 SetXXX 开头的方法来判断的。

如果生成的类是一个嵌套对象,请务必保证父对象和子对象都是 `Builder` 代码 或者 `Set` 型代码,因为判断时只会通过父对象来判断,
如果父对象是 `Builder` 代码而子对象是 `Set` 型代码则在生成子对象代码时出现一些错误代码,如果这种情况无法避免,可以将生成的错误代码删除,再点击子对象类单独生成。


**Q2: 通过方法参数和返回值进行代码生成时,是用什么逻辑做匹配 ?**

A: 首先我会拿到所有方法参数,依次将他们的变量名取出,接着对返回值的变量名做匹配,所以是通过变量名匹配。


**Q3: 在使用的过程中插件无反应,是什么问题 ?**

A: 这时你可以留意 `IDEA` 右下角气泡提示,如果它提示是绿色则代表你生成成功,如果是红色且可以看到 `BeanMapping` 开头的提示语则代表选中的对象不支持生成,
如果你看到了一个红色气泡但是并没有 `BeanMapping` 开头的提示语而是一个 `occurred error`,这代表我的插件出现了问题,你可以给我提 Issues。

Tip: 当代码还没有编译完成或者 `IDEA` 还没有准备完成,这时使用插件也有可能得到一个错误,因为插件依赖 `IDEA` 内部的上下文环境。


**Q4: 除了 Java,插件是否还支持其他语言 ?**

A: 截止到 2.0 版本,本插件只支持 Java。

<br/>

Expand Down
37 changes: 36 additions & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,42 @@ The relevant conversion code can be inserted automatically.
- Select a variable and use the shortcut key `Ctrl+M` or right-click on BeanMappingKey to automatically generate the conversion code to the pasteboard.
- Select a class and use the shortcut key `Ctrl+M` or right-click on BeanMappingKey to automatically generate the conversion code to the pasteboard.

In the above generation logic, if there is a `Builder` internal class, the `Builder` code will be generated first, and then the `set` type code will be generated.
In the above generation logic, if there is a `Builder` internal class, the `Builder` code will be generated first, and then the `Set` type code will be generated.

Tip: In the latest 2.X version, it can even generate nested objects, such as a `User` object contains an `Address` object,
Then it will generate both objects and assign them automatically.
<br/>

## Code Generation Guide

Carefully read the code generation guide to help you understand the logic of the code generated by this plugin. If you encounter problems in use, you can better judge whether it is a plugin problem or a usage problem.


**Q1: How to judge whether a class will generate `Builder` code or `Set` code?**

A: If there is an inner class ending with `Builder` in this class, `Builder` code will be generated, if not, `Set` type code will be generated,
`Set` type code is determined by looking for methods in the class that start with SetXXX.

If the generated class is a nested object, please make sure that the parent object and the child object are both `Builder` code or `Set` type code, because only the parent object will be judged when judging,
If the parent object is a `Builder` code and the child object is a `Set` type code, some error codes will appear when generating the child object code. If this situation cannot be avoided, you can delete the generated error code, and then click the child object class to separate generate.


**Q2: When generating code from method parameters and return values, what logic is used for matching?**

A: First, I will get all the method parameters, take out their variable names in turn, and then match the variable names of the return value, so the matching is done by variable names.


**Q3: The plugin does not respond during use, what is the problem?**

A: At this time, you can pay attention to the bubble prompt in the lower right corner of `IDEA`. If the prompt is green, it means that you have successfully generated. If it is red and you can see the prompt at the beginning of `BeanMapping`, it means that the selected object does not support generation.
If you see a red bubble but there is no prompt at the beginning of `BeanMapping` but an `occurred error`, it means that there is a problem with my plugin, you can send me Issues.

Tip: When the code is not compiled or `IDEA` is not ready, it is also possible to get an error when using the plugin, because the plugin depends on the context inside `IDEA`.


**Q4: Does the plugin support other languages besides Java?**

A: As of version 2.0, this plugin only supports Java.

<br/>

Expand Down
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tasks.withType(Javadoc) {
}

group 'org.rookie.plugins'
version '1.3'
version '2.0'

sourceCompatibility = 1.8

Expand All @@ -35,6 +35,11 @@ intellij {
}
patchPluginXml {
changeNotes = """
2.0 (2022-06-04) <br>
<ul>
<li>feature: Support for generation between two nested objects.</li>
<li>Optimization part of the code.</li>
</ul>
1.3 (2022-04-14) <br>
<ul>
<li>added 2022.X version support.</li>
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/rookie/plugins/bean/JavaMetaBean.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package org.rookie.plugins.bean;

import com.intellij.psi.PsiType;

public class JavaMetaBean {

private String type;
private PsiType type;

private String methodText;

public String getType() {
public PsiType getType() {
return type;
}

public void setType(String type) {
public void setType(PsiType type) {
this.type = type;
}

Expand All @@ -21,4 +23,12 @@ public String getMethodText() {
public void setMethodText(String methodText) {
this.methodText = methodText;
}

@Override
public String toString() {
return "JavaMetaBean{" +
"type=" + type.getPresentableText() +
", methodText='" + methodText + '\'' +
'}';
}
}
Loading

0 comments on commit 7e657ec

Please sign in to comment.