Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...