Posted on 2010-08-23 13:58
TWaver 阅读(1751)
评论(1) 编辑 收藏
大多正式一点的Java源代码,在头部都设有头注释信息。我们做的软件项目或者产品代码中,一般也需要设置一些公司信息、作者信息、版权信息等内容。现在的NetBeans或Eclipse等IDE工具都能很好的自动生成这些注视。不过如果要对你的整个项目代码进行批量的头注视修改、替换、维护,又该如何呢?本文用Swing做了一个小工具,来解决这个问题。
以下是JDK的源码的头注释的例子。
1/**//*
2 * @(#)Object.java 1.61 03/01/23
3 *
4 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
5 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6 */
7
8package java.lang;
9
10public class Object { ..}
如果我们有个小工具能把我们项目里所有目录下的Java代码统一设置头注释就好了;比如当版本信息等改变时,只要重新运行一下即可一次性更新。下面我们就来亲自写一个。
思路很简单:
- 建立一个窗口,用户可以设置一个目录、编写头注释信息;
- 查找目录下所有子文件,如果是Java文件则处理之,如果是目录则递归处理;
- 处理Java文件时,打开后,找到package语句或者第一个import语句,作为注释的插入点,插入注释;
- 将增加了头注释的文件内容写回文件。
- 本例中判断头注释插入点的逻辑比较简单,只是根据package语句或者第一个import语句来判断注释插入点,尚不严谨(比如原有的头注释中可能包含这些关键字),仅供参考。
源码如下:
1import java.io.*;
2import java.awt.*;
3import java.awt.event.*;
4import javax.swing.*;
5
6public class HeaderCommentsGenerator {
7
8 private static int count = 0;
9
10 public static void main(String[] args) {
11 final JFrame frame = new JFrame("TWaver中文社区之Swing探秘");
12 JPanel contentPane = (JPanel) frame.getContentPane();
13 JPanel centerPane = new JPanel(new BorderLayout(10, 10));
14 centerPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
15 JPanel pathPane = new JPanel(new BorderLayout());
16 final JTextField txtPath = new JTextField();
17 txtPath.setText("Please select your file or path.");
18 pathPane.add(txtPath, BorderLayout.CENTER);
19 JButton btnSelectPath = new JButton("Browser");
20 btnSelectPath.addActionListener(new ActionListener() {
21
22 public void actionPerformed(ActionEvent e) {
23 JFileChooser chooser = new JFileChooser();
24 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
25 int returnVal = chooser.showOpenDialog(frame);
26 if (returnVal == JFileChooser.APPROVE_OPTION) {
27 txtPath.setText(chooser.getSelectedFile().getAbsolutePath());
28 }
29 }
30 });
31 btnSelectPath.setMnemonic('B');
32 pathPane.add(btnSelectPath, BorderLayout.EAST);
33 centerPane.add(pathPane, BorderLayout.NORTH);
34 final JTextArea txtComments = new JTextArea();
35 txtComments.setText("/*\n"
36 + " * Copyright 2003-2004 ABC Software, Inc. All rights reserved.\n"
37 + " */");
38 centerPane.add(new JScrollPane(txtComments), BorderLayout.CENTER);
39
40 contentPane.add(centerPane, BorderLayout.CENTER);
41
42 JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
43 JButton btnOK = new JButton("Generate!");
44 btnOK.addActionListener(new ActionListener() {
45
46 public void actionPerformed(ActionEvent e) {
47 String path = txtPath.getText();
48 File file = new File(path);
49 if (!file.exists()) {
50 JOptionPane.showMessageDialog(frame,
51 "Path '" + path + "' not exist.",
52 "Error",
53 JOptionPane.ERROR_MESSAGE);
54 } else {
55 commentFile(file, txtComments.getText());
56 JOptionPane.showMessageDialog(frame,
57 "Finish, total " + count + " files are processed.",
58 "Information",
59 JOptionPane.INFORMATION_MESSAGE);
60 }
61 }
62 });
63 btnOK.setMnemonic('G');
64 JButton btnClose = new JButton("Close");
65 btnClose.addActionListener(new ActionListener() {
66
67 public void actionPerformed(ActionEvent e) {
68 System.exit(0);
69 }
70 });
71 btnClose.setMnemonic('C');
72 buttonPane.add(btnOK);
73 buttonPane.add(btnClose);
74 contentPane.add(buttonPane, BorderLayout.SOUTH);
75
76 frame.setSize(500, 300);
77 frame.show();
78 }
79
80 private static void commentFile(File file, String comments) {
81 if (file != null && file.exists()) {
82 if (file.isDirectory()) {
83 String[] children = file.list();
84 for (int i = 0; i < children.length; i++) {
85 File child = new File(file.getPath() + System.getProperty("file.separator") + children[i]);
86 commentFile(child, comments);
87 }
88 } else {
89 if (file.getName().toLowerCase().endsWith(".java")) {
90 System.out.println(file.getName());
91 count++;
92 try {
93 RandomAccessFile raFile = new RandomAccessFile(file, "rw");
94 byte[] content = new byte[(int) raFile.length()];
95 raFile.readFully(content);
96 String all = new String(content);
97 all = all.trim();
98 while (all.startsWith("\n")) {
99 all = all.substring(1);
100 }
101 if (all.indexOf("package") != -1) {
102 all = all.substring(all.indexOf("package"));
103 }
104 if (all.indexOf("import") != -1) {
105 all = all.substring(all.indexOf("package"));
106 }
107 all = comments + "\n" + all;
108 raFile.close();
109 FileWriter writer = new FileWriter(file);
110 writer.write(all);
111 writer.close();
112 } catch (Exception ex) {
113 ex.printStackTrace();
114 }
115 }
116 }
117 }
118 }
119}
程序运行如下图:
希望这个小工具对你有点用处。