Java读取VB代码,分析函数调用关系

import java.util.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class SubFunctionSummary {

	private static String[] fileFilter = new String[] { ".bas", ".frm", ".cls" };
	private static String[] function = new String[] { " Function ", "End Function", "Fun_" };
	private static String[] sub = new String[] { " Sub ", "End Sub" , "Sub_" };
	// 不是以Sub_ 或者 Fun_开头的Sub 或者 Function
	private static String[] specialSubFunction = new String[] {
			"WriteIniString", "GetIniString" };
	
	public static void main(String[] args) {

		String path = "C:/Documents and Settings/Evan/桌面/Down.LieHuo.Net";///frmBD_ASL.frm";
		process(path);
	}

	public static void process(String path) {

		File directory = new File(path);
		if (directory.isDirectory()) {
			for (int i = 0; i < directory.listFiles().length; i++) {
				process2(directory.listFiles()[i]);
			}
		} else if (directory.isFile()) {
			process2(directory);
		}
	}

	public static void process2(File file) {

		String fileName = file.getName();
		if (fileName.endsWith(fileFilter[0]) || fileName.endsWith(fileFilter[1])
				|| fileName.endsWith(fileFilter[2])) {

			process3(file);
		} else {
			return;
		}
	}

	public static void process3(File file) {

		Map<String, List<String>> subFunctionMap = new HashMap<String, List<String>>();
		List<String> subFunctionList = null;
		BufferedReader reader = null;
		String srcLine = "";
		String subFunctionName = null;

		try {
			reader = new BufferedReader(new FileReader(file));
			while (srcLine != null) {

				int iFunction = srcLine.indexOf(function[0]);
				int iSub = srcLine.indexOf(sub[0]);
				int iStart = 0;
				int iEnd = 0;

				// Function 或者 Sub 定义开始行
				if (iFunction > 0 || iSub > 0) {

					if (iFunction > 0) {
						// 非注释行
						if (srcLine.substring(0, iFunction).indexOf("'") <= 0) {
							iStart = iFunction + function[0].length();
							iEnd = srcLine.indexOf("(");
						}
					} else {
						// 非注释行
						if (srcLine.substring(0, iSub).indexOf("'") <= 0) {
							iStart = iSub + sub[0].length();
							iEnd = srcLine.indexOf("(");
						}
					}
					if (iStart > 0 && iEnd > iStart) {
						subFunctionName = srcLine.substring(iStart, iEnd);
						subFunctionList = new ArrayList<String>();
					}
				} else {
					// Function 或者 Sub 定义结束行
					if (srcLine.startsWith(function[1])
							|| srcLine.startsWith(sub[1])) {
						if (subFunctionName != null && subFunctionList != null) {
							subFunctionMap.put(subFunctionName, subFunctionList);
						}
					} else {
						process4(srcLine, subFunctionList, subFunctionName);
					}
				}

				srcLine = reader.readLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		output("C:/Documents and Settings/Evan/桌面", "SubFunction.csv", subFunctionMap, file.getName());
	}

	public static void process4(String srcLine, List<String> subFunctionList, String subFunctionName) {
		
		// 被调用的Sub 或者Function的名字
		String subSubFunctionName = null;
		
		int iFunction = srcLine.indexOf(function[2]);
		int iSub = srcLine.indexOf(sub[2]);
		int iStart = 0;
		int iEnd = 0;
		
		// 包含Fun_ 或者 Sub_ 的行
		if (iFunction > 0 || iSub > 0) {

			if (iFunction > 0) {
				// 非注释行
				if (srcLine.substring(0, iFunction).indexOf("'") <= 0) {
					iStart = iFunction;
					iEnd = srcLine.substring(iStart).indexOf(" ");
				}
			} else {
				// 非注释行
				if (srcLine.substring(0, iSub).indexOf("'") <= 0) {
					iStart = iSub + sub[0].length();
					iEnd = srcLine.substring(iStart).indexOf(" ");
				}
			}
			if (iStart > 0 && iEnd > iStart) {
				subSubFunctionName = srcLine.substring(iStart, iEnd);
				// 不是在Functiong赋值的语句
				if (!subFunctionName.equals(subSubFunctionName) && !subFunctionList.contains(subSubFunctionName)) {
					subFunctionList.add(subSubFunctionName);
				}
			}
		} else {
			for (int i = 0; i < specialSubFunction.length; i++) {
				if (srcLine.indexOf(specialSubFunction[i]) >= 0) {
					subSubFunctionName = specialSubFunction[i];
					// 不是在Functiong赋值的语句
					if (!subFunctionName.equals(subSubFunctionName) && !subFunctionList.contains(subSubFunctionName)) {
						subFunctionList.add(subSubFunctionName);
					}
				}
			}
		}
	}
	
	public static void output(String path, String fileName, Map<String, List<String>> subFunctionMap, String srcName) {
		
		File file = new File(path + "/" + fileName);
		BufferedWriter writer = null;

		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			writer = new BufferedWriter(new FileWriter(file, true));
			writer.write(srcName + ",,\n");

			for (Iterator<Entry<String, List<String>>> iterator = subFunctionMap.entrySet().iterator(); iterator.hasNext();) {

				Entry<String, List<String>> subFunctionEntry = iterator.next();
				String subFunctionName = subFunctionEntry.getKey();
				List<String> subFunctionList = subFunctionEntry.getValue();

				if (subFunctionList != null && !subFunctionList.isEmpty()) {
					for (int i = 0; i < subFunctionList.size(); i++) {
						if (i == 0) {
							writer.write("," + subFunctionName + "," + subFunctionList.get(i) + "\n");
						} else {
							writer.write(",," + subFunctionList.get(i));
						}
					}
				} else {
					writer.write("," + subFunctionName + ",\n");
				}
			}

			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值