Set the header comment for your Java code batch

xiaoxiao2021-03-06  41

Most official Java source code, with head annotation information in the head, including some copyright statements and other information. For example, the source code of JDK 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 gadget to set the header annotation in all the Java code in all directories in our project; Let's write one person.

The idea is very simple:

Create a window, the user can set a directory, write the header information; find all the subfiles in the directory, if it is a Java file, if it is recursive; when processed the Java file, open, find the package statement or A Import statement, an insertion point, inserts an comment; will add the file content of the header to write back the file.

In this example, the logic of judging the header insertion point is simple, just determines the annotation insertion point according to the Package statement or the first IMPORT statement, is not rigorous (such as 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 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 ()) {JOPANE.SHOWMESSAGEDIALOG (Frame, "Path '" Path "NOT EXIST.", "Error", JOPTIONPANE.ERROR_MESSAGE (File, Txtcomments.getText ()); JOPANE.SHOWMESSAGEDIALOG (Files, "Finish, Total" Count "Files Are Processed.", "Information", JOptionPan e.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.Indirectory ()) {string [] children = file.list (); for (int i) = 0; i

Writer.close ();} catch (exception ex) {ex.printstacktrace ();}}}}}} I hope this gadget is a bit used.

转载请注明原文地址:https://www.9cbs.com/read-116324.html

New Post(0)