-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add @Generator annotation #3372
base: master
Are you sure you want to change the base?
Conversation
Add @Generator annotation, javac handler, eclipse handler, tests, feature page
I think you meant to swap the 2 snippets of code? The PR description now shows the use of the annotation under 'before lombok'. Minor detail 😄 |
It was to show how handler transform annotated code. I updated description. It will be less confusing now. |
On javac handler, body statements are not marked as generated anymore. Error if yields inside local class, lambda.
Introduce additional boolean field to check if generator peeked.
Can someone review this pr? |
Besides I got the usecase, I dislike introducing a runtime dependency on Lombok classes. |
The |
@storycraft Can you explain to me why we need this feature in lombok? Both examples can be easily coded using the Stream API or common librarys. At the moment, I see little benefit and a high maintenance burden, which would disqualify this feature. The second showstopper is the bytecode transformation. That strategy makes it impossible to delombok the code and write the tests in the usual format. Thats probably why there are no tests for the generate state machine 😄 |
This PR introduces
@Generator
annotation.@Generator
annotation converts normal method into generator(lazily evaluated iterator). It provides a easy way to create custom iterators. Creating iterators by hand are very hard and complex.Using
@Generator
annotation, user can suspend execution and yield values to iterator usingyieldThis
oryieldAll
statement. The method must returnIterable
orIterator
.This PR closes #560 (Same functionality)
Transformation steps
@Generator
is wrapped with internal local class__Generator
by handler. The__Generator
class extendslombok.Lombok.Generator
which is stub class implementingIterable
,Iterator
. The stub class also have blankyieldThis
,yieldAll
method declaration.lombok.Lombok.Generator
class is transformed in post compiler phase using bytecode manipulation. The transformer remove stub class, capture every local variable into field, and transformyieldThis
,yieldAll
statements. Efficiently transforms into state machine.Code example
Method
range
yields number fromfrom
toto
, and methodjoin
delegates multiple iterables by order.With Lombok
Generated code by Lombok handler
After bytecode manipulation, cannot be decompiled into java code.