Useful IntelliJ plugins for java developers 2 (Lombok)
1. Introduction
Today, I would show you the useful IntelliJ IDEA plugin for java developers : the Lombok plugin.
2.1 The Lombok plugin introduction
- The plugin description
- This plugin provides support for lombok annotations to write great Java code with IntelliJ IDEA.
- The plugin official site
2.2 How to install this plugin
- Install the plugin online:
- Preferences > Plugins > Browse repositories… > Search for “lombok”
- Install Plugin Manually:
- Download the latest release and install it manually using Preferences > Plugins > Install plugin from disk…
- Restart IDE.
2.3 How to use this plugin
2.3.1 Set the lombok dependency in your project
- for maven users In your pom.xml:
- for gradel users In your build.gradle:
2.3.2 Change the IntelliJ IDEA preferences
In your project: Click Preferences -> Build, Execution, Deployment -> Compiler, Annotation Processors. Click Enable Annotation Processing.
Afterwards you might need to do a complete rebuild of your project via Build -> Rebuild Project.
2.4 The lombok annotations
2.4.1 The lombok @Getter/@Setter annotation
- What does this annotation do?
- You can annotate any field with @Getter and/or @Setter, to let lombok generate the default getter/setter automatically.
- The official site for this annotation: site
- The example usage code:
- The output:
2.4.2 The lombok @ToString annotation
- What does this annotation do?
- Any class definition may be annotated with @ToString to let lombok generate an implementation of the toString() method. By default, it’ll print your class name, along with each field, in order, separated by commas.
- The official site for this annotation: site
- The example usage code:
- The output:
You can also use the exlude mode of the ToString annotation like this:
The output is the same as the above.
2.4.3 The lombok @EqualsAndHashCode annotation
- What does this annotation do?
- Any class definition may be annotated with @EqualsAndHashCode to let lombok generate implementations of the equals(Object other) and hashCode() methods.
- The official site for this annotation: site
- The example usage code:
The line 1 means just generate hashCode and equals using the width and height field of the object.
- The output:
You can also use the exlude mode of the EqualsAndHashCode annotation like this:
The output is the same as the above.
2.4.4 The lombok @RequiredArgsConstructor annotation
- What does this annotation do?
- @RequiredArgsConstructor generates a constructor with 1 parameter for each field that requires special handling.
- The official site for this annotation: site
- The example usage code:
You can see that the height and width is final, so the lombok @RequiredArgsConstructor detect it and generate a constructor as follows:
public BoxConstructor(int height, int width) {…}
- The output:
2.4.5 The lombok @Log annotation
- What does this annotation do?
- You can annotate any class with a log annotation to let lombok generate a logger field.
- The @Log features:
- @CommonsLog
- Creates private static final org.apache.commons.logging.Log log = - org.apache.commons.logging.LogFactory.getLog(LogExample.class);
- @JBossLog
- Creates private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(- LogExample.class);
- @Log
- Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(- LogExample.class.getName());
- @Log4j
- Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(- LogExample.class);
- @Log4j2
- Creates private static final org.apache.logging.log4j.Logger log = - org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
- @Slf4j
- Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(- LogExample.class);
- @XSlf4j
- Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
- @CommonsLog
- The official site for this annotation: site
- The example usage code:
- The output:
2.4.6 The lombok @Data annotation
- What does this annotation do?
- @Data is a shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields, and @RequiredArgsConstructor.
- The official site for this annotation: site
- The example usage code:
- The output:
2.4.7 The lombok @Cleanup annotation
- What does this annotation do?
- You can use @Cleanup to ensure a given resource is automatically cleaned up before the code execution path exits your current scope. You do this by annotating any local variable declaration with the @Cleanup annotation.
- The official site for this annotation: site
- The example usage code:
3. Summary
The Lombok plugin helps you to generate some boilertemplate code for you , it can improve your efficiency. I recommend use it.
Other similar posts about IntelliJ IDEA plugins:
- The .ignore plugin
- A plugin helps you to create ignore rules with templates