Automate Java Class and Method Javadoc Generation with IntelliJ Live Templates
This guide shows how to configure IntelliJ IDEA to automatically insert class and method Javadoc comments using file and code templates and live templates, including custom Groovy scripts for @author, @date, @param, and @return annotations.
1. Class Comment Template
Open Settings → Editor → File and Code Templates, select the File tab, then the Class sub‑tab and paste the template shown in the red box.
/**
* @author jitwxs
* @date ${YEAR}年${MONTH}月${DAY}日 ${TIME}
*/Note
The Description field lists all template variables supported by IDEA, such as ${DATE} and ${TIME}. After saving, new classes automatically receive the comment. To apply the same template to interfaces, configure the Interface tab similarly.
2. Method Comment Template
The goal is to generate @param and @return annotations automatically based on method signatures.
In Settings → Editor → Live Templates, click the + button, choose 2. Template Group... and create a group named userDefine:
Then select the newly created group, click + again and choose 1. Live Template:
In the template editor set:
Abbreviation must be * Description can describe the template purpose
Template text contains the Javadoc skeleton (the first line must start with * and there must be no leading /)
Expand with set to
EnterDefine the variables used in the template. IDEA does not recognize $date$, $time$, $param$ and $return$ by default, so we map them with Groovy scripts.
For $param$ use:
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())For $return$ use:
groovyScript("return \"${_1}\" == 'void' ? null : '
* @return ' + \"${_1}\"", methodReturnType())After clicking Edit variables, assign the appropriate expressions (IDEA built‑in functions for date and time, custom Groovy for param and return).
Set the Expression for each variable as shown:
Make sure the Skip if defined option is unchecked, then save the template.
3. Verify the Result
3.1 Class Comment
Creating a new class now automatically inserts the class comment:
3.2 Method Comment
The template works for various method signatures (no parameters, single/multiple parameters, with/without return value). Example output:
4. Q & A
(1) Why must the Abbreviation be * and Expand with be Enter? Because IDEA triggers a template when the abbreviation followed by the expand key is typed; * + Enter produces the opening /** of a Javadoc comment.
(2) Why is there an empty line with * in the template? It reserves a line for a method description; you can delete it if not needed.
(3) Why are $time$$param$ placed together? The custom param script was adjusted to avoid generating an empty @param line when there are no parameters, so $param$ must stay on the same line as $time$ to handle backspace correctly.
(4) Why implement return manually instead of using methodReturnType() ? The built‑in function returns void for methods without a return value, which is not useful; the custom script only emits @return when a real return type exists.
(5) Why is $return$ not on a separate line? When methodReturnType() returns null, a separate line would cause backspace handling issues, so it is merged with the preceding line.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
