Automate Java Class and Method Javadoc with IntelliJ IDEA Live Templates

This guide shows how to configure IntelliJ IDEA to automatically generate class and method Javadoc comments using file templates and live templates, including custom Groovy scripts for @param and @return annotations, applicable contexts, and step‑by‑step screenshots.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Automate Java Class and Method Javadoc with IntelliJ IDEA Live Templates

1. Class Comment Template

Open SettingsEditor → File and Code Templates, select the File tab, then the Class template and insert the following content (author and date placeholders are listed in the Description field):

/**
 * @author jitwxs
 * @date ${YEAR}年${MONTH}月${DAY}日 ${TIME}
 */

After saving, new classes will automatically include this comment. The same can be applied to interfaces by editing the Interface template.

2. Method Comment Template

Use Live Templates to generate method Javadoc.

Create a template group (e.g., userDefine) in Settings → Editor → Live Templates.

Add a new Live Template with abbreviation *, description, and the following template text (note the leading asterisk on the first line):

*
 * @author jitwxs
 * @date $date$ $time$$param$ $return$
 *

Set Expand with to Enter and define the applicable context as Java.

Configure variables: date and time use IDEA’s built‑in functions. param uses a custom Groovy script to generate @param lines only when parameters exist:

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

uses another Groovy script to add @return only for non‑void methods:

groovyScript("return \"${_1}\" == 'void' ? null : '\\r\
 * @return ' + \"${_1}\"", methodReturnType())

Uncheck “Skip if defined” to keep the placeholder line for method description.

3. Verify the Result

When creating a new class, the class comment appears automatically. When invoking the method template (type * and press Enter) the generated Javadoc adapts to the method’s parameters and return type, handling cases such as no parameters, single or multiple parameters, and void or non‑void returns.

4. Frequently Asked Questions

Why must the abbreviation be * and expand with Enter ? IDEA triggers a template when the abbreviation followed by the expand key is typed; using * plus Enter produces the leading asterisk required by Javadoc. Why is there an empty * line in the template? It reserves space for a method description; you may delete it if not needed. Why are $time$$param$ placed on the same line? The custom param script omits a line when there are no parameters, so the placeholders must be adjacent to keep the layout correct. Why implement a custom return‑type handling instead of methodReturnType() ? The built‑in function returns void for methods without a return value, which would generate an unnecessary @return line; the custom script suppresses it. Why isn’t $return$ on a separate line? When the return expression is null, placing it on the same line avoids an extra blank line that cannot be backspaced.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaIntelliJ IDEAcode documentationJavadocLive TemplatesIDE automation
Java Backend Technology
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.