Fundamentals 7 min read

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 and Code Templates and Live Templates, including custom Groovy scripts for @param and @return tags, applicable contexts, and common pitfalls.

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 Template

Open SettingsEditor → File and Code Templates, select the File tab, then the Class sub‑tab and paste the template shown (author and date placeholders). After saving, every newly created class automatically includes the comment. To apply the same template to interfaces, also edit the Interface tab.

2. Method Comment Template

Method comment templates are more complex. In SettingsEditor → Live Templates, click the + button, choose 2. Template Group… and create a group (e.g., userDefine). Then add a new Live Template with the following settings:

Abbreviation: * Description: (any description you prefer)

Template text: the Javadoc skeleton shown below (note that the first line must start with * and the Expand with key must be Enter).

Applicable contexts: select Java so the template works for all Java files.

Make sure the Abbreviation is exactly * and that Expand with is set to the Enter key; this combination triggers the template when you type * and press Enter.

Define the following variables in the template:

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

This script generates @param lines for each method parameter.

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

This script adds an @return line only when the method has a non‑void return type.

Uncheck the "Skip if defined" option, then click OK to save the template.

3. Q&A

Why must the abbreviation be "*" and expand with Enter? IDEA triggers a template by concatenating the abbreviation with the expand key; * + Enter produces the opening /** of a Javadoc comment.

Why is there an empty "*" line in the template? It reserves a line for a method description; you can delete it if you don’t need it.

Why are $time$$param$ placed together? To avoid an extra blank line when there are no parameters; the custom param script suppresses empty @param entries.

Why implement a custom return‑type handler instead of using methodReturnType() ? The built‑in function returns void for methods without a return value, which is not useful for Javadoc; the custom script only emits @return when appropriate.

Why is $return$ not on a separate line? When the custom return handler returns null, placing it on a separate line would cause backspace handling issues.

Class template screenshot
Class template screenshot
Live template group creation
Live template group creation
Template editing screen
Template editing screen
Applicable contexts selection
Applicable contexts selection
Edit variables dialog
Edit variables dialog
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.

AutomationJavadocLive Templatesintellij-idea
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.