How to Auto-Generate Javadoc Templates for Classes and Methods in IntelliJ IDEA
This guide shows how to configure IntelliJ IDEA to automatically generate Javadoc class and method comments using file templates and live templates, including step‑by‑step settings, custom Groovy scripts for @param and @return, and verification of the results.
1. Class Comment Template
Open Settings → Editor → File and Code Templates, select the Class tab and add the following template (without the leading slash):
/**
* @author jitwxs
* @date ${YEAR}年${MONTH}月${DAY}日 ${TIME}
*/2. Method Comment Template
In Settings → Editor → Live Templates create a new template group (e.g., userDefine) and add a new live template with abbreviation *. Set the description and template text as shown, ensure Expand with is Enter, and set applicable contexts to Java.
Template text (copy exactly):
*
* @author jitwxs
* @date $date$ $time$
* @param $param$
* @return $return$
*/Define variables: date and time use IDEA built‑in functions. param uses 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())And a custom script for return:
groovyScript("return \"${_1}\" == 'void' ? null : '\\r\
* @return ' + \"${_1}\"", methodReturnType())3. Verify the Result
When creating a new class, the class comment is inserted automatically.
Method comments are generated for various cases (no parameters, single parameter, multiple parameters, no return value, with return value).
4. Q & A
Why must the abbreviation be * and expand with Enter? Because IDEA triggers a template when the template name followed by the expand key is typed; * + Enter activates the Javadoc template.
Why is there an empty line with * in the template? It reserves a line for the method description, which can be removed if not needed.
Why are $time$$param$ placed together? The @param generation script was adjusted to avoid an empty line when there are no parameters, requiring the placeholders to be on the same line.
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 adds @return when a real return type exists.
Why is $return$ not on a separate line? When methodReturnType() returns null, handling a separate line causes backspace issues, so it is kept on the same line as other tags.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
