Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Lombok generates code onto existing class? [duplicate]

I can generate classes from scratch using annotationprocessor but I could not modify a class like lombok does. I've searched for the generated classes by lombok in android studio however I found nothing. then I checked the lombok overview via their website and also investigated it in forums but I've reached at end without anything. My question is so simple actually. How lombok unifies the generated code with mines while I use @Setter for instance. How can I develop a processor such as?

like image 315
Mustafa Güven Avatar asked Nov 03 '16 11:11

Mustafa Güven


People also ask

How does Lombok work in Java?

Lombok acts as an annotation processor that “adds” code to your classes at compile time. Annotation processing is a feature added to the Java compiler at version 5. The idea is that users can put annotation processors (written by oneself, or via third-party dependencies, like Lombok) into the build classpath.

What is @builder toBuilder true?

If using @Builder to generate builders to produce instances of your own class (this is always the case unless adding @Builder to a method that doesn't return your own type), you can use @Builder(toBuilder = true) to also generate an instance method in your class called toBuilder() ; it creates a new builder that starts ...

What is the use of @builder annotation?

The @Builder annotation produces complex builder APIs for the annotated POJO classes. For example, if we annotate a class Article annotated with @Builder annotation, we can create Article instances using builder API.

What is @builder annotation in Lombok?

Overview. Project Lombok's @Builder is a helpful mechanism for using the Builder pattern without writing boilerplate code. We can apply this annotation to a Class or a method. In this quick tutorial, we'll look at the different use cases for @Builder.


2 Answers

Seems like a duplicate of How does lombok work?, and I would flag to close as dupe but your bounty's preventing it.

In short, Lombok doesn't actually generate code at all. Instead, it uses unspecified and undocumented internal compiler implementation api calls to directly modify the program's abstract syntax tree between reading the source code and outputting compiled bytecode. It could break without warning or notice on updating to a new compiler version, but there's currently no other way.

like image 175
Douglas Avatar answered Sep 23 '22 01:09

Douglas


Lombok is generating code during the compilation phase. Here is a tutorial for that http://hannesdorfmann.com/annotation-processing/annotationprocessing101 .

If you are all new to declaring your own annotations i strongly recommend getting started with runtime annotations. They are easier to understand and debug and your code "acts" during the runtime phase you are already familiar with. A short tutorial for that: http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html

like image 25
tuempl Avatar answered Sep 22 '22 01:09

tuempl