Skip to content

Commit d484329

Browse files
committed
CSharpToJavaModelUtils:支持路径处理、支持Dictionary转Map、支持int转Integer、支持泛型等复杂封装类型、支持并发转换、支持仅导入需要的java类;由于C#类中不包含相对类路径,暂不支持自动导入自定义类
1 parent 0a27d6a commit d484329

1 file changed

Lines changed: 80 additions & 37 deletions

File tree

src/com/zxiaofan/util/jsonBean/CSharpToJavaModelUtils.java

Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Map.Entry;
24+
import java.util.Objects;
2425
import java.util.Scanner;
2526
import java.util.regex.Matcher;
2627
import java.util.regex.Pattern;
@@ -69,15 +70,15 @@ public static void start() {
6970
String path = scanner.nextLine();
7071
System.out.println("请输入生成Java文件的包名:");
7172
String packageName = scanner.nextLine();
72-
transform(path, packageName);
73-
System.out.println("转换完成,请到" + outputPath + "查看转换结果!");
73+
String outputPathDefine = transform(path, packageName);
74+
System.out.println("转换完成,请到" + outputPathDefine + "查看转换结果!");
7475
try {
7576
String[] cmd = new String[5];
7677
cmd[0] = "cmd";
7778
cmd[1] = "/c";
7879
cmd[2] = "start";
7980
cmd[3] = " ";
80-
cmd[4] = outputPath;
81+
cmd[4] = outputPathDefine;
8182
Runtime.getRuntime().exec(cmd);
8283
} catch (IOException e) {
8384
e.printStackTrace();
@@ -112,17 +113,21 @@ public static void start() {
112113
* 绝对路径.
113114
* @param packageName
114115
* 包名.
116+
* @return outputPathDefine
115117
*/
116-
public static void transform(String path, String packageName) {
118+
public static String transform(String path, String packageName) {
117119
String originPath = handlePath(path); // 待转换的目录
118120
if (originPath.endsWith("\\")) {
119121
originPath = originPath.substring(0, originPath.length() - 1);
120122
}
121123
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
122-
outputPath = outputPath + format.format(new Date()) + "\\" + packageName.replaceAll("\\.", "\\\\") + "\\model\\vo";
124+
String outputPathDefine = outputPath + format.format(new Date()) + "\\" + packageName.replaceAll("\\.", "\\\\") + "\\";
125+
outputPathDefine = outputPathDefine + packagePath.replaceAll("\\.", "\\\\");
126+
outputPathDefine = handlePath(outputPathDefine);
123127
List<String> filePaths = getAllFilePath(path);
124128
Map<String, List<FieldVo>> mapData = buildOriginData(filePaths, originPath); // Map<相对路径,字段信息集合>
125-
createJavaModel(mapData, packageName);
129+
createJavaModel(mapData, packageName, outputPathDefine);
130+
return outputPathDefine;
126131
}
127132

128133
/**
@@ -132,9 +137,12 @@ public static void transform(String path, String packageName) {
132137
* map
133138
* @param packageName
134139
* packageName
140+
* @param outputPathDefine
141+
* 生成文件夹绝对路径
135142
*/
136-
private static void createJavaModel(Map<String, List<FieldVo>> map, String packageName) {
137-
createFile(outputPath);
143+
private static void createJavaModel(Map<String, List<FieldVo>> map, String packageName, String outputPathDefine) {
144+
createFile(outputPathDefine);
145+
int totalAll = 0;
138146
for (Entry<String, List<FieldVo>> entry : map.entrySet()) {
139147
String relativePath = entry.getKey();
140148
if (!"".equals(relativePath.trim())) {
@@ -144,7 +152,8 @@ private static void createJavaModel(Map<String, List<FieldVo>> map, String packa
144152
if (fieldVos.isEmpty()) {
145153
continue;
146154
}
147-
StringBuffer buffer = new StringBuffer();
155+
totalAll++;
156+
StringBuilder buffer = new StringBuilder();
148157
if (!"".equals(packageName) && packageName != null && !packageName.endsWith(".")) {
149158
packageName += ".";
150159
}
@@ -153,10 +162,36 @@ private static void createJavaModel(Map<String, List<FieldVo>> map, String packa
153162
// relativePathPackage = relativePathPackage.substring(relativePathPackage.lastIndexOf("."));
154163
// }
155164
buffer.append("package " + packageName + packagePath + relativePathPackage + ";" + rn + rn);
156-
buffer.append(importBigDecimal);
157-
buffer.append(importDate);
158-
buffer.append(importList);
159-
buffer.append(importMap);
165+
List<String> importT = new ArrayList<>();
166+
for (FieldVo vo : fieldVos) {
167+
if (null == vo) {
168+
continue;
169+
}
170+
String[] types = vo.getType().replaceAll(" ", "").split("<|,|>");
171+
for (String type : types) {
172+
if (Objects.equals("BigDecimal", type) && !importT.contains(type)) {
173+
importT.add(type);
174+
} else if (Objects.equals("Date", type) && !importT.contains(type)) {
175+
importT.add(type);
176+
} else if (Objects.equals("List", type) && !importT.contains(type)) {
177+
importT.add(type);
178+
} else if (Objects.equals("Map", type) && !importT.contains(type)) {
179+
importT.add(type);
180+
}
181+
}
182+
}
183+
if (importT.contains("BigDecimal")) {
184+
buffer.append(importBigDecimal);
185+
}
186+
if (importT.contains("Date")) {
187+
buffer.append(importDate);
188+
}
189+
if (importT.contains("List")) {
190+
buffer.append(importList);
191+
}
192+
if (importT.contains("Map")) {
193+
buffer.append(importMap);
194+
}
160195
buffer.append(author);
161196
buffer.append("public class " + fieldVos.get(0).getClassName() + " {" + rn);
162197
// 字段定义
@@ -186,15 +221,15 @@ private static void createJavaModel(Map<String, List<FieldVo>> map, String packa
186221
}
187222
buffer.append("}" + rn);
188223
relativePath = relativePath.replaceAll("\\.", "\\\\");
189-
String finalPath = outputPath + "\\" + relativePath; // + fieldVos.get(0).getClassName() + ".java";
224+
String finalPath = outputPathDefine + "\\" + relativePath; // + fieldVos.get(0).getClassName() + ".java";
190225
if (!"".equals(relativePath)) {
191226
createFile(finalPath);
192227
}
193228
String claseName = fieldVos.get(0).getClassName();
194229
if (claseName.contains("<")) {
195230
claseName = claseName.substring(0, claseName.indexOf("<"));
196231
}
197-
finalPath = outputPath + "\\" + relativePath + "\\" + claseName + ".java";
232+
finalPath = outputPathDefine + "\\" + relativePath + "\\" + claseName + ".java";
198233
try {
199234
FileWriter fw = new FileWriter(finalPath);
200235
fw.write(buffer.toString());
@@ -206,6 +241,7 @@ private static void createJavaModel(Map<String, List<FieldVo>> map, String packa
206241
e.printStackTrace();
207242
}
208243
}
244+
System.out.println("===共计生成" + totalAll + "个文件===");
209245
}
210246

211247
/**
@@ -259,30 +295,37 @@ private static Map<String, List<FieldVo>> buildOriginData(List<String> paths, St
259295
}
260296
fileTxt = formatStr(fileTxt);
261297
fileTxt = fileTxt.substring(fileTxt.indexOf("namespace"));
262-
fileTxt = fileTxt.replaceAll("#region public members", "").replaceAll(", ", ",").replaceAll(" ", " ");
298+
fileTxt = fileTxt.replaceAll("#region public members", "").replaceAll(", ", ",").replaceAll("\\s\\s", " ");
263299
// Start 多匹配模式
264-
StringBuffer pattern = new StringBuffer();
265-
pattern.append("<summary>.{0,30}?(?<desc>.{0,30}?)?</summary>");
266-
pattern.append(".{0,80}?public\\s(?<type>.*?)?\\s");
267-
pattern.append("(?<fieldname>.*?)?\\{");
268-
Matcher matcher = Pattern.compile(pattern.toString()).matcher(fileTxt);
269-
while (matcher.find()) {
270-
FieldVo vo = new FieldVo();
271-
fieldDesc = matcher.group("desc").replaceAll("/", "").trim();
272-
type = matcher.group("type").trim();
273-
fieldNameUpper = matcher.group("fieldname").trim();
274-
if ("class".equals(type) && fieldNameUpper.matches(fileClassname + "<[A-Za-z]+>")) {
275-
fileClassname = fieldNameUpper;
276-
} else if (!"class".equals(type) && !fieldNameUpper.endsWith(")") && !"".equals(fieldNameUpper)) {
277-
vo.setClassName(fileClassname);
278-
vo.setFieldNameUpper(fieldNameUpper);
279-
type = typeTrans(type);
280-
vo.setType(type);
281-
// vo.setRelativePath(relativePath);
282-
vo.setFieldNameLower(upperConvertToLower(fieldNameUpper));
283-
vo.setFieldDesc(fieldDesc);
284-
fieldVos.add(vo);
300+
StringBuilder pattern = new StringBuilder();
301+
pattern.append("<summary>.{0,40}?(?<desc>.{0,40}?)?</summary>");
302+
pattern.append(".{0,80}?public\\s(?<type>[a-zA-Z0-9_,<>\\s]+?)?\\s");
303+
pattern.append("(?<fieldname>[a-zA-Z0-9_<>]+?)?\\s+\\{");
304+
try {
305+
Matcher matcher = Pattern.compile(pattern.toString()).matcher(fileTxt);
306+
while (matcher.find()) {
307+
FieldVo vo = new FieldVo();
308+
fieldDesc = matcher.group("desc").replaceAll("/", "").trim();
309+
type = matcher.group("type").trim();
310+
fieldNameUpper = matcher.group("fieldname").trim();
311+
if ("class".equals(type) && fieldNameUpper.matches(fileClassname + "<[A-Za-z]+>")) {
312+
fileClassname = fieldNameUpper;
313+
} else if (!"class".equals(type) && !fieldNameUpper.endsWith(")") && !"".equals(fieldNameUpper)) {
314+
vo.setClassName(fileClassname);
315+
vo.setFieldNameUpper(fieldNameUpper);
316+
type = typeTrans(type);
317+
vo.setType(type);
318+
// vo.setRelativePath(relativePath);
319+
vo.setFieldNameLower(upperConvertToLower(fieldNameUpper));
320+
vo.setFieldDesc(fieldDesc);
321+
fieldVos.add(vo);
322+
}
285323
}
324+
} catch (Exception e) {
325+
e.printStackTrace();
326+
System.out.println(fileTxt);
327+
System.out.println(pattern.toString());
328+
System.exit(0);
286329
}
287330
// End 多匹配模式
288331
mapData.put(relativePath, fieldVos);

0 commit comments

Comments
 (0)