Automate Java Class and Method Javadoc with IntelliJ Live Templates
This guide shows how to configure IntelliJ IDEA to automatically generate class and method Javadoc comments using File and Code Templates for classes and Live Templates for methods, including custom Groovy scripts for @param and @return annotations.
Class Comment Template
Open Settings → Editor → File and Code Templates , select the Class file template, and add the Javadoc header shown below. The template inserts the author name and the current date automatically when a new class is created.
/**
* @author jitwxs
* @date ${YEAR}年${MONTH}月${DAY}日 ${TIME}
*/Method Comment Template
Use Live Templates to generate method Javadoc. Create a new template group (e.g., userDefine) and add a new live template with abbreviation *. Set the description and template text, then define the applicable context as Java.
Map template variables to IDEA expressions. For @param generation, use a custom Groovy script that builds the annotation list from the method parameters. For @return, another Groovy script adds the annotation only when the method has a non‑void return type.
groovyScript("def result = '';def params = \"${_1}\".replaceAll('[\\[|\\]|\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {if(params[i] != '')result+='* @param ' + params[i] + ((i < params.size() - 1) ? '
' : '')}; return result == '' ? null : '
' + result", methodParameters()) groovyScript("return \"${_1}\" == 'void' ? null : '
* @return ' + \"${_1}\"", methodReturnType())Verification
After saving the settings, creating a new class automatically includes the class Javadoc header, and invoking the live template (type * and press Enter ) inserts a fully populated method comment with @author, @date, @param, and @return annotations as appropriate.
Q & A
Why must the abbreviation be *? Because IDEA expands a template when the abbreviation followed by the Enter key is typed.
Why is there an empty * line in the template? It reserves space for a method description; you can delete it if not needed.
Why are $time$ and $param$ placed together? The custom param script only works when the placeholder is on the same line.
Why implement a custom return handling instead of using methodReturnType()? The built‑in function returns void for methods without a return value, which is not useful for Javadoc.
Why is $return$ not on a separate line? Placing it on the same line avoids backspace handling issues when the return type is absent.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
