码迷,mamicode.com
首页 > 其他好文 > 详细

代码统计量工具

时间:2015-07-25 00:09:29      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

Java GUI编写的统计代码量小工具,输入工程路径名即可统计,支持Java、C++、C。

源码及可执行文件见:https://github.com/openluopworld/CodeLineCounter。

技术分享
  1 package com.luop.codelines;
  2 
  3 import java.awt.BorderLayout;
  4 import java.awt.Font;
  5 import java.awt.GridLayout;
  6 import java.awt.event.ActionEvent;
  7 import java.awt.event.ActionListener;
  8 import java.io.BufferedReader;
  9 import java.io.File;
 10 import java.io.FileReader;
 11 import java.io.IOException;
 12 import java.util.ArrayList;
 13 import java.util.List;
 14 
 15 import javax.swing.JButton;
 16 import javax.swing.JComboBox;
 17 import javax.swing.JFrame;
 18 import javax.swing.JLabel;
 19 import javax.swing.JPanel;
 20 import javax.swing.JTextArea;
 21 import javax.swing.JTextField;
 22 
 23 /**
 24  * Count the lines of a project
 25  * @author LuoPeng
 26  * @time 2015.7.9
 27  *
 28  */
 29 public class Interface extends JFrame {
 30     
 31     /**
 32      * 
 33      */
 34     private static final long serialVersionUID = 9113566755801573057L;
 35     
 36     // the absolute paths of all the source files
 37     private List<String> names = new ArrayList<String>();
 38     // the language of the user‘s project
 39     private String language = null;
 40     
 41     // the code language
 42     private String[] languages = new String[]{"Java", "C++", "C"};
 43     // panel about the file info
 44     private JPanel panelFileInfo = null;
 45     // panel show the result
 46     private JPanel panelResult = null, panelResultLines = null, panelResultDetail = null;
 47     // pane about the soft info
 48     private JPanel panelSoftInfo = null;
 49     // choose the language
 50     private JComboBox<String> combox = null;
 51     // input the file path
 52     private JTextField filePath = null;
 53     // count button
 54     private JButton countBu = null;
 55     private JLabel lbResult = null;
 56     // show the result lines
 57     private JTextField tfResult = null;
 58     // show the detail lines of each file
 59     private JTextArea taComputeProcess = null;
 60     // software info
 61     private JLabel lbReadme1, lbReadme2, lbReadme3, lbReadme4 = null;
 62     
 63     // font size 18
 64     private int fontSizeEighteen = 18;
 65     // font size 12
 66     private int fontSizeTwelve = 12;
 67     // font name
 68     private String fontName = "楷体";
 69     
 70     /**
 71      * create the layout, the component are as follows
 72      * -------------------------------
 73      * |  combox  filePath  countBu  |
 74      * -------------------------------
 75      * |    lbResult    tfResult     |
 76      * |                             |
 77      * |                             |
 78      * |      taComputeProcess       |
 79      * |                             |
 80      * |                             |
 81      * -------------------------------
 82      * |         lbReadme1           |
 83      * |         lbReadme2           |
 84      * |         lbReadme3           |
 85      * |         lbReadme4           |
 86      * -------------------------------
 87      */
 88     public Interface() {
 89         /*
 90          * add the file info component
 91          */
 92         panelFileInfo = new JPanel();
 93         panelFileInfo.setLayout(new GridLayout(1, 3, 20, 20));
 94         combox = new JComboBox<String>(languages);
 95         panelFileInfo.add(combox);
 96         filePath = new JTextField();
 97         filePath.setFont(new Font(fontName, Font.PLAIN, fontSizeEighteen));
 98         panelFileInfo.add(filePath);
 99         countBu = new JButton("计算");
100         countBu.setFont(new Font(fontName, Font.PLAIN, fontSizeEighteen));
101         countBu.addActionListener(new ActionListener() {
102             public void actionPerformed(ActionEvent e) {
103                 String path = filePath.getText();
104                 language = combox.getSelectedItem().toString();
105                 int lines = codeLinesCounter(path);
106                 tfResult.setText("" + lines);
107             }
108         });
109         panelFileInfo.add(countBu);
110         this.add(panelFileInfo, BorderLayout.NORTH);
111         
112         /*
113          * add the result component 
114          */
115         // total lines
116         panelResult = new JPanel();
117         panelResult.setLayout(new BorderLayout());
118         panelResultLines = new JPanel();
119         panelResultLines.setLayout(new GridLayout(1, 2, 20, 20));
120         lbResult = new JLabel("代码总量:");
121         lbResult.setFont(new Font(fontName, Font.PLAIN, fontSizeEighteen));
122         panelResultLines.add(lbResult);
123         tfResult = new JTextField();
124         tfResult.setFont(new Font(fontName, Font.BOLD, fontSizeEighteen));
125         tfResult.setEditable(false);
126         panelResultLines.add(tfResult);
127         panelResult.add(panelResultLines, BorderLayout.NORTH);
128         // detail lines of each file
129         panelResultDetail = new JPanel();
130         panelResultDetail.setLayout(new GridLayout(1, 1, 20, 20));
131         taComputeProcess = new JTextArea();
132         taComputeProcess.setEditable(false);
133         taComputeProcess.setFont(new Font(fontName, Font.PLAIN, fontSizeTwelve));
134         taComputeProcess.setLineWrap(true); // change line automaticlly
135         panelResultDetail.add(taComputeProcess);
136         panelResult.add(panelResultDetail, BorderLayout.CENTER);
137         this.add(panelResult, BorderLayout.CENTER);
138         
139         /*
140          * add the software info component
141          */
142         panelSoftInfo = new JPanel();
143         panelSoftInfo.setLayout(new GridLayout(4, 1));
144         lbReadme1 = new JLabel("README:输入文件路径,点击计算按钮开始统计");
145         lbReadme1.setFont(new Font(fontName, Font.PLAIN, fontSizeEighteen));
146         panelSoftInfo.add(lbReadme1);
147         lbReadme2 = new JLabel();
148         panelSoftInfo.add(lbReadme2);
149         lbReadme3 = new JLabel("[作者:wuhanluop@163.com]");
150         lbReadme3.setFont(new Font(fontName, Font.PLAIN, fontSizeEighteen));
151         panelSoftInfo.add(lbReadme3);
152         lbReadme4 = new JLabel("[博客:http://blog.com.cn/luopeng]");
153         lbReadme4.setFont(new Font(fontName, Font.PLAIN, fontSizeEighteen));
154         panelSoftInfo.add(lbReadme4);
155         this.add(panelSoftInfo, BorderLayout.SOUTH);
156         
157         /*
158          * the Frame info
159          */
160         this.setTitle("代码统计工具");
161         this.setLocation(0, 0);
162         this.setSize(400, 400);
163         this.setVisible(true);
164         this.setResizable(false);
165         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
166     
167     }
168 
169     public static void main( String[] args ) {
170         new Interface();
171     }
172     
173     /**
174      * count the lines of a project
175      * 
176      * @param path the path of the project
177      * @return code lines, or -1 if the path is not a directory
178      */
179     private int codeLinesCounter(String path) {
180         
181         if ( names.size() != 0) { // clear the possible file paths of last result
182             names.clear();
183         }
184         taComputeProcess.setText(""); // clear the possible info of last result
185         
186         int number = 0;
187         File file = new File(path);
188         if ( !file.isDirectory()) {
189             return -1;
190         }
191         
192         // get all the file names
193         if ( "Java".equals(language) ) {
194             getFileNames(path, ".java");
195         } else if ("C++".equals(language)) {
196             getFileNames(path, ".cpp");
197             getFileNames(path, ".h");
198         } else if ("C".equals(language)) {
199             getFileNames(path, ".c");
200             getFileNames(path, ".h");
201         }
202         
203         // count the lines of each file
204         int size = names.size();
205         int tempNumber = 0;
206         try {
207             for ( int i = 0; i < size; i++) {
208                 tempNumber = countLines(names.get(i));
209                 number += tempNumber;
210                 if ( null == taComputeProcess.getText() ||
211                         "".equals(taComputeProcess.getText())) {
212                     taComputeProcess.setText(names.get(i) + "————" + tempNumber + " lines.");
213                 } else {
214                     taComputeProcess.setText(taComputeProcess.getText() + "\n" +
215                             names.get(i) + "————" + tempNumber + " lines.");
216                 }
217             }
218         } catch (IOException e) {
219             e.printStackTrace();
220         }
221         
222         return number;
223         
224     }
225     
226     /**
227      * Get all the file names of the direct children of directory ‘path‘
228      * 
229      * Root
230      *   -Directory1
231      *     -nihao.java
232      *     -beijing.txt
233      *   -Hello.java
234      *   -123.txt
235      *   
236      * if the path is ‘Root‘, the result will only contain ‘Root//Hello.java‘ and ‘Root//123.txt‘
237      *  
238      * @param path the file path of a project
239      * @param language the coding language of the project
240      * @return
241      */
242     private void getFileNames(String path, String language) {
243         
244         String[] files = new File(path).list();
245         int length = files.length;
246         for ( int i = 0; i < length; i++ ) {
247             // files[i] is the name of a file or directory
248             if ( files[i].endsWith(language)) {
249                 names.add( path + "//" + files[i]);
250             } else if ( new File(path + "//" + files[i]).isDirectory()) {
251                 // recursion
252                 getFileNames(path + "//" + files[i], language);
253             }
254         }
255         
256     }
257     
258     /**
259      * count the lines of a source file
260      * 
261      * @param filePath filePath the path of the file
262      * @return lines of the file
263      * @throws IOException
264      */
265     private int countLines ( String filePath) throws IOException {
266         int lines = 0; 
267         BufferedReader br = null;
268         String line = null;
269         
270         br = new BufferedReader(new FileReader(new File(filePath)));
271         line = br.readLine();
272         while (line != null) {
273             lines++;
274             line = br.readLine();
275         }
276         
277         br.close();
278         return lines;
279     }
280     
281 }
View Code

代码统计量工具

标签:

原文地址:http://www.cnblogs.com/luop/p/4675035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!