Backend Development 8 min read

Auto‑Generate Java Class and Method Javadoc with IntelliJ IDEA Templates

This guide shows how to configure IntelliJ IDEA to automatically insert Javadoc class and method comments, including author, date, @param and @return tags, using File and Code Templates and Live Templates with custom Groovy scripts.

macrozheng
macrozheng
macrozheng
Auto‑Generate Java Class and Method Javadoc with IntelliJ IDEA Templates

1. Class Comment

Open IDEA

Settings

Editor → File and Code Templates

, select the

File

tab, then the

Class

sub‑tab and add the template shown below.

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

The template includes author and date; all IDEA template variables are listed in the

Description

field.

After saving, new classes automatically receive this comment. To apply the same to interfaces, also configure the

Interface

tab.

2. Method Comment

This tutorial implements automatic generation of @param and @return tags based on the method signature.

Generate

@param

annotations from method parameters.

Generate

@return

annotation when the method has a return value.

To add a method comment template, go to

Settings → Editor → Live Templates

, click the

+

button, choose

2. Template Group...

and create a group (e.g.,

userDefine

).

Enter the group name.

Select the newly created group, click

+

again and choose

1. Live Template

.

Set Abbreviation to

*

, fill in Description and Template text . Ensure Expand with is set to the Enter key.

Copy the following template text (note the first line has no slash and the leading

*

is at column 1):

<code>*
 * 
 * @author jitwxs
 * @date $date$ $time$$param$ $return$
 */
</code>

Since no applicable contexts are set yet, click

Define

and check

Java

to apply the template to all Java files.

In the template text, placeholders such as

$date$

,

$time$

,

$param$

, and

$return$

need expressions. Click

Edit variables

and assign expressions.

For

date

and

time

, use IDEA’s built‑in functions. For

param

, use a custom Groovy script:

<code>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())
</code>

For the return value, use another Groovy script:

<code>groovyScript("return \"${_1}\" == 'void' ? null : '\\r\\n * @return ' + \"${_1}\"", methodReturnType())
</code>
Note: The “Skip if defined” option is unchecked because we do not need that behavior.

Click OK to save – the setup is complete.

3. Verify Results

3.1 Class Comment

The class comment is generated only when creating a new class:

3.2 Method Comment

The following cases are demonstrated:

No parameters

Single parameter

Multiple parameters

No return value

Has return value

4. Q&A

(1) Why must the Abbreviation be * and Expand with be Enter?

IDEA expands a template as “template name + trigger key”. Using

*

plus the Enter key triggers the template, producing

/** ... */

, which conforms to Javadoc syntax.

(2) Why is there an empty * line in the template?

It is reserved for a method description; you may delete it if not needed.

(3) Why are $time$ and $param$ placed together?

The custom

param

script avoids generating an empty

@param

line when there are no parameters, so

$param$

must share the same line with

$time$

.

(4) Why implement return handling manually?

methodReturnType()

returns

void

for methods without a return value, which is unnecessary; the custom script adds

@return

only when a return type exists.

(5) Why isn’t $return$ on a separate line?

When

methodReturnType()

returns

null

, placing

$return$

on its own line would cause backspace handling issues, so it stays on the same line.

Javacode generationIntelliJ IDEAJavadocLive TemplatesGroovy Script
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login 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.