package temp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

/**
 * 此类文件作用于为大量类文件
 * 加上类注释,方法注释
 * 加注释时不会覆盖已有注释
 * @author lKF44520
 * @date   2011-07-20
 */
public class RemarkHelper {
	public static void main(String[] args) throws IOException {
		RemarkHelper ft = new RemarkHelper();
		List<String> path = new ArrayList<String>();
		boolean isInputPath = true;
		Scanner input = new Scanner(System.in);
		while (true) {
			if (isInputPath) {
				System.out
						.println("***********************************************************");
				System.out.println("1.请输入需要新增注释的文件路径(多个路径以','分开)∶");
				String str = input.next();
				if (str.trim().equals(""))
					return;
				String[] temp = str.split(",");
				System.out
						.println("2.输入路径为:>>>------------------------------------------<<<<");
				for (String s : temp) {
					if (!s.trim().equals("")) {
						path.add(s.trim());
						System.out.println("  path-->" + s);
					}
				}
				isInputPath = false;
			} else {
				System.out.print("\n3.请确认输入路径(Y 确认 N 重输)∶");
				String str = input.next();
				if (str.trim().toLowerCase().equals("y")) {
					for (String filepath : path) {
						ft.addRemark(filepath);
					}
					System.out.print("\n4.是否继续?(Y 继续  )");
					String str1 = input.next();
					if (str1.trim().toLowerCase().equals("y")) {
						path.clear();
						isInputPath = true;
					} else {
						break;
					}
				} else if (str.trim().toLowerCase().equals("n")) {
					path.clear();
					isInputPath = true;
				} else {
					System.out.println("**************************输入有误!****************************");
				}
			}

		}
	}

	/**
	 *  新增注释的主方法
	 * @param filePath  文件路径
	 * @throws IOException
	 * @author lKF44520
	 * @date 2011-07-19
	 */
	private void addRemark(String filePath) throws IOException {
		StringBuffer sbf = new StringBuffer();
		// 判断文件
		File file = new File(filePath);
		if (!file.exists() || file.isDirectory()) {
			System.out.println("文件:--> " + filePath + "<--路径有误!");
			return;
		} else if (!file.canWrite()) {
			System.out.println("文件:--> " + filePath + "<--属性只读!");
			return;
		}
		// 读文件
		String str = readTXTFile(file);
		// 处理文件
		sbf.append(editString(str.trim()));
		// 写文件
		writeFile(file, sbf.toString());
		System.out.println("文件:---> " + filePath + " <---处理成功!");
	}

	/**
	 * 处理文字
	 * 传入类代码进行新增注释处理,
	 * 返回处理后代码
	 * @param str  类代码 
	 * @return     处理后代码
	 */
	public String editString(String str) {
		StringBuffer sbf = new StringBuffer();
		String[] temp = str.split("\r");
		boolean isremark = false;
		boolean cont = false;
		String temp_line = "";
		for (String t : temp) {
			t = t.replace("\n", "");
			// 是否存在注释
			if (t.trim().endsWith("*/")) {
				isremark = true;
			}
			// 说明不存在注释
			if (!t.trim().equals("") && !t.trim().startsWith("public")
					&& !t.trim().startsWith("private")
					&& !t.trim().startsWith("protected")
					&& !t.trim().endsWith("*/")) {
				isremark = false;
			}

			// 开始寻找注释段
			if ((t.trim().startsWith("public")
					|| t.trim().startsWith("private") || t.trim().startsWith(
					"protected"))
					&& !isremark || cont) {
				// 类注释
				if (t.replace(" ", "").startsWith("publicclass")) {
					String remark = "";
					remark = "\r/**\r" + "*  \r";
					// 附加注释
					Properties prop = new Properties(System.getProperties());
					String userName = prop.getProperty("user.name");
					remark += "* @author " + userName + "\r";
					// 附加时间
					String date = new SimpleDateFormat("yyyy-MM-dd")
							.format(new Date());
					remark += "* @date   " + date + "\r";
					remark += "*/";
					remark += "\r";
					t = remark + t;
				} else { // 方法注释
					if (t.indexOf("{") < 0) { // 没找到{
						temp_line += t;
					} else { // 找到了{
						t = temp_line + t + "\r";
						t = getMethodRemark(t) + t;
						temp_line="";
					}
					cont = t.indexOf("{") < 0; // 找不到{ true
				}

			}
			sbf.append((cont ? "" : t) + "\r");

		}
		return sbf.toString();
	}

