Giter Club home page Giter Club logo

codegenerator's Introduction

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.

codegenerator's People

Contributors

lotabout avatar aesy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.