Lombox plugin installation, Log, Data, Value and other annotation details

effect

Lombox is an IDE plug-in. It can automatically generate code by writing the annotations officially provided by lombox in the code, which helps to optimize the simplicity of the code and eliminate bloat. Of course, if you don't need it, you just need to write a few more lines of code, such as in the POJO class. getter and setter methods etc.

Steps for usage

Install the lombox plugin in the IDE

Eclipse installation steps:
1. Download Lombok.jar, you can download the jar package from the official document or the Internet
2. Copy lombok.jar to the folder where myeclipse.ini / eclipse.ini is located
3. Open eclipse.ini / myeclipse .ini, insert the following two lines at the end and save:

    -Xbootclasspath/a:lombok.jar
  -javaagent:lombok.jar

4. Restart eclipse/myeclipse

Installation steps in IDEA:
This is relatively simple, open the tool, and then pop up the interface File->Setting->Plugins->Browse Repositories 搜索lombox插件,然后在右边点击“安装”to install. Then make the annotation take effect in the settings panelBuild,Execution,Deployment->Compiler->Annotation Processors->Enable annotation processing勾选

Add Maven dependency

The purpose is to introduce annotations for plugins to generate code.
The latest version can be checked from the maven repository

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.10</version>
    </dependency>
</dependencies>

annotation

The use of lombok mainly relies on annotations.
Official Annotation List

A total of 10 annotations:
val, var, @NonNull, @Cleanup, @Cleanup, @Getter/@Setter, @ToString, @EqualsAndHashCode, @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor, @Data, @Value, @Builder, @Value, @SneakyThrows, @Synchronized, @Getter(lazy=true), @Log, experimental.

1. @NonNullNull pointer filtering helps us to verify whether the parameter is a null pointer. 注解在参数上,如果该参数为null 会throw new NullPointerException(参数名);
Use lombok:

public NonNullExample(@NonNull Person person) {
    super("Hello");
    this.name = person.getName();
  }

Unused

public NonNullExample(Person person) {
    super("Hello");
    if (person == null) {
      throw new NullPointerException("person");
    }
    this.name = person.getName();
  }

2. @Cleanup, the annotation is before the variable. Automatically recycle resources, call the close()method by default
For example :

   @Cleanup InputStream in = new FileInputStream(args[0]);
  @Cleanup OutputStream out = new FileOutputStream(args[1]);

3. @Getter/@Setter,Annotate on properties or classes.
4. Provide setting/getting methods for properties @ToString, annotated on the class. Automatically generate toString() method
5. @EqualsAndHashCodeAnnotated on the class. Automatically override the hashCodeand equalsmethods of the parent class
6. There are three annotations for generating constructors.
@NoArgsConstructor: Annotated on the class. Generate a parameter-free constructor, which can highlight its importance when used in conjunction with other annotations. For example, when using a framework such as hibernate, if there is a parameter-based constructor, NoArgsConstructor will show his role.

@RequiredArgsConstructor: Annotated on the class. A constructor is generated that contains constants and variables marked NotNull. The generated constructor is private. If you want to provide external use, you can use the staticName option to generate a static method.

@AllArgsContructor: Annotated on the class. A file containing all variables will be generated, and if the variable uses the NotNull annotation, it will check whether it is empty or not.

7. @DataAnnotation on the class. All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor!. Means that 自动为所有字段添加@ToString, @EqualsAndHashCode, @Getter方法,为非final字段添加@Setter,和@RequiredArgsConstructor!
all code generated by default is public, if needed differently Permission modifiers can use the AccessLevel.NONE option
e.g.

import lombok.AccessLevel;
import lombok.Setter;
import lombok.Data;
import lombok.ToString;

@Data 
public class DataExample {
  private final String name;
  @Setter(AccessLevel.PACKAGE) private int age;
  private double score;
  private String[] tags;

  @ToString(includeFieldNames=true)
  @Data(staticConstructor="of")
  public static class Exercise<T> {
    private final String name;
    private final T value;
  }
}

8. @Value, Annotated on the class. It automatically adds Getter, toString(), equals(), hashCode() and a full-parameter constructor during compilation.
Corresponding to @Data, the main difference is that if @NonFinal is not added to the variable, @Value will make everything final. Of course, if it is final, there is no set method.

import lombok.AccessLevel;
import lombok.experimental.NonFinal;
import lombok.experimental.Value;
import lombok.experimental.Wither;
import lombok.ToString;

@Value public class ValueExample {
  String name;
  @Wither(AccessLevel.PACKAGE) @NonFinal int age;
  double score;
  protected String[] tags;

  @ToString(includeFieldNames=true)
  @Value(staticConstructor="of")
  public static class Exercise<T> {
    String name;
    T value;
  }
}

9. @Log, Annotated on the class. Log annotation, a loglog object named static final variable is automatically generated, and the instantiated class can use this variable directly.
That is, the annotation @Logcan automatically generate private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(XXX.getName());variables for the class.
There are also annotations for several other log situations including log4j and Slf4j, but the variables are unchanged, or static final log.

@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);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324606526&siteId=291194637