Auto‑Generate Class and Method Javadoc with IntelliJ IDEA Live Templates
This guide shows how to configure IntelliJ IDEA to automatically insert class‑level Javadoc comments and smart method‑level @param and @return annotations using custom live templates, covering template creation, variable expressions, and verification steps.
1. Class Comment Template
Open Settings → Editor → File and Code Templates, select the File tab, then the Class sub‑tab and add the following content:
/**
* @author jitwxs
* @date ${YEAR}年${MONTH}月${DAY}日 ${TIME}
*/After saving, every newly created class will automatically include this comment. To make the same comment work for interfaces, also edit the Interface tab with the same template.
2. Method Comment Template
This part creates a live template that automatically generates @param and @return tags based on the method signature.
Go to Settings → Editor → Live Templates and click the + button → Template Group.... Create a group (e.g., userDefine).
Select the newly created group, click + again and choose Live Template.
Set Abbreviation to * and Expand with to Enter.
Fill in Description and Template text (see code snippets below).
Click Define and check the Java context so the template applies to Java files.
Click Edit variables and set the expressions for the variables used in the template.
Variable expressions (Groovy scripts):
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()) groovyScript("return \"${_1}\" == 'void' ? null : '\\r\
* @return ' + \"${_1}\"", methodReturnType())Make sure the Skip if defined option is unchecked, then click OK to save the template.
3. Verify the Result
3.1 Class Comment
When a new class is created, the comment appears automatically:
3.2 Method Comment
The template correctly handles various situations: no parameters, a single parameter, 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 typing the abbreviation followed by the expansion key; using * + Enter produces the Javadoc opening /** automatically.
Why is there an empty * line in the template? It is reserved for a method description; you can delete it if you prefer.
Why are $time$$param$ placed on the same line? The custom param script was adjusted to avoid generating an empty line when there are no parameters, so the placeholders must stay on the same line for proper backspace handling.
Why implement a custom return‑type script 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 a real return type exists.
Why is $return$ not on a separate line? When methodReturnType() returns null, placing it on a separate line would cause backspace issues; keeping it inline solves the problem.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
