88

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?

0

3 Answers 3

121

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

enter image description here

2
55

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.

3
  • 3
    I like the addition this article mentions for setting up en expression to populate the variable with a sensible default. coderwall.com/p/hzmhbw
    – Snekse
    Aug 8, 2014 at 21:48
  • @Snekse That's a good addition. You are welcome to improve my answer by editing it.
    – Nowaker
    Aug 9, 2014 at 2:31
  • This answer doesn't actually answer the questioner's question: to auto-fill the $CLASS_NAME$ variable with the class name you need to set the "Expression" associated to the $CLASS_NAME$ variable to be className(). See CrazyCoder's answer for a screenshot. Dec 9, 2016 at 10:08
2

log4j 2:

private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger($CLASS_NAME$.class);
1
  • 2
    This answer doesn't actually answer the questioner's question: to auto-fill the $CLASS_NAME$ variable with the class name you need to set the "Expression" associated to the $CLASS_NAME$ variable to be className(). See CrazyCoder's answer for a screenshot. Dec 9, 2016 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.