Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line Numbers Generation with Lombok

Tags:

java

lombok

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 ?

like image 998
Ashley Avatar asked Jun 19 '16 14:06

Ashley


People also ask

Is it good practice to use Lombok?

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.

What is Lambok?

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.

How does Lombok annotation work?

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.


1 Answers

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.

like image 54
Jens Bannmann Avatar answered Sep 28 '22 02:09

Jens Bannmann