Skip to content

Commit 0651597

Browse files
committed
利用反射对2022.3.3及以上版本进行兼容性支持
1 parent ba3ccba commit 0651597

4 files changed

Lines changed: 40 additions & 23 deletions

File tree

src/main/java/com/sjhy/plugin/actions/MainAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.sjhy.plugin.entity.TypeMapper;
1616
import com.sjhy.plugin.enums.MatchType;
1717
import com.sjhy.plugin.tool.CacheDataUtils;
18+
import com.sjhy.plugin.tool.CompatibleUtils;
1819
import com.sjhy.plugin.tool.CurrGroupUtils;
1920
import com.sjhy.plugin.tool.StringUtils;
2021
import com.sjhy.plugin.ui.SelectSavePath;
@@ -84,7 +85,7 @@ private boolean typeValidator(Project project, DbTable dbTable) {
8485

8586
FLAG:
8687
for (DasColumn column : columns) {
87-
String typeName = column.getDataType().getSpecification();
88+
String typeName = CompatibleUtils.getDataType(column).getSpecification();
8889
for (TypeMapper typeMapper : typeMapperList) {
8990
try {
9091
if (typeMapper.getMatchType() == MatchType.ORDINARY) {
@@ -114,7 +115,7 @@ private boolean typeValidator(Project project, DbTable dbTable) {
114115

115116
public static class Dialog extends DialogWrapper {
116117

117-
private String typeName;
118+
private final String typeName;
118119

119120
private JPanel mainPanel;
120121

src/main/java/com/sjhy/plugin/actions/MainActionGroup.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,7 @@ public class MainActionGroup extends ActionGroup {
2525
/**
2626
* 缓存数据工具类
2727
*/
28-
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
29-
30-
/**
31-
* 是否不存在子菜单
32-
*/
33-
private boolean notExistsChildren;
34-
35-
/**
36-
* 是否分组按钮
37-
*
38-
* @return 是否隐藏
39-
*/
40-
@Override
41-
public boolean hideIfNoVisibleChildren() {
42-
return this.notExistsChildren;
43-
}
28+
private final CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
4429

4530

4631
/**
@@ -87,7 +72,6 @@ public AnAction[] getChildren(@Nullable AnActionEvent event) {
8772
//保存数据到缓存
8873
cacheDataUtils.setDbTableList(dbTableList);
8974
cacheDataUtils.setSelectDbTable(selectDbTable);
90-
this.notExistsChildren = false;
9175
return getMenuList();
9276
}
9377

@@ -134,7 +118,6 @@ public void actionPerformed(@NotNull AnActionEvent e) {
134118
* @return 空菜单组
135119
*/
136120
private AnAction[] getEmptyAnAction() {
137-
this.notExistsChildren = true;
138121
return AnAction.EMPTY_ARRAY;
139122
}
140123
}

src/main/java/com/sjhy/plugin/dto/ColumnInfoDTO.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
import com.intellij.psi.PsiField;
55
import com.sjhy.plugin.entity.TypeMapper;
66
import com.sjhy.plugin.enums.MatchType;
7+
import com.sjhy.plugin.tool.CompatibleUtils;
78
import com.sjhy.plugin.tool.CurrGroupUtils;
89
import com.sjhy.plugin.tool.DocCommentUtils;
910
import com.sjhy.plugin.tool.NameUtils;
1011
import lombok.Data;
1112
import lombok.NoArgsConstructor;
1213

13-
import java.util.HashMap;
14-
import java.util.Map;
1514
import java.util.regex.Pattern;
1615

1716
/**
@@ -36,7 +35,7 @@ public ColumnInfoDTO(PsiField field) {
3635
public ColumnInfoDTO(DasColumn column) {
3736
this.name = NameUtils.getInstance().getJavaName(column.getName());
3837
this.comment = column.getComment();
39-
this.type = getJavaType(column.getDataType().toString());
38+
this.type = getJavaType(CompatibleUtils.getDataType(column).toString());
4039
this.custom = false;
4140
this.ext = "{}";
4241
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.sjhy.plugin.tool;
2+
3+
import com.intellij.database.model.DasColumn;
4+
import com.intellij.database.model.DataType;
5+
6+
import java.lang.reflect.InvocationTargetException;
7+
import java.lang.reflect.Method;
8+
9+
/**
10+
* 兼容工具
11+
*
12+
* @author makejava
13+
* @date 2023/04/04 17:08
14+
*/
15+
public class CompatibleUtils {
16+
17+
public static DataType getDataType(DasColumn dasColumn) {
18+
try {
19+
// 兼容2022.3.3及以上版本
20+
Method getDasTypeMethod = dasColumn.getClass().getMethod("getDasType");
21+
Object dasType = getDasTypeMethod.invoke(dasColumn);
22+
Method toDataTypeMethod = dasType.getClass().getMethod("toDataType");
23+
return (DataType) toDataTypeMethod.invoke(dasType);
24+
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
25+
// 兼容2022.3.3以下版本
26+
try {
27+
Method getDataTypeMethod = dasColumn.getClass().getMethod("getDataType");
28+
return (DataType) getDataTypeMethod.invoke(dasColumn);
29+
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
30+
throw new RuntimeException(ex);
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)