@Helper

Since

@Helper was introduced as an experimental feature in lombok v1.16.6.

Experimental

Experimental because:

  • Lambdas with general function types offer an alternative strategy.
  • Perhaps a way to make helper methods with less boilerplate is possible, making this feature obsolete.
Current status: unknown - We don't have enough experience with this feature to make predictions on its future.

Overview

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.

With Lombok

01 import lombok.experimental.Helper;
02 
03 public class HelperExample {
04   int someMethod(int arg1) {
05     int localVar = 5;
06 
07     @Helper class Helpers {
08       int helperMethod(int arg) {
09         return arg + localVar;
10       }
11     }
12 
13     return helperMethod(10);
14   }
15 }

Vanilla Java

01 public class HelperExample {
02   int someMethod(int arg1) {
03     int localVar = 5;
04 
05     class Helpers {
06       int helperMethod(int arg) {
07         return arg + localVar;
08       }
09     }
10     Helpers $Helpers = new Helpers();
11 
12     return $Helpers.helperMethod(10);
13   }
14 }

Small print

@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.