大多正式一點的Java原始碼,在頭部都設有頭注釋資訊,其中包含一些著作權聲明等資訊。例如JDK的源碼一般如下:
/*
* @(#)Object.java 1.61 03/01/23
*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang;
public class Object { .....}
如果我們有個小工具能把我們項目裡所有目錄下的Java代碼統一設定頭注釋就好了;比如當版本資訊等改變時,只要重新運行一下即可一次性更新。下面我們就來親自寫一個。
思路很簡單:
- 建立一個視窗,使用者可以設定一個目錄、編寫頭注釋資訊;
- 尋找目錄下所有子檔案,如果是Java檔案則處理之,如果是目錄則遞迴處理;
- 處理Java檔案時,開啟後,找到package語句或者第一個import語句,作為注釋的插入點,插入注釋;
- 將增加了頭注釋的檔案內容寫迴文件。
本例中判斷頭注釋插入點的邏輯比較簡單,只是根據package語句或者第一個import語句來判斷注釋插入點,尚不嚴謹(比如原有的頭注釋中可能包含這些關鍵字),僅供參考。
源碼如下:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HeaderCommentsGenerator {
private static int count = 0;
public static void main(String[] args) {
final JFrame frame = new JFrame("HeaderCommentsGenerator 1.0 [xiaozhonghua@hotmail.com]");
JPanel contentPane = (JPanel) frame.getContentPane();
JPanel centerPane = new JPanel(new BorderLayout(10, 10));
centerPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
JPanel pathPane = new JPanel(new BorderLayout());
final JTextField txtPath = new JTextField();
txtPath.setText("Please select your file or path.");
pathPane.add(txtPath, BorderLayout.CENTER);
JButton btnSelectPath = new JButton("Browser...");
btnSelectPath.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = chooser.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
txtPath.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
});
btnSelectPath.setMnemonic('B');
pathPane.add(btnSelectPath, BorderLayout.EAST);
centerPane.add(pathPane, BorderLayout.NORTH);
final JTextArea txtComments = new JTextArea();
txtComments.setText("/*/n" +
" * Copyright 2003-2004 ABC Software, Inc. All rights reserved./n" +
" */");
centerPane.add(new JScrollPane(txtComments), BorderLayout.CENTER);
contentPane.add(centerPane, BorderLayout.CENTER);
JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
JButton btnOK = new JButton("Generate!");
btnOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String path = txtPath.getText();
File file = new File(path);
if (!file.exists()) {
JOptionPane.showMessageDialog(frame,
"Path '" + path + "' not exist.",
"Error",
JOptionPane.ERROR_MESSAGE);
} else {
commentFile(file, txtComments.getText());
JOptionPane.showMessageDialog(frame,
"Finish, total " + count + " files are processed.",
"Information",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
btnOK.setMnemonic('G');
JButton btnClose = new JButton("Close");
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnClose.setMnemonic('C');
buttonPane.add(btnOK);
buttonPane.add(btnClose);
contentPane.add(buttonPane, BorderLayout.SOUTH);
frame.setSize(500, 300);
frame.show();
}
private static void commentFile(File file, String comments) {
if (file != null && file.exists()) {
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
File child = new File(file.getPath() + System.getProperty("file.separator") + children[i]);
commentFile(child, comments);
}
} else {
if (file.getName().toLowerCase().endsWith(".java")) {
System.out.println(file.getName());
count++;
try {
RandomAccessFile raFile = new RandomAccessFile(file, "rw");
byte[] content = new byte[ (int) raFile.length()];
raFile.readFully(content);
String all = new String(content);
all = all.trim();
while (all.startsWith("/n")) {
all = all.substring(1);
}
if (all.indexOf("package") != -1) {
all = all.substring(all.indexOf("package"));
}
if (all.indexOf("import") != -1) {
all = all.substring(all.indexOf("package"));
}
all = comments + "/n" + all;
raFile.close();
FileWriter writer = new FileWriter(file);
writer.write(all);
writer.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
}
希望這個小工具對你有點用處。