Java反编译代码左侧注释批量清除

清除Java代码中有影响的注释

package com.bonze;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class RemoveContentUtil {

	private static String regex = "\\/\\*[\\p{ASCII}]*?\\*\\/\\s?";

	//反编译后的java文件目录     
	private static String directory = "E:\\workspace\\workspace_gjj_tonghua\\SDS\\src";

	private static String newDirectory = "";

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		start();
	}

	public static void start() throws IOException {
		File folder = new File(directory);
		directory = folder.getCanonicalPath();

		File parentFolder = folder.getParentFile();
		newDirectory = parentFolder.getCanonicalPath() + "\\" + folder.getName()
				+ System.currentTimeMillis() + "\\";
		File[] files = folder.listFiles();
		loopFile(files);
	}

	public static void loopFile(File[] files) throws IOException {
		if (files == null) {
			return;
		}
		for (File f : files) {
			if (f.isFile()) {
				if (f.getName().endsWith(".java")) {
					removeContent(directory, newDirectory, f, true);
				} else {
					removeContent(directory, newDirectory, f, false);
				}
			} else {
				loopFile(f.listFiles());
			}
		}
	}

	public static void removeContent(String oldDir, String newDir, File file, boolean remove) throws IOException {
		List<String> lines = FileUtils.readLines(file);
		String relocationString = relocationLineNumber(lines);
		String newContent = remove ? relocationString.replaceAll(regex, "") : relocationString;
		File newFile = new File(file.getCanonicalPath().replace(oldDir, newDir));
		FileUtils.writeStringToFile(newFile, newContent);
	}

	public static String relocationLineNumber(List<String> lines) {
		int currentLine = 0;
		StringBuilder content = new StringBuilder();
		for (String line : lines) {
			currentLine++;
			int num = readLineNumber(line);
			if (num != -1) {
				while (currentLine < num) {
					currentLine++;
					content.append(IOUtils.LINE_SEPARATOR);
				}
			}
			content.append(line).append(IOUtils.LINE_SEPARATOR);
		}
		//IOUtils.LINE_SEPARATOR;         
		return content.toString();
	}

	private static int readLineNumber(String line) {
		int start = line.indexOf("/*");
		int end = line.indexOf("*/");
		if (start > -1 && end > start) {
			//check is annotation             
			String left = line.substring(end + 2).trim();
			if (left.startsWith("@")) {
				return -1;
			}
			String linNum = line.substring(start + 2, end).trim();
			try {
				return Integer.parseInt(linNum);
			} catch (NumberFormatException e) {
				return -1;
			}
		}
		return -1;
	}
}

复制代码

转载于:https://juejin.im/post/5a7f996b6fb9a0633229a413

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值