Here is the Eclipse template that I want to port:
${:import(org.apache.log4j.Logger)} private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);
My current version in IDEA is as follows:
private static final Logger LOG = Logger.getLogger($CLASS_NAME$.class);$END$
where $CLASS_NAME$
is configured to use className()
as its expression.
Unfortunately, I don't find any documentation on adding the import statement. Is there somehing equivalent to Eclipse ${:import(...)}
?
Automatically add import statements You can configure the IDE to automatically add import statements if there are no options to choose from. In the Settings/Preferences dialog ( Ctrl+Alt+S ), click Editor | General | Auto Import. Select the Add unambiguous imports on the fly checkbox, and apply the changes.
From the main menu, select File | New Projects Setup | Save Project as Template. In the dialog that opens, name the template and configure the options: Save: if the project contains more than one module, select whether you want to create a template from the whole project or from one of the modules.
In Eclipse, you press CTRL + SHIFT + O “Organize Imports” to import packages automatically. For IntelliJ IDEA, if you press CTRL + ALT + O “Optimize Imports”, it just removes some unused imports, never imports any package.
According to this post, it is intended to use only full-qualified expressions. I tried it out and this worked for me:
private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger($CLASS_NAME$.class);$END$
IDEA automatically shortens it and adds the necessary import statements:
import org.apache.log4j.Logger; // ... private static final Logger LOG = Logger.getLogger(MyClass.class);
If you want to try yourself, note that you first have to define CLASS_NAME
as className()
via Edit variables. Also make sure that you allowed your Live Template for Java declarations via Change (at the bottom). Here is a screenshot with the final setup:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With