This is the old documentation of lombok-pg. The new version can be found in the wiki of the github repository. Take me to the new Version then!

@Action

Overview

Encapsulates a method that does not return a value, so its signature looks like this:

public void methodName(T1 t1, T2 t2, …, Tn tn)

By default @Action uses lombok.Actions as template class. This means you need some lombok-pg classes at runtime. That's why lombok-pg provides a small library called lombok-pg-runtime.jar, containing only the runtime dependencies.
As a benefit of using lombok.Actions you get type getters for free, meaning you can execute methods like Class<?> getParameterType1() … Class<?> getParameterTypeN() on the action objects.
If you want to avoid having a runtime dependency to lombok-pg or just want to define you own template, use @Action(template=MyActionTemplate.class) and make sure that MyActionTemplate satisfies the following conditions:

  • The template class and any accessible (meaning public static) inner class qualifies as a possible template if they are an abstract class or an interface that have only one abstract public method.
  • The type arguments of the abstract method have to match the type arguments of the surrounding type.
  • A template class must not define multiple templates for the same argument count.

With Lombok

01 import lombok.ExtensionMethod;
02 import lombok.Action;
03 import lombok.Actions.Action1;
04 
05 @ExtensionMethod(Operations.class)
06 public class Main {
07 
08   public static void main(final String[] args) {
09     "Hello World".andThen(println());
10   }
11 
12   @Action
13   private static void println(final Object o) {
14     System.out.println(o);
15   }
16 }
17 
18 public class Operations {
19 
20   public static <T> void andThen(final T value, final Action1<T> andThen) {
21     if (value != nullandThen.apply(value);
22   }
23 }

Vanilla Java

01 import lombok.Actions.Action1;
02 
03 public class Main {
04 
05   public static void main(String[] args) {
06     Operations.andThen("Hello World", println());
07   }
08 
09   private static Action1<Object> println() {
10     return new Action1<Object>() {
11       public void apply(final Object o) {
12         System.out.println(o);
13       }
14     };
15   }
16 }
17 
18 public class Operations {
19 
20   public static <T> void andThen(final T value, final Action1<T> andThen) {
21     if (value != nullandThen.apply(value);
22   }
23 }

Small print

Being able to specifing a template class also allows you to use @Action with other libraries such as commons-collections @Action(template=Closure.class).