Auto‑Generate Java Class and Method Javadoc with IntelliJ IDEA Templates
This guide shows how to configure IntelliJ IDEA to automatically insert Javadoc class and method comments, including author, date, @param and @return tags, using File and Code Templates and Live Templates with custom Groovy scripts.
1. Class Comment
Open IDEA Settings → Editor → File and Code Templates, select the File tab, then the Class sub‑tab and add the template shown below.
/**
* @author jitwxs
* @date ${YEAR}年${MONTH}月${DAY}日 ${TIME}
*/The template includes author and date; all IDEA template variables are listed in the Description field.
After saving, new classes automatically receive this comment. To apply the same to interfaces, also configure the Interface tab.
2. Method Comment
This tutorial implements automatic generation of @param and @return tags based on the method signature.
Generate @param annotations from method parameters.
Generate @return annotation when the method has a return value.
To add a method comment template, go to Settings → Editor → Live Templates, click the + button, choose 2. Template Group... and create a group (e.g., userDefine).
Enter the group name.
Select the newly created group, click + again and choose 1. Live Template.
Set Abbreviation to *, fill in Description and Template text . Ensure Expand with is set to the Enter key.
Copy the following template text (note the first line has no slash and the leading * is at column 1):
*
*
* @author jitwxs
* @date $date$ $time$$param$ $return$
*/Since no applicable contexts are set yet, click Define and check Java to apply the template to all Java files.
In the template text, placeholders such as $date$, $time$, $param$, and $return$ need expressions. Click Edit variables and assign expressions.
For date and time, use IDEA’s built‑in functions. For param, use a custom Groovy script:
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 ? '\\r\
' : '')}; return result == '' ? null : '\\r\
' + result", methodParameters())For the return value, use another Groovy script:
groovyScript("return \"${_1}\" == 'void' ? null : '\\r\
* @return ' + \"${_1}\"", methodReturnType())Note: The “Skip if defined” option is unchecked because we do not need that behavior.
Click OK to save – the setup is complete.
3. Verify Results
3.1 Class Comment
The class comment is generated only when creating a new class:
3.2 Method Comment
The following cases are demonstrated:
No parameters
Single parameter
Multiple parameters
No return value
Has return value
4. Q&A
(1) Why must the Abbreviation be * and Expand with be Enter?
IDEA expands a template as “template name + trigger key”. Using * plus the Enter key triggers the template, producing /** ... */, which conforms to Javadoc syntax.
(2) Why is there an empty * line in the template?
It is reserved for a method description; you may delete it if not needed.
(3) Why are $time$ and $param$ placed together?
The custom param script avoids generating an empty @param line when there are no parameters, so $param$ must share the same line with $time$.
(4) Why implement return handling manually? methodReturnType() returns void for methods without a return value, which is unnecessary; the custom script adds @return only when a return type exists.
(5) Why isn’t $return$ on a separate line?
When methodReturnType() returns null, placing $return$ on its own line would cause backspace handling issues, so it stays on the same 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.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.
