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.
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 → Class and 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\\n ' : '')}; return result == '' ? null : '\\r\\n ' + result", methodParameters())Return generation script:
groovyScript("return \"${_1}\" == 'void' ? null : '\\r\\n * @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.
IT Xianyu
We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.
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.