How to Configure Class and Method Comment Templates in IntelliJ IDEA
This guide explains step‑by‑step how to set up class and method comment templates in IntelliJ IDEA, including creating file and live templates, configuring parameters, adding Groovy scripts for @param and @return generation, and verifying the generated Javadoc comments for Java files.
Reading font
This article provides a detailed tutorial on configuring automatic class and method comment templates in IntelliJ IDEA for Java development.
First, it shows how to add a class comment template by opening
Settings → Editor → File and Code Templates → File → Classand inserting a Javadoc header that includes author and date placeholders.
Example class template:
/**
* @author jitwxs
* @date ${YEAR}年${MONTH}月${DAY}日 ${TIME}
*
*/Next, it explains how to create a method comment template using Settings → Editor → Live Templates. A new template group (e.g., userDefine) is created, and a live template with abbreviation * is added.
Method template text to copy:
*
*
* @author jitwxs
* @date $date$ $time$$param$ $return$
*The template must be associated with the Java language via Define → Java in the "Applicable contexts" section.
To make the $param$ and $return$ placeholders work, custom Groovy scripts are defined for each variable.
Param generation 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())Return generation script:
groovyScript("return \"${_1}\" == 'void' ? null : '\\r\
* @return ' + \"${_1}\"", methodReturnType())The article also covers optional settings such as "Skip if defined" and explains why certain placeholders are placed on the same line.
Finally, it demonstrates the results: class comments appear automatically when a new class is created, and method comments are generated correctly for various method signatures (no parameters, single parameter, multiple parameters, with or without return values).
Q&A sections address common questions about template abbreviations, the purpose of empty asterisk lines, and the handling of return types.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.