Fundamentals 8 min read

Auto‑Generate Class and Method Javadoc Templates in IntelliJ IDEA

This guide walks you through configuring IntelliJ IDEA to automatically insert class‑level and method‑level Javadoc comments using File and Code Templates and Live Templates, including GroovyScript expressions for dynamic @author, @date, @param, and @return tags.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Auto‑Generate Class and Method Javadoc Templates in IntelliJ IDEA

Class Comment Template

Open SettingsEditor → File and Code Templates, select the Class tab and replace the default content with a custom template that inserts author and date information. The template uses IDEA’s built‑in variables such as ${YEAR}, ${MONTH}, ${DAY} and ${TIME}:

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

After saving, any newly created Java class will automatically contain this header.

Method Comment Template

Method comments are more complex, so we create a Live Template:

Open SettingsEditor → Live Templates.

Click the + button, choose 2. Template Group..., name the group (e.g., userDefine).

Select the new group, click + again, and choose 1. Live Template.

Set Abbreviation to * (the trigger key) and ensure Expand with is set to Enter.

In the Template text field paste the following (note the first line starts directly with * and there is no leading / slash):

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

Set the Applicable Contexts to Java so the template works for all Java files.

Next, define the variables used in the template: date and time use IDEA’s built‑in functions (select them from the dropdown). param and return require custom Groovy scripts because the default implementations either always generate a line or produce an unwanted void entry.

Groovy script for param (generates @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 ? '
 ' : ''); }; return result == '' ? null : '
 ' + result", methodParameters())

Groovy script for return (adds @return only when the method does not return void):

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

Make sure the Skip if defined option is unchecked, otherwise the cursor would jump over already defined sections.

Verification

When you create a new class, the class header appears automatically. When you invoke the method template (type * and press Enter) inside a Java file, the generated Javadoc adapts to the method signature, handling cases such as no parameters, single or multiple parameters, no return value, and a return value.

Q & A

Why must the abbreviation be * and Expand with be Enter ? The template is triggered by the combination * + Enter, which IDEA interprets as the start of a Javadoc comment ( /**).

Why is there an empty * line in the template? It reserves a place for a method description; you can delete it if not needed.

Why are $time$ and $param$ placed together? The custom param script expects the placeholder to be on the same line to handle backspace correctly.

Why implement return manually instead of using methodReturnType() ? The default returns void for methods without a return value, which is unnecessary; the custom script only emits @return when a real return type exists.

Why isn’t $return$ on a separate line? When methodReturnType() returns null, placing it on a separate line would cause backspace handling issues, so it is kept inline.

After saving the settings, the templates work as described.

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 IDEAJavadocLive TemplatesIDE configurationGroovyScript
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.