I have been working with and actively using Lombok for 2 months. With Java I'm a little more familiar. But, for the first time, I was faced with the following syntax structure in the language:
@RequiredArgsController(onController = @__(@Autowired))
^^^
What does that mean, and how does it get compiled?
Lombok Value annotation (@Value) is used when creating Immutable classes. All Lombok generated fields are made private and final by default, and setters are not generated. The class itself is also made final by default.
Lombok offers various annotations aimed at replacing Java code that is well known for being boilerplate, repetitive, or tedious to write. For example, by using Lombok, you can avoid writing constructors with no arguments, toString() , equals() , and hashCode() methods by simply adding a few annotations.
The big difference between @Value and @Data annotation is that @Value is mainly used to create immutable objects. @Value is a also an all-in-one annotation that combines: @Getter, @AllArgsConstructor, @ToString and @EqualsAndHashCode and @FieldDefaults(makeFinal = true, level = AccessLevel.
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
This is an experimental Lombok syntax, created to support a layer of indirection when referencing multiple annotations, rather than use a Class<?>[]
.
The syntax is a little strange; to use any of the 3
onX
features, you must wrap the annotations to be applied to the constructor / method / parameter in@__(@AnnotationGoesHere)
. To apply multiple annotations, use@__({@Annotation1, @Annotation2})
. The annotations can themselves obviously have parameters as well.
https://projectlombok.org/features/experimental/onX.html
An explanation from Lombok developer Roel Spilker:
The reason for it is that javac already resolves annotations in the parsing phase, and gives errors if it can determine that the annotations are invalid. By using a non-existent annotation
@__
it cannot determine it is bogus (it might be created by an annotation processor) and will not give an error right away. That gives Lombok the time to do its work and remove the@__
from the code.
It means that the generated constructor (not controller) will also have the @Autowired
annotation added to it so that spring can do its magic. With lombok you can write your code like
@RequiredArgsConstructor(onConstructor=@__(@Autowired(required=true)))
public class FooController {
private final FooService service;
interface FooService {}
}
and lombok will convert it during compilation to
public class FooController {
private final FooService service;
@Autowired(required=true)
public FooController(FooService service) {
this.service = service;
}
}
@__
is used to overcome the type limitations of annotations because
@interface MultipleAnnotations {
Annotation[] value();
}
does not work because the supertype of all annotations is itself not an annotation and
@interface MultipleAnnotations {
Class<? extends Annotation>[] value();
}
does not allow parameters in annotations: @MultipleAnnotations(SomeAnnotation.class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With