Automate Java Class and Method Javadoc with IntelliJ IDEA Live Templates

This guide walks you through configuring IntelliJ IDEA's File and Code Templates and Live Templates to automatically generate class and method Javadoc comments, including author, date, @param, and @return tags, with step‑by‑step screenshots, Groovy scripts, and a Q&A section.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Automate Java Class and Method Javadoc with IntelliJ IDEA Live Templates

1. Class Comment

Open SettingsEditor → File and Code Templates, select the Class tab, and add the template shown in the red box. The template includes author and date placeholders, which are listed in the Description section of IDEA.

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

After saving, new classes will automatically include this comment. To apply the same template to interfaces, configure the Interface tab similarly.

2. Method Comment

This tutorial creates a method comment template that automatically generates @param annotations based on method parameters and a @return annotation when the method has a return value.

Generate @param annotations.

Generate @return annotation when applicable.

In SettingsEditor → Live Templates, click the + button, choose 2. Template Group..., and name the group (e.g., userDefine). Then add a new Live Template inside this group.

Set the Abbreviation to * and ensure Expand with is set to the Enter key. Edit the template fields as follows:

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

Define the variables:

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 ? '
 ' : ''); }; return result == '' ? null : '
 ' + result", methodParameters())

For the return value:

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

Make sure Abbreviation is * and Expand with is Enter. Save the template.

3. Verify Results

3.1 Class Comment

When creating a new class, the comment is automatically inserted:

3.2 Method Comment

The template correctly handles various cases:

No parameters

Single parameter

Multiple parameters

No return value

With return value

4. Q & A

(1) Why must the Abbreviation be * and Expand with be Enter? Because IDEA triggers the template when the abbreviation followed by the Enter key is typed, forming the Javadoc start /**.

(2) Why is there an empty * line in the template? It is reserved for a method description; you can remove it if not needed.

(3) Why are $time$$param$ placed together? The custom param script was modified to avoid generating an empty @param line when there are no parameters, requiring the placeholders to be on the same line.

(4) Why implement methodReturnType() manually? The default returns void for methods without a return value, which is not useful; the custom script only generates @return when a return type exists.

(5) Why is $return$ not on a separate line? When methodReturnType() returns null, handling backspace becomes problematic, so it is kept on the same line.

After completing these steps, click OK to save the settings. The templates are now ready for use.

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.

JavaAutomationIntelliJ IDEAIDEJavadocLive Templates
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.