This annotation processor adds a lazy/cacheable method to any non-final method annotated with @LazyGen
via code
generation.
Note: A Lazy/Cacheable method is a method which only executes its body once and keeps a copy of the result in memory.
annotationProcessor 'io.github.askmeagain:lazygenprocessor:1.2.1'
implementation 'io.github.askmeagain:lazygenprocessor:1.2.1'
- Add LazyGen package and annotation processor to your project
- Add
@GenerateLazyClass(ResultType.XXX)
to a class (or interface in case of MapStruct mapper) - Specify how your result should look like:
ResultType.CLASS
: Creates a normal class which extends the original classResultType.ABSTRACT_CLASS
: Creates an abstract class which extends/implements the interfaceResultType.MAPSTRUCT_COMPATIBLE
: Creates an abstract class, prepared correctly for usage with MapStructResultType.MAPSTRUCT_COMPATIBLE_WITHOUT_ANNOTATIN
is the same as step 3, but the @Mapper annotation is not added
- Add
@LazyGen
to any method on this class and any childs (class or interface doesnt matter) - Hit build
- A class is generated which inherits the original class, with Lazy as suffix
You can also specify how the caching should be implemented:
ONE_TIME_USE
: returns always the same (cached) value, ignoring the input parametersMULTI_USE
: will cache the result of the method in a map based on a key. The key is calculated viahashCode()
of the input parametersPARENT
: default on@LazyGen
methods, use what the@GenerateLazyClass
specified (by defaultONE_TIME_USE
)
You can configure a general usage, via @GenerateLazyClass(usage = ONE_TIME_USE)
, but each @LazyGen
method can
override this behaviour by themselves.
- Add
@GenerateLazyClass(ResultType.MAPSTRUCT_COMPATIBLE)
to your mapstruct mapper - Remove
@Mapper
annotation from your mapper - Add
@LazyGen
to any method - Get your MapStruct mapper via
Mappers.getMapper(XXXXXXLazy.class);
Note: Methods which are touched by the MapStruct annotation processor can only be made lazy if they are
implemented/referenced via @Named
annotation or else MapStruct cannot find the correct method since the @Named
annotation is not inheritable.
Before
@GenerateLazyClass
public class NormalClass {
@LazyGen
String abc(){
return "Test";
}
}
After
public class LazyNormalClass extends NormalClass {
private java.lang.String _abc;
@Override
public java.lang.String abc() {
if (_abc != null) {
return _abc;
}
_abc = super.abc();
return _abc;
}
}
Before
@GenerateLazyClass(ResultType.MAPSTRUCT_COMPATIBLE)
public interface TestMapper {
@Mapping(target = ".", source = "input", qualifiedByName = "a")
String mapSummations(String input);
@LazyGen
@Named("a")
default String a(TestMapper calculator) {
System.out.println("a");
return "a";
}
}
After
@Mapper
public abstract class LazyTestMapper implements TestMapper {
@Named("a")
@Override
public java.lang.String a(io.github.askmeagain.lazygen.calculator.simple.MapstructAbstractClass _TestMapper0) {
if (_a != null) {
return _a;
}
_a = TestMapper.super.a(_TestMapper0);
return _a;
}
private java.lang.String _a;
}