Use Custom Annotations

Custom annotations allow repeated or boiler-plate code to be moved into a simple Java interface. These can cover a range of different implementations, and are very useful for classes that perform very similar functionality.
A common occurrence in large applications is to insert similar data into a database as can be seen in the example below. This example generates insert statements for a queue to process them at a later date.
public class StudentDatabaseService {
  
  public int insertStudent(UUID uuid, ) {
    // ... insert value here
  }     
}

public class ClassroomDatabaseService {
  
  public int insertClassroom(String name) {
    // ... insert value here
  }     
}
This can then be replaced by a code generation annotation which will create a class which can be used in its place as seen below. The example below is utilising a library called Java Poet which enables class, field, and method generation from provided parameters at compile-time.
// SQLInsertGenerator.java
@Target(ElementType.Type)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQLInsertGenerator {
  String tableName,
  Type[] types,
  String implName
}

// SQLInsertGeneratorAnnotationProcessor.java
@SupportedAnnotationTypes("com.example.SQLInsertGenerator")
public class SQLInsertGeneratorAnnotationProcessor extends AbstractProcessor {

  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    final MethodSpec insertMethod = MethodSpec.methodBuilder("insert")
      // add other parameters
      .addAnnotation(Override.class)
      .returns()
      .build();
  }
}

// StudentDatabaseService.java
@SQLInsertGenerator(
  tableName = "students",
  insertParameters = [String.class, Integer.class],
  implName = "StudentDatabaseServiceImpl"
)
public interface StudentDatabaseService {}

// ClassroomDatabaseService.java
@SQLInsertGenerator(
  tableName = "classrooms",
  insertParameters = [String.class, Integer.class],
  implName = "ClassroomDatabaseServiceImpl"
)
public interface ClassroomDatabaseService {}
boilerplate
annotations
code-generation