Gipsy
Groovy version of Jipsy, a configurable AST Transformation to simplify the use of the Service Provider Interface.
Introduction
Gipsy delivers the same functionality that Jipsy does but uses Groovy’s AST Transformations instead of JDK6' Annotation Processor.
As explained at the service loader documentation, services must follow certain rules in order to be considered as such; they also must be registered using an standard location based on a naming convention. The following rules apply to classes that may be considered services
-
The class must implement at least one target interface (the service interface).
-
The class must provide a no-args constructor.
-
The class must be public.
-
The class name should be added to a file named
META-INF/services/<target_interface_name>
This library provides a mechanism for enforcing those rules by simply adding an annotation on each service implementation, for example say there exists the following Calculator
service interface
package com.acme
interface Calculator {
double add(double a, double b)
}
A basic implementation of such service may be as follows
package com.acme
@org.kordamp.jipsy.annotations.ServiceProviderFor(Calculator)
class BasicCalculator implements Calculator {
double add(double a, double b) { a + b }
}
Notice that Gipsy reuses the same annotations from Jipsy. Compile your code. If you look closely at your project’s output you’ll see a file named META-INF/services/com.acme.Calculator
whose contents should look similar to
# Generated by org.kordamp.gipsy.transform.service.ServiceProviderProcessor (1.1.1)
com.acme.BasicCalculator
Et voilà! There are no additional sources to be touched nor files to be created; Jipsy will take care of the boiler plate.
Installing
Gipsy requires the following dependencies
-
jipsy-processor-1.1.1
-
groovy-all-3.0.7
Gipsy can be downloaded directly from Maven Central, configure it via Maven or Gradle.
<dependency> <groupId>org.kordamp.gipsy</groupId> <artifactId>gipsy</artifactId> <version>1.1.1</version> <scope>provided</scope> </dependency>
dependencies { annotationProcessor 'org.kordamp.gipsy:gipsy:1.1.1' compileOnly 'org.kordamp.jipsy:jipsy-annotations:1.1.1' }
Creating Your Own AST Transformations
TBD