You can define your own templates by opening the dialog under Preferences\Java\Editor\Templates
Defining templates is quite straightforward. If you need a detailed introduction, please refer to the following source in Tutorialspoint.
Tips and tricks
Make it accessible
As it is possible to export and import templates, I keep my templates file under version control, so I can use them easily in all of my projects.Naming
Naming my own templates is important, while I need to select them all for export, I want to handle them together, and I want to find them during the coding really fast.
Therefore I add the prefix "my" to the template name, and use namespaces to group my templates. So, my templates are named like this:
You can found a complete list of the variables found here.
Therefore I add the prefix "my" to the template name, and use namespaces to group my templates. So, my templates are named like this:
- my.junit.assertTrue
- my.log.createLogger
- my.stringUtils.notBlank
Variables
You can use variables in your template, which makes it possible to define complex expressions.You can found a complete list of the variables found here.
Imports
If you use any class in the template, that needs to be imported, define the required import statements as well. Sometime I generate a template for commonly used classes, just for spare the time of importing the needed package. Commons StringUtils is such an example for it.${:import(org.slf4j.Logger,org.slf4j.LoggerFactory)} // Logger instance private static final Logger log = LoggerFactory.getLogger(${enclosing_type}.class);${cursor}
Static imports
If you use methods, to be imported statically, you can use the staicImport keyword${is:importStatic('org.junit.Assert.assertTrue')} assertTrue(${cursor});
My Templates are stored under GitHub, and free to use by anyone. It contains following templates:
- Singleton
- Lazy initialization in getter method
- Basic JUnit assertions with imports
- AssertThat method calls with static exports
- Mockito commands (when, mock, times)
- JUnit exception testing with the
@Rule annotation - Logging (create logger, using different log methods, logging for exception)
- Input validation for methods, using commons validation
- generating enumeration with a field inside
- Fixme and Todo templates
Nincsenek megjegyzések:
Megjegyzés küldése