Intellij Live Template

JavaIntellij IdeaLive Templates

Java Problem Overview


Is there any way to use a default token value in Intellij Live templates? For example I have the following live template which declares a private variable that I would like to create nearly for every class:

private static final Logger logger = Logger.getLogger($CLASS$.class)

It seems unnecessary to type $CLASS$ every time when this live template is used, because it can be derived from the class in scope or filename. I was wondering if it is possible to use environment defined tokens in live templates as defaults?

Java Solutions


Solution 1 - Java

Check some other templates that already use the current class name:

enter image description here

Solution 2 - Java

log4j:

private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger($CLASS_NAME$.class);

slf4j:

private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger($CLASS_NAME$.class);

Make sure that you set the applicable context to Java and that shorten FQ names and skip if defined are enabled.

Solution 3 - Java

log4j 2:

private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger($CLASS_NAME$.class);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeonidView Question on Stackoverflow
Solution 1 - JavaCrazyCoderView Answer on Stackoverflow
Solution 2 - JavaNowakerView Answer on Stackoverflow
Solution 3 - Javabruno.do.amaralView Answer on Stackoverflow