大多正式一點的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
8
package java.lang;
9
10
public class Object {
..}

2

3

4

5

6

7

8

9

10


如果我們有個小工具能把我們項目里所有目錄下的Java代碼統一設置頭注釋就好了;比如當版本信息等改變時,只要重新運行一下即可一次性更新。下面我們就來親自寫一個。
思路很簡單:
- 建立一個窗口,用戶可以設置一個目錄、編寫頭注釋信息;
- 查找目錄下所有子文件,如果是Java文件則處理之,如果是目錄則遞歸處理;
- 處理Java文件時,打開后,找到package語句或者第一個import語句,作為注釋的插入點,插入注釋;
- 將增加了頭注釋的文件內容寫回文件。
- 本例中判斷頭注釋插入點的邏輯比較簡單,只是根據package語句或者第一個import語句來判斷注釋插入點,尚不嚴謹(比如原有的頭注釋中可能包含這些關鍵字),僅供參考。
源碼如下:
1
import java.io.*;
2
import java.awt.*;
3
import java.awt.event.*;
4
import javax.swing.*;
5
6
public 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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19


20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

程序運行如下圖:
希望這個小工具對你有點用處。