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
Filetab, then the
Classsub‑tab and add the template shown below.
<code>/**
* @author jitwxs
* @date ${YEAR}年${MONTH}月${DAY}日 ${TIME}
*/
</code>The template includes author and date; all IDEA template variables are listed in the
Descriptionfield.
After saving, new classes automatically receive this comment. To apply the same to interfaces, also configure the
Interfacetab.
2. Method Comment
This tutorial implements automatic generation of @param and @return tags based on the method signature.
Generate
@paramannotations from method parameters.
Generate
@returnannotation 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):
<code>*
*
* @author jitwxs
* @date $date$ $time$$param$ $return$
*/
</code>Since no applicable contexts are set yet, click
Defineand check
Javato apply the template to all Java files.
In the template text, placeholders such as
$date$,
$time$,
$param$, and
$return$need expressions. Click
Edit variablesand assign expressions.
For
dateand
time, use IDEA’s built‑in functions. For
param, use a custom Groovy script:
<code>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\\n ' : '')}; return result == '' ? null : '\\r\\n ' + result", methodParameters())
</code>For the return value, use another Groovy script:
<code>groovyScript("return \"${_1}\" == 'void' ? null : '\\r\\n * @return ' + \"${_1}\"", methodReturnType())
</code>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
paramscript avoids generating an empty
@paramline when there are no parameters, so
$param$must share the same line with
$time$.
(4) Why implement return handling manually?
methodReturnType()returns
voidfor methods without a return value, which is unnecessary; the custom script adds
@returnonly 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.
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.