自动拼接字符串
public class SQLHelper {
private SQLHelper(){
}
public static void appendSQL(String fileName, String sb) {
try {
File file = new File(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = null;
List<String> list = new ArrayList<String>();
while(null != (line=br.readLine())){
list.add(sb + ".append(\" " + line.trim() + " \");");
}
br.close();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
for(String sql : list) {
bw.write(sql);
bw.newLine();
}
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
二 自动生成注解
public class AnnotationHelper {
private AnnotationHelper() {
}
// TODO 类注解,参数注解需要补上
public static void feignClient(String fileName, String value) {
try {
File file = new File(fileName);
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
String line;
List<String> list = new ArrayList<String>();
Stack<String> stack = new Stack<String>();
while (null != (line = br.readLine())) {
String temp = line;
if (FeignProcessor.isMethod(line)) {
String methodStr = FeignProcessor.getMethodString(stack,
line);
String methodStrTemp = methodStr;// 参数注解需要补上
String[] split = methodStr.split("\\s*");
String methodName = "";
for (String s : split) {
if (s.contains("(")) {
methodName = s.split("[(]")[0];
}
}
list.add(value + methodName + ".do\") \r\n" + methodStrTemp);
} else {
if (FeignProcessor.needAdd(line)) {
list.add(line);
}
}
stack.push(temp);
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class FeignProcessor {
static boolean isMethod(String line) {
if (line.contains(";") && !line.startsWith("package")
&& !line.startsWith("import")
&& !line.contains("FeignClient")
&& !line.contains("interface")) {
return true;
}
return false;
}
static String getMethodString(Stack<String> stack, String str) {
if (stack.empty()) {
return str;
}
String pop = stack.pop();
if (pop.contains("/") || pop.contains(";") || pop.trim().equals("")) {
return str;
} else {
return getMethodString(stack, pop + str);
}
}
static boolean needAdd(String line) {
if (line.contains("\\*") || line.contains("/")
|| line.contains(";") || line.trim().equals("")) {
return true;
}
if (line.startsWith("package") || line.startsWith("import")
|| line.contains("FeignClient")
|| line.contains("interface")) {
return true;
}
return false;
}
}
}