From 037c6ef6b66f5e8f3e821544ad83704b457f5e7f Mon Sep 17 00:00:00 2001 From: wangbing <1919101440@qq.com> Date: Sat, 7 Dec 2019 00:02:37 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 9ca472529cb4defae3d971b800e38f8688d52090 --- .../javafx/manger/callable/ApiCallable.java | 147 +++++---- .../dbtool/javafx/tool/EntityReader.java | 285 ++++++++++++++++++ .../dbtool/javafx/tool/JavaClassReader.java | 53 +++- .../modules/Java_api/module/req/request.java | 1 + .../modules/Java_api/module/rsp/response.java | 3 + src/main/resources/modules/Java_api/pom.ftl | 100 ++++++ 6 files changed, 515 insertions(+), 74 deletions(-) create mode 100644 src/main/java/xyz/wbsite/dbtool/javafx/tool/EntityReader.java create mode 100644 src/main/resources/modules/Java_api/pom.ftl diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java index 10584d65..93c68b39 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java @@ -5,6 +5,7 @@ import xyz.wbsite.dbtool.javafx.manger.ManagerFactory; import xyz.wbsite.dbtool.javafx.manger.ProjectManager; import xyz.wbsite.dbtool.javafx.po.AbstractDBmapper; import xyz.wbsite.dbtool.javafx.po.Api; +import xyz.wbsite.dbtool.javafx.tool.EntityReader; import xyz.wbsite.dbtool.javafx.tool.JavaClassReader; import xyz.wbsite.dbtool.javafx.tool.Tool; @@ -12,6 +13,8 @@ import java.io.File; import java.io.IOException; import java.util.*; import java.util.concurrent.Callable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class ApiCallable implements Callable { @@ -68,15 +71,16 @@ public class ApiCallable implements Callable { for (Api api : apiList) { if (api.isCheck()) { - File module = new File(domain, api.getModule()); - module.mkdirs(); - File request = new File(module, "req"); - request.mkdirs(); - File response = new File(module, "rsp"); - response.mkdirs(); - File entity = new File(module, "ent"); + File apiModule = new File(domain, api.getModule()); + apiModule.mkdirs(); + File apiRequest = new File(apiModule, "req"); + apiRequest.mkdirs(); + File apiResponse = new File(apiModule, "rsp"); + apiResponse.mkdirs(); + File entity = new File(apiModule, "ent"); entity.mkdirs(); + List entList = new ArrayList<>(); try { //region 生成request JavaClassReader javaClassReader = new JavaClassReader(api.getRequestFile()); @@ -87,13 +91,6 @@ public class ApiCallable implements Callable { } } - Method method = new Method(); - method.setStringMethod(api.getMethod()); - method.setRequest(javaClassReader.getClassName()); - method.setTarget(Tool.getRequestTarget(javaClassReader.getClassName())); - method.setMethod(Tool.getRequestAction(javaClassReader.getClassName())); - method.setManager(Tool.getRequestTarget(javaClassReader.getClassName()) + "Manager"); - { HashMap ctx = new HashMap(); ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); @@ -108,12 +105,44 @@ public class ApiCallable implements Callable { ctx.put("tool", tool); ctx.put("hasList", javaClassReader.isHasList()); ctx.put("findOrSearchflag", javaClassReader.getFindOrSearchflag()); - File file = new File(request.getAbsolutePath(), api.getRequestFile().getName()); + File file = new File(apiRequest.getAbsolutePath(), api.getRequestFile().getName()); freeMarkerManager.outputTemp(file, "Java_api/module/req/request.java", ctx); System.out.println("生成文件" + file.getName() + "成功"); } //endregion + + for (String s : javaClassReader.getDeptEntList()) { + Pattern compile = Pattern.compile("(.*\\.ent)\\.(.*)"); + Matcher matcher = compile.matcher(s); + + if (matcher.find()) { + String Package = matcher.group(1); + String EntName = matcher.group(2); + + List files = Tool.find(this.module, EntName + ".java"); + + for (File file : files) { + EntityReader entReader = new EntityReader(file); + if (Package.equals(entReader.getPackageName())) { + HashMap ctx = new HashMap(); + ctx.put("package", "package " + entReader.getDomainName() + "." + entReader.getModuleName() + "." + "request;"); + ctx.put("domain", entReader.getDomainName()); + ctx.put("module", entReader.getModuleName()); + ctx.put("importList", entReader.getImportList()); + ctx.put("annotation", entReader.getAnnotationList()); + ctx.put("className", entReader.getClassName().replaceAll("Entity", "")); + ctx.put("body", entReader.getBody()); + ctx.put("tool", tool); + File EntFile = new File(entity.getAbsolutePath(), EntName); + freeMarkerManager.outputTemp(EntFile, "Java_api/module/ent/entity.java", ctx); + System.out.println("生成文件" + EntName + "成功"); + + } + } + + } + } } catch (IOException e) { e.printStackTrace(); } @@ -128,24 +157,61 @@ public class ApiCallable implements Callable { } } + List entNames = new ArrayList<>(); + + for (String s : javaClassReader.getDeptEntList()) { + Pattern compile = Pattern.compile("(.*\\.ent)\\.(.*)"); + Matcher matcher = compile.matcher(s); + + if (matcher.find()) { + String Package = matcher.group(1); + String EntName = matcher.group(2); + entNames.add(EntName); + + List files = Tool.find(this.module, EntName + ".java"); + + for (File file : files) { + EntityReader entReader = new EntityReader(file); + if (Package.equals(entReader.getPackageName())) { + HashMap ctx = new HashMap(); + ctx.put("package", "package " + entReader.getDomainName() + "." + entReader.getModuleName() + "." + "request;"); + ctx.put("domain", entReader.getDomainName()); + ctx.put("module", entReader.getModuleName()); + ctx.put("importList", entReader.getImportList()); + ctx.put("annotation", entReader.getAnnotationList()); + ctx.put("className", entReader.getClassName().replaceAll("Entity", "")); + ctx.put("body", entReader.getBody()); + ctx.put("tool", tool); + File EntFile = new File(entity.getAbsolutePath(), EntName+".java"); + freeMarkerManager.outputTemp(EntFile, "Java_api/module/ent/entity.java", ctx); + System.out.println("生成文件" + EntName + "成功"); + } + } + + } + } + { HashMap ctx = new HashMap(); ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); ctx.put("domain", javaClassReader.getDomainName()); ctx.put("module", javaClassReader.getModuleName()); ctx.put("importList", javaClassReader.getImportList()); + ctx.put("entNames", entNames); ctx.put("annotation", javaClassReader.getAnnotationList()); ctx.put("className", javaClassReader.getClassName().replaceAll("Response", "")); ctx.put("body", javaClassReader.getBody()); ctx.put("tool", tool); ctx.put("hasList", javaClassReader.isHasList()); ctx.put("Tclass", javaClassReader.getTclass()); - File file = new File(response.getAbsolutePath(), api.getResponseFile().getName().replaceAll("Request", "Response")); + File file = new File(apiResponse.getAbsolutePath(), api.getResponseFile().getName().replaceAll("Request", "Response")); freeMarkerManager.outputTemp(file, "Java_api/module/rsp/response.java", ctx); System.out.println("生成文件" + api.getResponseFile().getName() + "成功"); } //endregion + + } catch (IOException e) { e.printStackTrace(); } @@ -378,53 +444,4 @@ public class ApiCallable implements Callable { System.out.println("finish"); return true; } - - - public class Method { - private String request; - private String stringMethod; - private String manager; - private String method; - private String target; - - public String getRequest() { - return request; - } - - public void setRequest(String request) { - this.request = request; - } - - public String getTarget() { - return target; - } - - public void setTarget(String target) { - this.target = target; - } - - public String getStringMethod() { - return stringMethod; - } - - public void setStringMethod(String stringMethod) { - this.stringMethod = stringMethod; - } - - public String getManager() { - return manager; - } - - public void setManager(String manager) { - this.manager = manager; - } - - public String getMethod() { - return method; - } - - public void setMethod(String method) { - this.method = method; - } - } } diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/tool/EntityReader.java b/src/main/java/xyz/wbsite/dbtool/javafx/tool/EntityReader.java new file mode 100644 index 00000000..4ae6d4b3 --- /dev/null +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/EntityReader.java @@ -0,0 +1,285 @@ +package xyz.wbsite.dbtool.javafx.tool; + +import java.io.*; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class EntityReader { + private File javaClass; + + private String packageName; + private String domainName; + private String moduleName; + private List annotationList; + private List importList; + private Set deptReqList; + private Set deptEntList; + private String className; + private String fatherName; + private List body; + private boolean hasList = false; + private String findOrSearchflag = "0"; + private String Tclass = null; + + public EntityReader(File javaClass) throws IOException { + this.javaClass = javaClass; + importList = new ArrayList<>(); + annotationList = new ArrayList<>(); + deptReqList = new HashSet<>(); + deptEntList = new HashSet<>(); + domainName = ""; + read(); + } + + private void read() throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(javaClass), "utf-8")); + + int step = 1;//1-读取package,2-读取import,3-读取类注释,4-读取类名、父类,5-读取类体 + + String line = null; + StringBuffer zs = new StringBuffer(); + StringBuffer sb = new StringBuffer(); + while ((line = bufferedReader.readLine()) != null) { + if (line.matches("\\s*package\\s(.*);")) { + Pattern compile = Pattern.compile("\\s*package\\s(.*);"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + packageName = matcher.group(1); + } + + String s = line.replaceAll("package ", ""); + s = s.replaceAll(";", ""); + String[] split = s.split("\\."); + + if ("req".equals(split[split.length - 1]) || "rsp".equals(split[split.length - 1]) || "ent".equals(split[split.length - 1])) { + moduleName = split[split.length - 2]; + for (int i = 0; i < split.length - 2; i++) { + domainName += split[i] + "."; + } + domainName = domainName.substring(0, domainName.length() - 1); + } + + continue; + } + if (line.matches("import\\s.*")) { + if (line.contains(".ent.")) { + Pattern compile = Pattern.compile("import\\s+(.*\\.ent\\..*);"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + String group = matcher.group(1); + deptEntList.add(group); + } + } else if (line.contains(".req.")) { + Pattern compile = Pattern.compile("import\\s+(.*\\.req\\..*);"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + String group = matcher.group(1); + deptReqList.add(group); + } + } else if (!line.contains("frame")) { + importList.add(line); + } + continue; + } + if (line.contains("/**") || line.contains("*") || line.contains("*/")) { + if (zs != null) { + zs.append(line + "\n"); + annotationList.add(line); + if (line.contains("*/")) { + zs = null; + } + } + } + if (line.contains("class")) { + line = line.replaceAll("\\{", " { "); + line = line.replaceAll("}", " } "); + line = line.replaceAll("\\s+", " "); + String[] split = line.split("\\s"); + for (int i = 0; i < split.length; i++) { + if ("class".equals(split[i])) { + className = split[i + 1]; + } + if ("extends".equals(split[i])) { + fatherName = split[i + 1]; + + if (fatherName.contains("FindResponse") || fatherName.contains("GetAllResponse")) { + hasList = true; + + Pattern pattern = Pattern.compile("<(.*?)>"); + Matcher matcher = pattern.matcher(fatherName); + if (matcher.find()) { + String group = matcher.group(1); + Tclass = group; + } + } + if (fatherName.contains("FindRequest")) { + findOrSearchflag = "1"; + } else if (fatherName.contains("SearchRequest")) { + findOrSearchflag = "2"; + } + } + } + } + sb.append(line + "\n"); + } + + body = new ArrayList<>(Arrays.asList(sb.substring(sb.indexOf("{")).split("\n"))); + + for (int i = 0; i < body.size(); i++) { + if (body.get(i).contains("{")) { + body.set(i, body.get(i).replaceAll("\\{", "")); + String s = body.get(i); + if (s.length() <= 1) { + body.remove(i); + } + break; + } + } + Iterator iterator = body.iterator(); + while (iterator.hasNext()){ + String next = iterator.next(); + if (next.contains("@ColumnName")){ + iterator.remove(); + } + + if (next.contains("@ColumnDescription")){ + iterator.remove(); + } + } + + + for (int i = body.size() - 1; i >= 0; i--) { + if (body.get(i).contains("}")) { + body.set(i, body.get(i).replaceAll("}", "")); + break; + } + } + if (body.size() > 1 && "".equals(body.get(0))) { + body.remove(0); + } + if (body.size() > 1 && "".equals(body.get(body.size() - 1))) { + body.remove(body.size() - 1); + } + + bufferedReader.close(); + } + + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public String getDomainName() { + return domainName; + } + + public void setDomainName(String domainName) { + this.domainName = domainName; + } + + public String getModuleName() { + return moduleName; + } + + public void setModuleName(String moduleName) { + this.moduleName = moduleName; + } + + public List getAnnotationList() { + return annotationList; + } + + public void setAnnotationList(List annotationList) { + this.annotationList = annotationList; + } + + public List getImportList() { + return importList; + } + + public void setImportList(List importList) { + this.importList = importList; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public String getFatherName() { + return fatherName; + } + + public void setFatherName(String fatherName) { + this.fatherName = fatherName; + } + + public List getBody() { + return body; + } + + public void setBody(List body) { + this.body = body; + } + + public File getJavaClass() { + return javaClass; + } + + public void setJavaClass(File javaClass) { + this.javaClass = javaClass; + } + + public boolean isHasList() { + return hasList; + } + + public void setHasList(boolean hasList) { + this.hasList = hasList; + } + + public String getTclass() { + return Tclass; + } + + public String getFindOrSearchflag() { + return findOrSearchflag; + } + + public void setFindOrSearchflag(String findOrSearchflag) { + this.findOrSearchflag = findOrSearchflag; + } + + public void setTclass(String tclass) { + Tclass = tclass; + } + + public Set getDeptReqList() { + return deptReqList; + } + + public void setDeptReqList(Set deptReqList) { + this.deptReqList = deptReqList; + } + + public Set getDeptEntList() { + return deptEntList; + } + + public void setDeptEntList(Set deptEntList) { + this.deptEntList = deptEntList; + } + + public static void main(String[] args) throws IOException { + EntityReader javaClassReader = new EntityReader(new File("D:\\wangbing\\Project\\dbtool\\target\\project\\EXAMPLE-WEB\\src\\main\\java\\com\\example\\module\\system\\rsp\\DictFindResponse.java")); + + System.out.println(); + } +} diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaClassReader.java b/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaClassReader.java index 563557a1..2f4c9c07 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaClassReader.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/JavaClassReader.java @@ -1,9 +1,7 @@ package xyz.wbsite.dbtool.javafx.tool; import java.io.*; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -15,6 +13,8 @@ public class JavaClassReader { private String moduleName; private List annotationList; private List importList; + private Set deptReqList; + private Set deptEntList; private String className; private String fatherName; private List body; @@ -26,6 +26,8 @@ public class JavaClassReader { this.javaClass = javaClass; importList = new ArrayList<>(); annotationList = new ArrayList<>(); + deptReqList = new HashSet<>(); + deptEntList = new HashSet<>(); domainName = ""; read(); } @@ -39,8 +41,12 @@ public class JavaClassReader { StringBuffer zs = new StringBuffer(); StringBuffer sb = new StringBuffer(); while ((line = bufferedReader.readLine()) != null) { - if (line.startsWith("package")) { - packageName = line; + if (line.matches("\\s*package\\s(.*);")) { + Pattern compile = Pattern.compile("\\s*package\\s(.*);"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + packageName = matcher.group(1); + } String s = line.replaceAll("package ", ""); s = s.replaceAll(";", ""); @@ -56,11 +62,24 @@ public class JavaClassReader { continue; } - if (line.startsWith("import")) { - if (!line.contains("frame")) { + if (line.matches("import\\s.*")) { + if (line.contains(".ent.")) { + Pattern compile = Pattern.compile("import\\s+(.*\\.ent\\..*);"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + String group = matcher.group(1); + deptEntList.add(group); + } + } else if (line.contains(".req.")) { + Pattern compile = Pattern.compile("import\\s+(.*\\.req\\..*);"); + Matcher matcher = compile.matcher(line); + if (matcher.find()) { + String group = matcher.group(1); + deptReqList.add(group); + } + } else if (!line.contains("frame")) { importList.add(line); } - continue; } if (line.contains("/**") || line.contains("*") || line.contains("*/")) { @@ -229,8 +248,24 @@ public class JavaClassReader { Tclass = tclass; } + public Set getDeptReqList() { + return deptReqList; + } + + public void setDeptReqList(Set deptReqList) { + this.deptReqList = deptReqList; + } + + public Set getDeptEntList() { + return deptEntList; + } + + public void setDeptEntList(Set deptEntList) { + this.deptEntList = deptEntList; + } + public static void main(String[] args) throws IOException { - JavaClassReader javaClassReader = new JavaClassReader(new File("C:\\dbtool\\auth\\src\\main\\java\\edu\\services\\auth\\rsp\\FileFindResponse.java")); + JavaClassReader javaClassReader = new JavaClassReader(new File("D:\\wangbing\\Project\\dbtool\\target\\project\\EXAMPLE-WEB\\src\\main\\java\\com\\example\\module\\system\\rsp\\DictFindResponse.java")); System.out.println(); } diff --git a/src/main/resources/modules/Java_api/module/req/request.java b/src/main/resources/modules/Java_api/module/req/request.java index a86adc17..f6435eec 100644 --- a/src/main/resources/modules/Java_api/module/req/request.java +++ b/src/main/resources/modules/Java_api/module/req/request.java @@ -23,6 +23,7 @@ public class ${className}Request <#if findOrSearchflag=='1'>extends ApiFindReque ${i} + public void check() { } diff --git a/src/main/resources/modules/Java_api/module/rsp/response.java b/src/main/resources/modules/Java_api/module/rsp/response.java index 9f8bf10b..a14d5168 100644 --- a/src/main/resources/modules/Java_api/module/rsp/response.java +++ b/src/main/resources/modules/Java_api/module/rsp/response.java @@ -3,6 +3,9 @@ package ${domain}.${module}.rsp; <#list importList as i> ${i} +<#list entNames as entName> +import ${domain}.${module}.ent.${entName}; + <#if hasList> import ${domain}.ApiFindResponse; <#else> diff --git a/src/main/resources/modules/Java_api/pom.ftl b/src/main/resources/modules/Java_api/pom.ftl new file mode 100644 index 00000000..e46ccc78 --- /dev/null +++ b/src/main/resources/modules/Java_api/pom.ftl @@ -0,0 +1,100 @@ + + + 4.0.0 + + com.wb + ${api} + 1.0-SNAPSHOT + jar + + + true + + + + + com.fasterxml.jackson.core + jackson-databind + 2.9.6 + + + com.squareup.okhttp3 + okhttp + 3.4.1 + + + + junit + junit + 4.12 + + + <#-- + <#----> + <#--org.hibernate.validator--> + <#--hibernate-validator--> + <#--6.0.10.Final--> + <#----> + <#----> + <#--org.glassfish--> + <#--javax.el--> + <#--3.0.0--> + <#----> + + + + + ${r"${artifactId}"}-${r"${version}"} + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + UTF-8 + + + + org.apache.maven.plugins + maven-dependency-plugin + 2.7 + + + copy-dependencies + prepare-package + + copy-dependencies + + + + ${r"${project.build.directory}"}/lib + + false + + false + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + true + lib/ + com.example.ApiClient + + + + + + + \ No newline at end of file