@Helper
was introduced as an experimental feature in lombok v1.16.6.
Experimental because:
This annotation lets you put methods in methods. You might not know this, but you can declare classes inside methods, and the methods in this class can access any (effectively) final local variable or parameter defined and set before the declaration. Unfortunately, to actually call any methods you'd have to make an instance of this method local class first, but that's where @Helper
comes in and helps you out! Annotate a method local class with @Helper
and it's as if all the methods in that helper class are methods that you can call directly, just as if java had allowed methods to exist inside methods.
Normally you'd have to declare an instance of your helper, for example: HelperClass h = new HelperClass();
directly after declaring your helper class, and then call methods in your helper class with h.helperMethod();
. With @Helper
, both of these things are no longer needed: You do not need to waste a line of code declaring an instance of the helper, and you don't need to prefix all your calls to helper methods with nameOfHelperInstance.
@Helper
requires that the helper class has a no-args constructor. A compiler error will be generated if this is not the case.
Currently, the instance of your helper that's made under the hood is called $Foo
, where Foo
is the name of your helper. We might change this in the future; please don't rely on this variable existing. We might even replace this later with a sibling method instead.
Please don't rely on this
making any sense in the helper method code. You can refer to the real 'this' by using the syntax NameOfMyClass.this
.
ANY unqualified method call in code that exists below the declaration of the helper method with the same name as any method in the helper is assumed to be a call to the helper. If the arguments don't end up being compatible, you get a compiler error.
Unless you're using JDK8 or higher (which introduced the concept of 'effectively final'), you'll have to declare local variables and parameters as final
if you wish to refer to them in your method local class. This is a java limitation, not something specific to lombok's @Helper
.