-
-
Couldn't load subscription status.
- Fork 2.5k
Add @Generator annotation #3372
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
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
@Generatorannotation.@Generatorannotation 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
@Generatorannotation, user can suspend execution and yield values to iterator usingyieldThisoryieldAllstatement. The method must returnIterableorIterator.This PR closes #560 (Same functionality)
Transformation steps
@Generatoris wrapped with internal local class__Generatorby handler. The__Generatorclass extendslombok.Lombok.Generatorwhich is stub class implementingIterable,Iterator. The stub class also have blankyieldThis,yieldAllmethod declaration.lombok.Lombok.Generatorclass is transformed in post compiler phase using bytecode manipulation. The transformer remove stub class, capture every local variable into field, and transformyieldThis,yieldAllstatements. Efficiently transforms into state machine.Code example
Method
rangeyields number fromfromtoto, and methodjoindelegates multiple iterables by order.With Lombok
Generated code by Lombok handler
After bytecode manipulation, cannot be decompiled into java code.