Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the syntax `@__()` mean in Lombok?

Tags:

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?

like image 441
Andrew Tobilko Avatar asked Jun 27 '16 16:06

Andrew Tobilko


People also ask

What is @value annotation in Lombok?

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.

What are the annotations in Lombok?

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.

What is the difference between @data and @value in Lombok?

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.

What is Projectlombok Lombok?

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.


2 Answers

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.

like image 120
OrangeDog Avatar answered Sep 20 '22 15:09

OrangeDog


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)

like image 33
zapl Avatar answered Sep 18 '22 15:09

zapl