【java小工具】从.java文件中筛选出方法,并计算出方法在这些文件中出现的次数

  1 package getMethod;
  2 import java.io.*;
  3 import java.util.*;
  4 import java.util.regex.Matcher;
  5 import java.util.regex.Pattern;
  6 
  7 
  8 public class GM{
  9     
 10     public static void main(String[] args) throws Exception{
 11         //获取目录下的文件
 12         String dir="F:\project-cif";
 13         File root=new File(dir);
 14         //File[] files = root.listFiles();
 15         List<String> funs=new ArrayList<String>();
 16         //递归获取文件夹下所有的。java文件
 17         File[] files =new File[1000] ;
 18         List<File> allFiles=null;
 19         Map<String,Integer> map=new HashMap<String,Integer>();
 20         try {
 21          allFiles=showAllFiles(root);
 22             }
 23         catch (Exception e) {
 24             // TODO Auto-generated catch block
 25             e.printStackTrace();
 26         }
 27         
 28         for(int i=0;i<allFiles.size();i++){
 29             //System.out.println(allFiles.get(i));
 30             files[i]=allFiles.get(i);
 31         }
 32         for(File file:files){
 33         
 34         
 35         if(file!= null){
 36             //System.out.println(file+"文件中的方法如下:");
 37             //funs.add(file.getAbsolutePath()+"文件中的方法如下:");
 38         //创建用于读取的类
 39             //File a=new File(file.getAbsolutePath());
 40             //FileInputStream b=new FileInputStream(a);
 41         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file.getAbsolutePath()))));
 42         
 43         
 44         
 45         //对文件中每行进行遍历
 46         for (String line = br.readLine(); line != null; line = br.readLine()) {  
 47             
 48             //对每行进行正则匹配
 49             Pattern reg=Pattern.compile("^public[\s\w]+[^(class)]");
 50             //对line进行去空格处理trim()
 51             Matcher m=reg.matcher(line.trim());
 52             if(m.find()){
 53                 //1.截取字符串
 54                 
 55                 String methodName=getMethodName(line);
 56                 
 57                 
 58                 //对字符串进行处理
 59                 Pattern reg1=Pattern.compile("void");
 60                 Pattern reg2=Pattern.compile("\(");
 61                 Matcher n1=reg1.matcher(line.trim());
 62                 Matcher n2=reg2.matcher(line.trim());
 63                 if(n1.find()){
 64             //System.out.println(line.trim().substring(0,line.trim().length())+"");
 65            // funs.add(line.trim().substring(0,line.trim().length())+"");
 66                     map.put(line.trim().substring(0,line.trim().length()-1), 0);
 67                 }
 68                 else if(n2.find()){
 69                     //System.out.println(line.trim().substring(0,line.trim().length())+"");
 70                     //funs.add(line.trim().substring(0,line.trim().length())+"");
 71                     map.put(line.trim().substring(0,line.trim().length()-1), 0);
 72                 }
 73            
 74                 //将找到的存到funs
 75                 //funs.add(line);
 76                 
 77                 
 78                 
 79                 //此处将截取方法名字段放入map中
 80                 
 81                 
 82                 
 83                 //2.将method,total放入map
 84                  
 85                 
 86                 
 87                 }
 88         }
 89         
 90         
 91         
 92         
 93         br.close(); 
 94         
 95         }    //String line=file.readLine();
 96         }
 97         for(File file:files){
 98             if(file!= null){
 99                 
100             
101             
102             //对文件中每行进行遍历
103             for(Map.Entry<String, Integer> entry:map.entrySet()){
104                 BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file.getAbsolutePath()))));
105                 
106                 
107                     //System.out.println(entry.getValue()+"keyxxxxxxx"+entry.getKey());
108                     
109                     //System.out.println(br.readLine()+"");
110                     //System.out.println(br1.readLine()+"");
111                     
112                     for (String line1 = br1.readLine(); line1 != null; line1 = br1.readLine()) { 
113                             //Pattern reg3=Pattern.compile(entry.getKey().substring(0,entry.getKey().length()-1)+"\(");
114                         //Matcher n3=reg3.matcher(line1.trim());
115                             if(line1.contains(getMethodName(entry.getKey()))){
116                                 map.put(entry.getKey(),entry.getValue()+1);
117                                 //System.out.println("xxxxxxxxxxxxx");
118                             }
119                         //System.out.println(entry.getValue()+"keyyyyyyyyyyyyyy"+entry.getKey()+entry.getKey().substring(0,entry.getKey().length()-1));
120                         }
121                 
122                     br1.close();    
123                         
124                     
125                     }
126                 
127             
128             }
129             
130         }
131         for(Map.Entry<String, Integer> entry:map.entrySet()){
132             System.out.println("方法为"+entry.getKey()+"出现次数为"+entry.getValue());
133             
134             }
135          File funsfilecsv=new File("e:\read\funs-cif.csv");
136          if(!funsfilecsv.exists()){
137              funsfilecsv.createNewFile();
138          }
139          FileWriter fileWritter = new FileWriter(funsfilecsv.getAbsolutePath());
140          BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
141          /*for(int i1=0;i1<funs.size();i1++){
142              //System.out.println(funs.get(i1));
143          bufferWritter.write("""+funs.get(i1).toString()+"""+"
");
144          }*/
145          for(Map.Entry<String, Integer> entry:map.entrySet()){
146              bufferWritter.write("""+entry.getKey()+"""+","+entry.getValue()+"
");
147          }
148          bufferWritter.close();
149         }
150     public static List<File> resultfs=new ArrayList<File>();
151     public static  List<File>  showAllFiles(File dir) throws Exception{
152           File[] fs = dir.listFiles();
153          
154            for(int i=0; i<fs.length; i++){
155            //System.out.println(fs[i].getAbsolutePath());
156            if(fs[i].isDirectory()){
157             try{
158              showAllFiles(fs[i]);
159             }catch(Exception e){}
160            }else if(fs[i].getName().endsWith(".java")){
161                resultfs.add(fs[i]);
162                
163            }
164           }
165           //Object[] files=resultfs.toArray();
166          // System.out.println(files);
167            //res=resultfs;
168            
169           /*for(int i=0;i<resultfs.size();i++){
170               System.out.println(resultfs.get(i));
171           }*/
172            return resultfs;
173            
174           
175          }
176     public static String getMethodName(String str){
177         String[] s=str.trim().split(" ");
178         String result="";
179         for(String a:s){
180         //System.out.println(a);
181         Pattern reg=Pattern.compile("\(");
182         Matcher b=reg.matcher(a);
183         if(b.find()){
184             //System.out.println(a);
185             String[] s1=a.trim().split("\(");
186             //System.out.println(s1[0]+"(");
187             result= s1[0]+"(";
188             
189         }
190         }return result;
191         }
192 }
原文地址:https://www.cnblogs.com/zipon/p/5250860.html