	/**
	 * 传入一个方法结构进行处理
	 * @param str 方法结构
	 * @return    处理后代码
	 */
	private String getMethodRemark(String str) {
		String space = str.substring(0, str.indexOf("p"));
		StringBuffer result = new StringBuffer(space + "/**\r");
		result.append(space + "* \r");

		// 处理参数
		int start = str.indexOf("(");
		int end = str.indexOf(")");
		String param = "";
		if (end - start - 1 > 0)
			param = str.substring(start + 1, end);
		char[] chars = param.toCharArray();
		String endParam = "";
		boolean isnull = false;
		for (char c : chars) {
			if (c == ' ' && !isnull || c == ',' && !isnull) {
				isnull = true;
				endParam += " ";
			} else if (c != ' ' && c != ',') {
				isnull = false;
				endParam += c;
			}
		}
		String[] endParams = endParam.split(" ");
		if (endParams.length >= 2)
			for (int i = 0; i < endParams.length; i += 2) {
				result.append(space + "* @param  " + endParams[i + 1] + "\r");
			}
		// 处理返回值
		if (str.indexOf("void") == -1) {
			result.append(space + "* @return \r");
		}
		// 处理异常
		if (str.indexOf("throws") >= 0) {
			int t_start = str.indexOf("throws");
			int t_end = str.indexOf("{");
			String exception = str.substring(t_start + 6, t_end);
			exception = exception.replace(" ", "");
			String[] exc_strs = exception.split(",");
			for (String s : exc_strs) {
				result.append(space + "* @throws " + s.trim() + "\r");
			}
		}
		// 附加注释
		Properties prop = new Properties(System.getProperties());
		String userName = prop.getProperty("user.name");
		result.append(space + "* @author " + userName + "\r");
		// 附加时间
		String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		result.append(space + "* @date   " + date + "\r");
		result.append(space + "*/\r");
		return result.toString();
	}

	/**
	 * 将处理后的代码写入原文件中
	 * @param file  文件对象
	 * @param str   文件内容
	 */
	public void writeFile(File file, String str) {
		try {
			FileOutputStream out = new FileOutputStream(file);
			PrintStream p = new PrintStream(out);
			p.print(str);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 读取文件内容
	 * @param file  文件对象
	 * @return      文件内容
	 * @throws IOException
	 */
	public String readTXTFile(File file) throws IOException {
		FileInputStream input = new FileInputStream(file);
		byte[] by = new byte[1024];
		StringBuffer sbf = new StringBuffer();
		while (input.read(by) != -1) {
			sbf.append(new String(by));
			by = new byte[1024];
		}
		return sbf.toString();
	}
}
最近下载更多
text1111  LV1 2022年1月6日
_M*  LV11 2021年5月12日
ckkong  LV1 2020年10月27日
kukugao  LV8 2017年7月26日
xiamo  LV4 2015年4月21日
AXIN  LV36 2014年2月27日
最近浏览更多
3334004690  LV3 2023年11月1日
姜广坤  LV14 2022年11月9日
981352576  LV4 2022年5月11日
Jack261108  LV2 2022年5月6日
text1111  LV1 2022年1月6日
_M*  LV11 2021年5月12日
十年一剑  LV5 2021年1月12日
javaZheng1  LV6 2021年1月12日
zhangzhigang  LV2 2020年10月29日
ckkong  LV1 2020年10月27日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友