import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Pattern;
public class GenerateUnitTestJava {
public static void main(String[] args) throws IOException {
Path.PROJECT_NAME = "";
Delete.delete();
List<ClassTemplate> l = Scan.scan();
for (ClassTemplate t : l) {
Write.write(t);
}
}
private static class Path {
static String PROJECT_NAME = "";
static String find() throws FileNotFoundException {
return String.format("%s/%s", ResourceUtils.getFile("").getAbsolutePath(), PROJECT_NAME);
}
}
private static class Scan {
static Pattern JAVA_END = Pattern.compile(".+\\.java$");
static Pattern PACKAGE_PT = Pattern.compile(".*package .*;.*");
static Pattern STATIC_CLASS_PT = Pattern.compile(".*[ ]+static[ ]+class[ ]+.*");
static Pattern CLASS_PT = Pattern.compile(".*[ ]+class[ ]+.*");
static Pattern ENUM_PT = Pattern.compile(".*[ ]+enum[ ]+.*");
static String[] SP_ARR = {
"{", "<", " implements ", " extends "};
static List<ClassTemplate> scan() throws IOException {
return scan(Path.find() + "/src/main/java/");
}
static List<ClassTemplate> scan(String filePath) throws IOException {
return scan(new File(filePath));
}
static List<ClassTemplate> scan(File f) throws IOException {
if (null == f) {
return Collections.emptyList();
}
if (f.isDirectory()) {
File[] files = f.listFiles();
if (files.length < 1) {
return Collections.emptyList();
}
List<ClassTemplate> r = new ArrayList<>();
for (File f0 : files) {
r.addAll(scan(f0));
}
return r;
}
String fn = f.getName();
if (!JAVA_END.matcher(fn).matches()) {
return Collections.emptyList();
}
List<String> cns = findClassName(f);
List<ClassTemplate> r = new ArrayList<>();
for (String cn : cns) {
try {
Class<?> tClass = Class.forName(cn);
int mds = tClass.getModifiers();
if (tClass.isInterface() || Modifier.isPrivate(mds)
|| (Modifier.isAbstract(mds) && !tClass.isEnum())) {
continue;
}
r.add(Builder.buildTemplate(tClass));
} catch (Throwable throwable) {
// default
}
}
return r;
}
static List<String> findClassName(File f) throws IOException {
List<String> r = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(f));
String t;
while (null != (t=br.readLine()) && !PACKAGE_PT.matcher(t).matches()) {
// default;
}
if (null == t) {
br.close();
return Collections.emptyList();
}
int ps = t.indexOf("package ");
String pn = t.substring(ps + 8, t.indexOf(";",ps));
String fn = f.getName().substring(0, f.getName().length() - 5);
r.add(String.format("%s.%s", pn, fn));
while (null != (t=br.readLine())) {
if (t.length() < 7) {
continue;
}
findNormalClass(r, t, pn, fn);
findStaticClass(r, t, pn, fn);
findNormalEnum(r, t, pn, fn);
}
br.close();
return r;
}
static String getJName(String t, int f) {
int i = t.length();
for (String s : SP_ARR) {
int si = t.indexOf(s, f);
i = si > 0 && si < i ? si : i;
}
return t.substring(f, i).trim();
}
private static void findNormalEnum(List<String> r, String t, String pn, String fn) {
if (!ENUM_PT.matcher(t).matches()) {
return;
}
String cn = getJName(t, t.indexOf(" enum ") + 6);
if (cn.equals(fn)) {
return;
}
r.add(String.format("%s.%s", pn, cn));
r.add(String.format("%s.%s$%s", pn, fn, cn));
}
private static void findStaticClass(List<String> r, String t, String pn, String fn) {
if(!STATIC_CLASS_PT.matcher(t).matches
基础unitTest生成
最新推荐文章于 2023-06-02 14:27:34 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)