package getMethod;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class GM{ public static void main(String[] args) throws Exception{
//获取目录下的文件
String dir="F:\\project-cif";
File root=new File(dir);
//File[] files = root.listFiles();
List<String> funs=new ArrayList<String>();
//递归获取文件夹下所有的。java文件
File[] files =new File[1000] ;
List<File> allFiles=null;
Map<String,Integer> map=new HashMap<String,Integer>();
try {
allFiles=showAllFiles(root);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} for(int i=0;i<allFiles.size();i++){
//System.out.println(allFiles.get(i));
files[i]=allFiles.get(i);
}
for(File file:files){ if(file!= null){
//System.out.println(file+"文件中的方法如下:");
//funs.add(file.getAbsolutePath()+"文件中的方法如下:");
//创建用于读取的类
//File a=new File(file.getAbsolutePath());
//FileInputStream b=new FileInputStream(a);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file.getAbsolutePath())))); //对文件中每行进行遍历
for (String line = br.readLine(); line != null; line = br.readLine()) { //对每行进行正则匹配
Pattern reg=Pattern.compile("^public[\\s\\w]+[^(class)]");
//对line进行去空格处理trim()
Matcher m=reg.matcher(line.trim());
if(m.find()){
//1.截取字符串 String methodName=getMethodName(line); //对字符串进行处理
Pattern reg1=Pattern.compile("void");
Pattern reg2=Pattern.compile("\\(");
Matcher n1=reg1.matcher(line.trim());
Matcher n2=reg2.matcher(line.trim());
if(n1.find()){
//System.out.println(line.trim().substring(0,line.trim().length())+"");
// funs.add(line.trim().substring(0,line.trim().length())+"");
map.put(line.trim().substring(0,line.trim().length()-1), 0);
}
else if(n2.find()){
//System.out.println(line.trim().substring(0,line.trim().length())+"");
//funs.add(line.trim().substring(0,line.trim().length())+"");
map.put(line.trim().substring(0,line.trim().length()-1), 0);
} //将找到的存到funs
//funs.add(line); //此处将截取方法名字段放入map中 //2.将method,total放入map }
} br.close(); } //String line=file.readLine();
}
for(File file:files){
if(file!= null){ //对文件中每行进行遍历
for(Map.Entry<String, Integer> entry:map.entrySet()){
BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file.getAbsolutePath())))); //System.out.println(entry.getValue()+"keyxxxxxxx"+entry.getKey()); //System.out.println(br.readLine()+"");
//System.out.println(br1.readLine()+""); for (String line1 = br1.readLine(); line1 != null; line1 = br1.readLine()) {
//Pattern reg3=Pattern.compile(entry.getKey().substring(0,entry.getKey().length()-1)+"\\(");
//Matcher n3=reg3.matcher(line1.trim());
if(line1.contains(getMethodName(entry.getKey()))){
map.put(entry.getKey(),entry.getValue()+1);
//System.out.println("xxxxxxxxxxxxx");
}
//System.out.println(entry.getValue()+"keyyyyyyyyyyyyyy"+entry.getKey()+entry.getKey().substring(0,entry.getKey().length()-1));
} br1.close(); } } }
for(Map.Entry<String, Integer> entry:map.entrySet()){
System.out.println("方法为"+entry.getKey()+"出现次数为"+entry.getValue()); }
File funsfilecsv=new File("e:\\read\\funs-cif.csv");
if(!funsfilecsv.exists()){
funsfilecsv.createNewFile();
}
FileWriter fileWritter = new FileWriter(funsfilecsv.getAbsolutePath());
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
/*for(int i1=0;i1<funs.size();i1++){
//System.out.println(funs.get(i1));
bufferWritter.write("\""+funs.get(i1).toString()+"\""+"\n");
}*/
for(Map.Entry<String, Integer> entry:map.entrySet()){
bufferWritter.write("\""+entry.getKey()+"\""+","+entry.getValue()+"\n");
}
bufferWritter.close();
}
public static List<File> resultfs=new ArrayList<File>();
public static List<File> showAllFiles(File dir) throws Exception{
File[] fs = dir.listFiles(); for(int i=0; i<fs.length; i++){
//System.out.println(fs[i].getAbsolutePath());
if(fs[i].isDirectory()){
try{
showAllFiles(fs[i]);
}catch(Exception e){}
}else if(fs[i].getName().endsWith(".java")){
resultfs.add(fs[i]); }
}
//Object[] files=resultfs.toArray();
// System.out.println(files);
//res=resultfs; /*for(int i=0;i<resultfs.size();i++){
System.out.println(resultfs.get(i));
}*/
return resultfs; }
public static String getMethodName(String str){
String[] s=str.trim().split(" ");
String result="";
for(String a:s){
//System.out.println(a);
Pattern reg=Pattern.compile("\\(");
Matcher b=reg.matcher(a);
if(b.find()){
//System.out.println(a);
String[] s1=a.trim().split("\\(");
//System.out.println(s1[0]+"(");
result= s1[0]+"("; }
}return result;
}
}
04-25 08:38