18

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?

Mustafa Güven
  • 15,526
  • 11
  • 63
  • 83

2 Answers2

23

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.

Community
  • 1
  • 1
Douglas
  • 5,017
  • 1
  • 14
  • 28
  • 2
    Also re "How can I develop a processor such as?", the easiest way will be to write a custom Lombok annotation. See e.g. https://binkley.blogspot.co.uk/2014/12/writing-your-own-lombok-annotation.html – Rich Nov 11 '16 at 14:42
2

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

tuempl
  • 109
  • 3