Packaging and Implementing Your Java Code Generator — Code Generator [4/4]

Aditya Solge
3 min readAug 31, 2023

--

In this tutorial, we’re going to wrap up our journey of creating an annotation-based code generator in Java. If you’re just joining us, I recommend catching up on the previous parts of this series to get a comprehensive understanding of the concepts covered.

All parts of this project series

Packaging Your Code Generator

Packaging your custom annotation and processor using Maven is a critical step in getting your innovative tool ready for distribution. Let’s break down the process:

  1. Begin by creating a META-INF/services folder within the resources folder of your Maven project. If you don’t have a resources folder, go ahead and create one within the src/main directory.
  2. Inside the META-INF/services folder, create a file named javax.annotation.processing.Processor.
  3. Now, execute the mvn package command to build your project.
  4. Open the javax.annotation.processing.Processor file you created and enter the fully qualified class name of your processor, such as com.gogettergeeks.processor.FileDBProcessor.
  5. To finalize the packaging, run the mvn install command, which will place the generated JAR in your local Maven repository.

Implementing and Using Your Code Generator

Now comes the exciting part — using the code generator you’ve created. Follow these steps to leverage the power of your annotation-based code generator:

Start by creating a new Maven project (assuming you’re using Maven) and craft a model class. For example, let’s create a Student class:

import com.gogettergeeks.annotation.FileDBGenerated;
import com.gogettergeeks.annotation.Persisted;
import com.gogettergeeks.annotation.UniqueKey;

@FileDBGenerated
public class Student {
@Persisted
private String name;

@Persisted
@UniqueKey
private int rollNumber;

private double percentage;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getRollNumber() {
return rollNumber;
}

public void setRollNumber(int rollNumber) {
this.rollNumber = rollNumber;
}
}

After creating your model class, execute the mvn package command to build the project. You’ll notice that a target folder is generated, containing the five Java files that your code generator produces.

With the generated classes at your disposal, you’re all set to start using them. In your main method, include the following code

public class Main 
{
public static void main( String[] args ) throws Exception
{
System.out.println("====================== START ===========================");
StudentDao dao = new StudentDaoImpl();
List<Student> students = dao.getAll();
for (Student s : students) {
System.out.println("Name: " + s.getName() + " | RollNumber: " + s.getRollNumber());
}

System.out.println("==================== ADD =======================");
Student s1 = new Student();
s1.setName("John");
s1.setRollNumber(1);

Student s2 = new Student();
s2.setName("Tom");
s2.setRollNumber(2);

Student s3 = new Student();
s3.setName("Pop");
s3.setRollNumber(3);

Student s4 = new Student();
s4.setName("Baburao");
s4.setRollNumber(4);

dao.add(s1);
dao.add(s2);
dao.add(s3);
dao.add(s4);

students = dao.getAll();

for (Student s : students) {
System.out.println("Name: " + s.getName() + " | RollNumber: " + s.getRollNumber());
}

System.out.println("=================== UPDATE =============================");
s1.setName("Sam");
dao.update(s1);

students = dao.getAll();
for (Student s : students) {
System.out.println("Name: " + s.getName() + " | RollNumber: " + s.getRollNumber());
}

System.out.println("==================== DELETE ==========================");
dao.delete(s1);

students = dao.getAll();
for (Student s : students) {
System.out.println("Name: " + s.getName() + " | RollNumber: " + s.getRollNumber());
}
System.out.println("==================== END ==========================");
}
}

In this example, we’re showcasing several operations including fetching, adding, updating, and deleting students using the generated classes.

========================================================
Name: Tom | RollNumber: 2
Name: Pop | RollNumber: 3
Name: Baburao | RollNumber: 4
==================== ADD =======================
Name: Tom | RollNumber: 2
Name: Pop | RollNumber: 3
Name: Baburao | RollNumber: 4
Name: John | RollNumber: 1
Name: Tom | RollNumber: 2
Name: Pop | RollNumber: 3
Name: Baburao | RollNumber: 4
=================== UPDATE =============================
Name: Tom | RollNumber: 2
Name: Pop | RollNumber: 3
Name: Baburao | RollNumber: 4
Name: Sam | RollNumber: 1
Name: Tom | RollNumber: 2
Name: Pop | RollNumber: 3
Name: Baburao | RollNumber: 4
==================== DELETE ==========================
Name: Tom | RollNumber: 2
Name: Pop | RollNumber: 3
Name: Baburao | RollNumber: 4
Name: Tom | RollNumber: 2
Name: Pop | RollNumber: 3
Name: Baburao | RollNumber: 4
==================== END ==========================

Conclusion

Congratulations, you’ve successfully reached the end of this tutorial series! You’ve learned how to create a sophisticated annotation-based code generator in Java, package it using Maven, and effectively utilize the generated code to perform various operations. You’ve gained valuable insights into custom annotations, processors, and their implementation.

If you’ve found this tutorial enlightening and valuable, please don’t hesitate to hit the thumbs up button and share it with fellow developers. Stay tuned for more exciting tutorials to enhance your programming skills. Until next time, happy coding!

--

--

Aditya Solge
Aditya Solge

Written by Aditya Solge

I'm a full-time freelance software developer who used to work at Amazon. I help students with affordable learning at www.gogettergeeks.com.

No responses yet