一 自动拼接字符串,自动生成注解

自动拼接字符串

 

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;
		}
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值