Skip to content

Commit 8c475ef

Browse files
author
杨利兵
committed
代码优化
1 parent c6b5aa8 commit 8c475ef

23 files changed

+55
-155
lines changed

message.xlsx

-191 Bytes
Binary file not shown.

src/main/java/com/yanglb/codegen/core/GenFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.yanglb.codegen.core;
1717

1818
import com.yanglb.codegen.exceptions.CodeGenException;
19+
import com.yanglb.codegen.utils.ObjectUtil;
1920
import com.yanglb.codegen.utils.Resources;
2021

2122
public class GenFactory<T> {
@@ -41,9 +42,9 @@ public static <T> T createByName(String className) throws CodeGenException {
4142

4243
private T create(String implName)
4344
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
44-
T instance = null;
4545
if (implName.startsWith(".")) implName = "com.yanglb.codegen" + implName;
46-
instance = (T) Class.forName(implName).newInstance();
47-
return instance;
46+
47+
Object instance = Class.forName(implName).newInstance();
48+
return ObjectUtil.cast(instance);
4849
}
4950
}

src/main/java/com/yanglb/codegen/core/parser/BaseParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public ParamaModel parsing() {
152152
file = as[1];
153153

154154
// 检查参数是否正确
155-
if (Conf.supportCommands().indexOf(cmd) < 0) {
155+
if (!Conf.supportCommands().contains(cmd)) {
156156
throw new IllegalArgumentException(String.format("未知命令: %s,请使用 --help 命令查看用法。", cmd));
157157
}
158158
File f = new File(file);

src/main/java/com/yanglb/codegen/core/reader/BaseReader.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ private void doReader() throws CodeGenException {
172172
* @return
173173
*/
174174
private boolean isReadable(String sheetName) {
175-
if(sheetName.equals("说明") || sheetName.startsWith("#")) {
176-
return false;
177-
}
178-
return true;
175+
return !sheetName.equals("说明") && !sheetName.startsWith("#");
179176
}
180177

181178
/**

src/main/java/com/yanglb/codegen/core/reader/IReader.java

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public interface IReader<T> {
2626
* @return
2727
* @throws CodeGenException
2828
*/
29-
public List<T> reader(String excelFile) throws CodeGenException;
29+
List<T> reader(String excelFile) throws CodeGenException;
3030

3131
/**
3232
* 读取Excel中指定的几个Sheet
@@ -35,7 +35,7 @@ public interface IReader<T> {
3535
* @return
3636
* @throws CodeGenException
3737
*/
38-
public List<T> reader(String excelFile, String[] sheets) throws CodeGenException;
38+
List<T> reader(String excelFile, String[] sheets) throws CodeGenException;
3939

4040
/**
4141
* 读取Excel中指定的一个Sheet
@@ -44,41 +44,12 @@ public interface IReader<T> {
4444
* @return
4545
* @throws CodeGenException
4646
*/
47-
public T reader(String excelFile, String sheet) throws CodeGenException;
48-
49-
// /**
50-
// * 子类必须实现
51-
// * @param sheet
52-
// * @return
53-
// * @throws UnImplementException
54-
// * @throws CodeGenException
55-
// */
56-
// protected T onReader(XSSFSheet sheet) throws UnImplementException, CodeGenException;
57-
//
58-
// /**
59-
// * 进行读取
60-
// * @throws CodeGenException
61-
// */
62-
// private void doReader() throws CodeGenException;
63-
//
64-
// /**
65-
// * 检查特定的Sheet名是否可读
66-
// * @param sheetName
67-
// * @return
68-
// */
69-
// private boolean isReadable(String sheetName);
70-
71-
// /**
72-
// * 取得Cell的字符串数据,当不是字符串时进行转换
73-
// * @param cell
74-
// * @return
75-
// */
76-
// public String getCellStringValue(XSSFCell cell);
77-
47+
T reader(String excelFile, String sheet) throws CodeGenException;
48+
7849
/**
7950
* 设置开始位置
8051
* @param startRowNo 开始行
8152
* @param startColNo 开始列
8253
*/
83-
public void setStartPoint(int startRowNo, int startColNo);
54+
void setStartPoint(int startRowNo, int startColNo);
8455
}

src/main/java/com/yanglb/codegen/core/reader/ISettingReader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ public interface ISettingReader extends IReader<HashMap<String, String>> {
2626
* @return
2727
* @throws CodeGenException
2828
*/
29-
public HashMap<String, String> settingReader() throws CodeGenException;
29+
HashMap<String, String> settingReader() throws CodeGenException;
3030

3131
/**
3232
* 读取一个配置项目(会将Infos内容添加到结果集中,且属性添加generator前缀)
3333
* @param settingSheet
3434
* @return
3535
* @throws CodeGenException
3636
*/
37-
public HashMap<String, String> settingReader(String settingSheet) throws CodeGenException;
37+
HashMap<String, String> settingReader(String settingSheet) throws CodeGenException;
3838

3939
/**
4040
* 读取多个配置项目(会将Infos内容添加到结果集中,且属性添加generator前缀)
4141
* @param settingSheets
4242
* @return
4343
* @throws CodeGenException
4444
*/
45-
public HashMap<String, String> settingReader(String[] settingSheets) throws CodeGenException;
45+
HashMap<String, String> settingReader(String[] settingSheets) throws CodeGenException;
4646
}

src/main/java/com/yanglb/codegen/core/reader/ITableReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ public interface ITableReader extends IReader<TableModel> {
3232
* @return
3333
* @throws CodeGenException
3434
*/
35-
public TableModel reader(XSSFSheet sheet) throws CodeGenException;
35+
TableModel reader(XSSFSheet sheet) throws CodeGenException;
3636
}

src/main/java/com/yanglb/codegen/core/translator/BaseDdlTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
public class BaseDdlTranslator extends BaseSqlTranslator<DdlModel> {
3030
// 外键关系列表
31-
protected List<ForeignModel> foreignKeyList = new ArrayList<ForeignModel>();
31+
protected final List<ForeignModel> foreignKeyList = new ArrayList<>();
3232

3333
@Override
3434
protected void onBeforeTranslate() throws CodeGenException {

src/main/java/com/yanglb/codegen/core/translator/BaseTranslator.java

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
public class BaseTranslator<T> implements ITranslator<T> {
3030
protected T model;
31-
protected WritableModel writableModel;
31+
protected final WritableModel writableModel;
3232
protected HashMap<String, String> settingMap;
3333
protected ParamaModel paramaModel;
3434

@@ -89,52 +89,4 @@ private void doTranslate() throws CodeGenException{
8989
this.onTranslate();
9090
this.onAfterTranslate();
9191
}
92-
93-
/**
94-
* 模板替换
95-
* @param template 模板内容
96-
* @param replaceModel
97-
* @return
98-
* @throws CodeGenException
99-
*/
100-
protected String replaceFlags(String template, Object replaceModel) throws CodeGenException {
101-
List<String> flags = StringUtil.findFlags(template);
102-
String data = new String(template);
103-
for(String key:flags) {
104-
// Map元素
105-
String value = null;
106-
boolean hasKey = false;
107-
if(this.settingMap.containsKey(key)) {
108-
hasKey = true;
109-
value = this.settingMap.get(key);
110-
} else if(replaceModel != null) {
111-
// Model元素
112-
Field field = null;
113-
hasKey = true;
114-
// 在replaceModel查找key属性,如果有则使用该属性值。
115-
try {
116-
field = ObjectUtil.getDeepField(replaceModel.getClass(), key);
117-
field.setAccessible(true);
118-
if(field.get(replaceModel) != null) {
119-
value = field.get(replaceModel).toString();
120-
}
121-
} catch (NoSuchFieldException e) {
122-
// 没有此属性
123-
hasKey = false;
124-
} catch (Exception e) {
125-
throw new CodeGenException(String.format(Resources.getString("E_006"),
126-
replaceModel.getClass().toString(),
127-
field.getName(),
128-
e.getMessage()));
129-
}
130-
}
131-
132-
// 如果有值则替换
133-
if(hasKey) {
134-
String f = String.format("\\$\\{%s\\}", key);
135-
data = data.replaceAll(f, value);
136-
}
137-
}
138-
return data;
139-
}
14092
}

src/main/java/com/yanglb/codegen/core/translator/ITranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public interface ITranslator<T> {
3030
* @return WritableModel 一个可写的Model
3131
* @throws CodeGenException 翻译出错时抛出此异常
3232
*/
33-
public WritableModel translate(HashMap<String, String> settingMap, ParamaModel paramaModel, T model) throws CodeGenException;
33+
WritableModel translate(HashMap<String, String> settingMap, ParamaModel paramaModel, T model) throws CodeGenException;
3434

3535
// /**
3636
// * 预翻译器(子类如果重写,则必须显示调用超类的此方法)

0 commit comments

Comments
 (0)