我现在想做的工作是,对现在已有的代码,进行一些增强修正bug。譬如,传过来的参数,要判断是否为空,
也就是加上这么一句话,
if(testParameter==null){
........
}当然对于一个文件好说,现在是有N多文件。所以,我想采用一种偷懒的方法。问题就是怎么得到这个参数。我查API也没有找到合适的方法,但是我很纳闷的就是,为什么在Eclipse里面,提示的时候,把所有的参数都有呢?而且和我的函数原形一样。
也就是加上这么一句话,
if(testParameter==null){
........
}当然对于一个文件好说,现在是有N多文件。所以,我想采用一种偷懒的方法。问题就是怎么得到这个参数。我查API也没有找到合适的方法,但是我很纳闷的就是,为什么在Eclipse里面,提示的时候,把所有的参数都有呢?而且和我的函数原形一样。
试试这个:
/**
* @author zhangc
*
*一个测试程序,用来扫描文件(java文件),找出所有方法的参数列表
*/
import java.io.*;
import java.util.regex.*;
public class ScanSource {
static void findArgsList(Object targetSrc){
/*
* 正则匹配串基本上是这样子分组情况(A(B(c(d))))
* 串是:(\\w+\\s+\\w+\\s*\\(((\\s*\\w+\\s*(\\[\\])*\\s*\\s+(\\[\\])*\\s*\\w+\\s*(\\[\\])*,?)+)\\)\\s*\\{)
* 比如public static void findArgsList(Object targetSrc,int []a){
* A是匹配整个方法定义行:这里是:static void findArgsList(Object targetSrc,int []a){
* B是匹配匹配参数列表:这里是Object targetSrc,int []a
* C是匹配一个参数,包括类型和类型名称和逗号:这里是Object targetSrc,
* D是匹配数组标识符:这里是[]
* 这个串有点bt,水平有限,只能这样
*
*
*/
Pattern p=Pattern.compile("(\\w+\\s+\\w+\\s*\\(((\\s*\\w+\\s*(\\[\\])*\\s*\\s+(\\[\\])*\\s*\\w+\\s*(\\[\\])*,?)+)\\)\\s*\\{)");
Matcher m=p.matcher((CharSequence)targetSrc);
//locate the all methord defination
while(m.find()){
String methodName=null;
String methodArgName=null;
String strArgs=m.group(2);
System.out.println(strArgs);
}
}
public static String LoadTargetFile(String targetFileName) {
String result = null;
try {
FileInputStream fis = new FileInputStream(targetFileName); //临时分配10000size给byte数组。
byte[] bufReceived = new byte[10000]; int counts = fis.read(bufReceived);
byte[] bufActual = new byte[counts];
System.arraycopy(bufReceived, 0, bufActual, 0, counts);
result = new String(bufActual);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
} public static void main(String[] args) {
String target =LoadTargetFile("src/Test.java");
System.out.println(target);
findArgsList(target);
}
}