Tuesday, October 20, 2009

Serial Version UID - Netbeans 6.8 M2

Providing a code template is a very simple affair in Netbeans. You can manually create the entry in the options dialog or write a plugin. The plugin affair is quite simple.


  1. Create a new NB Module
  2. Expand the Tree as follows in the projects view
    1. -->Important files-->XML Layer-->-->Editors-->text-->x-java-->CodeTemplates-->Defaults
    2. Right click on the file named org-netbeans-modules-editor-java-codetemplates.xml.
    3. Click Edit
    4. append the following line near the end of the file (before the </codetemplates> tag)  
<codetemplate abbreviation="suid" descriptionId="Generate Serial version uid"><code><![CDATA[private static final long serialVersionUID = 1L; ]]></code></codetemplate>
    • Run the project, 
    • a new instance of netbeans will be started. 
    • Bring up the options dialog. 
    • Click on editor-->Code templates tab
    • Select Java as the language
    • Scroll down the list looking for suid
    Viola, code template ready.

    Another option is to use the code generator

    1. in the module create above right click on the module package and click new
    2. Optionally click other
    3. Select code generator in the Module development
    4. Provide a name
    5. in the mime type enter text/x-java
    6. Open the class that is generated and search for the getDisplayName method and replace the method content as return NbBundle.getMessage(SerialVersionUIDGenerator.class, "SerialVersionUIDGenerator.label");
    7. Now for the meat of the implementation. Search for the invoke method and add the following contents

            Caret caret = textComp.getCaret();
            int start = Math.min(caret.getDot(), caret.getMark());
            StringBuilder builder = new StringBuilder("private static final long serialVersionUID = 1L;");
            Document doc = textComp.getDocument();
            try {
                doc.insertString(start, builder.toString(), null);
            } catch (BadLocationException ex) {

    The plugin is available for download from Kenai or netbeans plugin portal