Thursday, 8 December 2016

IntelliJ: Live Template for generating static nested Factory class

If you find yourself having to create a static nested Factory class time and time again then you'll find the following IntelliJ / Android Studio Live Template handy:

public static class Factory {
    private final $dependencyClass$ $dependency$;

    Factory(@NonNull $dependencyClass$ $dependency$){
        this.$dependency$ = $dependency$;

    public $createdClass$ create(){
        return new $createdClass$($dependency$);

Set the "Applicability" of the Live Template to be "Java declarations" and set the "Expression" associated to each of the variables as follows:
  • $dependencyClass$ -> completeSmart()
  • $dependency$ -> suggestVariableName()
  • $createdClass$ -> className()
Tick the "Skip if defined" option for the $createdClass$ variable and set the "Abbreviation" and "Description" for the Live Template as you wish and that's it... you're good to go!

No comments: