Option exclude = “id” means that we don’t want to use this field in specified methods.
Value and Data
There are useful annotations: @Value and @Data, which are a composition of other annotations. So @Value is shorthand for: @ToString, @EqualsAndHashCode, @AllArgsConstructor, @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE), @Getter, and @Data is shorthand for: @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields, and @RequiredArgsConstructor.
Lombok has implemented some variants for Logger fields. It’s not necessary anymore to put a private static final field for logger. Proper annotation at the top of the class will be enough. So we can use one of these annotations:
@CommonsLogCreates, @JBossLog, @Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j:
instead of one of these long declarations:
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
@Synchronised – avoids multiple threads using a method at the same time.
@Cleanup – putting this annotation on the resource field (e.g. InputStream, OutputStream) will perform method close() on it before the code execution path exits your current scope.
val, var – we can use them as types of local variable declarations instead of actually writing the type. The difference between these two declarations is that the first one will make the field final.
final String name = “Steve”;
is the same as:
val name = “Steve”;
String name = “Steve”;
is the same as:
var name = “Steve”;
And if you want to learn more about Project Lombok and its features, take a look at projectlombok.org.
Last but not least, it is worth mentioning how Lombok actually works. While building the project, all annotations which were used are replaced with actual implementations, so if we go to target directory, we can see that the code is much longer and looks differently than it was originally written. We can say that Lombok just creates a visual impression for us to present the code in a better way, but when a compiler starts to work, everything comes back to its actual form.
As we can see, the Lombok Project is a very handy tool which significantly reduces lines of code. In my opinion, it also makes a project much more readable and there is smaller possibility of making mistakes using these annotations. If we don’t like it, we can always use the delombok feature (Code → Refactor → Delombok) which will switch annotations for original implementations.