Does using lombok
in java
class will result in wrong line numbers in stacktrace
?
Assumption : No delombok
has been used on the code.
Lombok does not interrupt with line number generation
but it would generate code wherever the annotations are placed. These line nos. would be part of byte code and hence when in stacktrace
is printed, it would contain wrong line nos. Is my above interpretation correct ?
Some Good PracticesLombok is just a tool to make your life easy. It does not provide any assistance or support for clean code. If clean code and design principles are not of importance (trust me it's not that bad as it sounds in most of the cases), then using Lombok to simplify development process is the best option.
Project Lombok (from now on, Lombok) is an annotation-based Java library that allows you to reduce boilerplate code. Lombok offers various annotations aimed at replacing Java code that is well known for being boilerplate, repetitive, or tedious to write.
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.
The line numbers are stored in the .class
file by the Java compiler based on the actual lines of the .java
source code file.
Lombok hooks into the compilation process itself, modifying the bytecode that is written into the .class
file on the fly. It never actually adds text to the .java
source file and it also does not alter the line number information the compiler stores in the .class
file.
Therefore, the line numbers in stack traces always match the actual Java source code.
Note: The question ruled out the "delombok" case. For interested readers: you can use the "delombok" plugin to generate plain .java
code that incorporates everything that Lombok would otherwise put in during compilation. The tool is intended for situations like removing Lombok from a code base, or feeding source code to an analyzer tool that is confused by Lombok stuff. In that case, you end up with different, larger .java
source files, which of course have different line numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With