Skip to content

Intellij IDEA Plugin for creating customized code generators like the builtin toString, equals, etc.

License

Notifications You must be signed in to change notification settings

lotabout/CodeGenerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeGenerator

An idea-plugin for code generation, support template customization.

// TODO: add demo

As we know, Intellij had provided useful code generators such as constructors, getter/setters, equals, hashCode, overrides and delegates, etc. And Intellij allows us to apply customized velocity templates for each generator. But we cannot add our own generators.

Code Generator is here to help. Two types of generation are supported here

  • Members(fields/method) based templates, such as serializers, equals, etc.
  • Class based template, such as transformers, converters, etc. Normally new classes are created.

Installation

  1. Search CodeGenerator in Idea plugins
  2. Download zip from from Releases

To install a plugin from the disk in idea:

  1. Open the Settings/Preferences dialog box and select Plugins on the left pane.
  2. On the right pane of the dialog, click the Install plugin from disk button.
  3. Select the location of the zip file, click OK to continue.
  4. Click Apply button of the Settings/Preferences dialog.
  5. Restart IntelliJ IDEA to activate.

Usage

  1. Go to the Settings/Preferences > Other Settings > CodeGenerator to create a new generator/template.
  2. Right click on your java file, Select Generate > CodeGenerator > [name of your generator] to run the generator.

According to the settings of your generator, there might be dialogs show up asking to select members or classes that's required by your generator.

Pipeline for Generators

Say we want to create a template for generating getters/setters, how will user use your template? An example(the default intellij implementation) is:

  1. A dialog show up listing all the fields that hadn't implement getters/setters yet.
  2. User select the members.
  3. The code is generated using the getter/setter template.

Thus, as a template creator, we need to:

getters-setters-workflow

Here we call it a pipeline for generators. Currently two types of user action are supported:

  1. Member selection: generator user can select fields/methods.
  2. Class selection: generator user can select a class.

Another example is: you might want to create templates that generate convertors between two classes, so that you want the user to select the target class to convert to.

In CodeGenerator, you can create a pipeline with several steps, CodeGenerator will execute the steps sequencially to collect the context variables. Finally generate the code use the template.

pipeline

Member Selection

member-selection

Templates varies on what members it allows for selection, for example:

  • Getters/Setters generator might want user to select only the fields that have no getters/setters implemented.
  • Delegate generators might want user to select the methods that belongs to the field or its super classes.

Thus CodeGenerator allows generator creators to provide the members to select:

  • set availableMembers to provide the members to select.
  • set selectedMembers to select the members initially, not setting it means select all available members.

Also after the selection, the template context will add some more variables:

  • fields1: the selected fields, where 1 is the step postfix;
  • methods1: the selected methods, if any;
  • members1: the selected fields/methods.

Here is an example of the context variables:

context-of-pipeline

Note in the begining, the class0 variable refers to the class entry where user starts code generation.

Class Selection

class-selection

Class selection is much simpler that template creator could specify the initial class to select.

Thanks to

  • CodeMaker: where the idea and part of code comes from.
  • generate-tostring: Offical toString generator. Part of the code comes from it.

About

Intellij IDEA Plugin for creating customized code generators like the builtin toString, equals, etc.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages