Most official Java source codes have header comments in the header, including some copyright statements and other information. For example, the JDK source code is generally as follows:
/*
* @ (#) 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 {.....}
If we have a small tool, we can set a uniform header comment for the Java code under all directories in our project. For example, when the version information is changed, we only need to re-run it to update it once. Next we will write one in person.
The idea is simple:
- Create a window where you can set a directory and write header comments;
- Search for all sub-files in the directory. If it is a Java file, it will be processed. If it is a directory, it will be recursively processed;
- When you open a Java file, find the package statement or the first Import Statement, and insert the comment as the comment insertion point;
- Write the content of the file with added header comments back to the file.
In this example, the logic for determining the header comment insertion point is relatively simple, but the comment insertion point is determined based on the package statement or the first Import Statement, not rigorous (for example, the original header comment may contain these keywords), for reference only.
The source code is as follows:
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 actionreceivmed (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 actionreceivmed (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 actionreceivmed (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 ();
}
}
}
}
}
}
I hope this tool is useful to you.