I created a custom annotation in order to apply currying to a method or constructor.
You can find the complete code for this here: curry.
For more information on currying check this post and for a background
on annotations in Java check this one.
First we create the annotation:
We only need the annotation to be processed and then discarded (we don’t need to
have it available at run time or recorded on the .class) so we use @Retention(RetentionPolicy.SOURCE).
This annotation will only be used on methods and constructors so we set those as the Target
on @Target({ElementType.METHOD, ElementType.CONSTRUCTOR}).
Then we create a processor for the annotation:
Using @SupportedAnnotationTypes("dev.jsedano.curry.annotation.Curry") we say that
this processor will only look for that particular annotation.
@SupportedSourceVersion(SourceVersion.RELEASE_17) here we are saying the Java version supported.
The last one is pretty interesting, @AutoService(Processor.class) is from a Google
library called auto-service. In order for the Java compiler to use a
processor the class needs to be declared inside the jar on the META-INF/services
directory on the javax.annotation.processing.Processor file, the auto-service library
does that for you.
Then we need to implement the process method on out custom processor.
Inside our class we get the processingEnv object that provides us with functionality
such as getMessager() that we use to print a warning message on compile time when
the @Curry annotation is used on a method with 1 or more than 10 parameters:
We also have this one getFiler() which allows us to create Java source files:
On the tests module of the project we declare some methods with the @Curry annotation, for example this constructor:
After running mvn clean verify on the parent module we can see the autogenerated code under target/generated-sources/annotations/dev.jsedano.curry.tests:
It is not pretty looking, but we can use it to then curry the five parameter constructor
of the example class:
You can see another example here, but if you want to compile it
you need to do mvn clean install on the curryer module of curry.
Then we can set the values we need in a curried way and use it:
Download the complete code from this post here: curry.