Code generation

Summary

Context-aware Code Generation is a unique feature that allows the tool to selectively use the content of the entire input source file to generate code. Context can be created out of fields, method names, parameters, and documentation. Providing additional information like constraints or exception declaration can help the model to generate more accurate results.

Example

To use the code generation feature create a new file. As an example of code generation of Java programming language, we will use a class name PaymentDao.

public class PaymentDao {

  private static final Logger logger = LoggerFactory.getLogger(PaymentDao.class);
  
  private final Session session;
  
  public PaymentDao(Session session) {
      this.session = session;
  }
  
  /**
   * Persists the payment in the database.
   *
   * @param payment payment to save
   * @throws IllegalArgumentException if payment is null
   */
  public void save(Payment payment) {
  }
  
  /**
   * Updates the payment in the database.
   *
   * @param payment payment to save
   */
  public void update(Payment payment) {
  }
  
  /**
   * Retrieves the payment from the database.
   *
   * @return payment the payment or null
   */
  public Payment get(String id) {
  }
  
  /**
   * Deletes the payment from the database.
   *
   * @param payment payment to save
   */
  public void delete(Payment payment) {
  }
}

Notice that the code defines a set of methods and two fields logger and session.

Generating code

In the provided example the source file defines a class with a set of methods. CodeMaker AI can process the entire file and generate the code for any method or function that has an empty body.

Generating code using Visual Studio Code

Generating code using JetBrains IDE

Generating code using CodeMaker CLI

  1. Executed command in the terminal: codemaker generate code **/PaymentDao.java

Replacing code

Similarly to generating code, it is possible to replace all method or function definitions across the entire file. In such case, the provided documentation will be used to generate the method function body.

Replacing documentation using Visual Studio Code

  1. Right-click in the editor and from the context menu choose CodeMaker AI > Replace > Code.

Replacing documentation using JetBrains IDE

  1. Right-click in the editor and from the context menu choose CodeMaker AI > Replace > Code.

Replacing documentation using CodeMaker CLI

  1. Executed command in the terminal: codemaker generate code --replace=true **/PaymentDao.java

Last updated