lombok and use common annotations

Brief introduction

Most of the projects are essential to contain the database entity (Entity), a data carrier (dto, dataObject), and these two sections contains a large number of no business logic setter, getter, empty argument constructor, and we generally replication class toString (), equals (), hashCode () method (model anemia). The work is repetitive work, as a programmer, lazy is one of the essential qualities, the work must have been a big cow good package approach, which is lombok.

idea to install plug-ins, support lombok

lombok is only generated at compile the corresponding body of code, so the project directly call setter, getter, constructor will complain, this time you can install the appropriate plug-in support lombok IDE. Here are idea plug-in installation, eclipse please Baidu own.

installation method

  1. Enter the settings page (windows: setting, Mac: Preferences)
  2. Click Plugin
  3. Browse repositories
  4. Search lombok
  5. Click Install
  6. After installation Open comments permission to use normal: 
    • –>setting
    • –>Build,Execution,Deployment
    • –>Compiler
    • –>Annontation Processors
    • -> checkEnable annotation processing
    • –> Apply
  7. Restart Idea

The method of introducing

gradle

// https://mvnrepository.com/artifact/org.projectlombok/lombok
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.16'

 

maven

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.16</version>
</dependency>

Common method

@Setter

Generating setter method, final variable does not contain

//原始类
@Setter
public class TestEntity {
 
    private String name;
 
    private Integer age;
 
    private final String type = "type";
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
}

@Getter

Generating getter methods, final variable does not contain

//原始类
@Getter
public class TestEntity {
 
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
 
    public String getName() {
        return this.name;
    }
 
    public Integer getAge() {
        return this.age;
    }
 
    public String getType() {
        this.getClass();
        return "person";
    }
}

@NoArgsConstructor

Generates an empty configuration parameters

//原始类
@NoArgsConstructor
public class TestEntity {
 
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
}

@AllArgsConstructor

All configuration parameters generated

//原始类
@AllArgsConstructor
public class TestEntity {
 
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    @ConstructorProperties({"name", "age"})
    public TestEntity(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

@RequiredArgsConstructor

The marker is configured to generate a property of @NoNull

If the operation is marked as property @NoNull is null, null pointer exception is thrown.

//原始类
@RequiredArgsConstructor
public class TestEntity {
 
    private String name;
    @NonNull
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public class TestEntity {
    private String name;
    @NonNull
    private Integer age;
    private final String type = "person";
 
    @ConstructorProperties({"age"})
    public TestEntity(@NonNull Integer age) {
        if(age == null) {
            throw new NullPointerException("age");
        } else {
            this.age = age;
        }
    }
}

@ToString

All attributes generated toString () method

//原始类
@ToString
public class TestEntity {
 
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
 
    public String toString() {
        StringBuilder var10000 = (new StringBuilder()).append("TestEntity(name=").append(this.name).append(", age=").append(this.age).append(", type=");
        this.getClass();
        return var10000.append("person").append(")").toString();
    }
}

@EqualsAndHashCode

Generating equals () and hashCode methods

//原始类
@EqualsAndHashCode
public class TestEntity {
 
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
 
    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof TestEntity)) {
            return false;
        } else {
            TestEntity other = (TestEntity)o;
            if(!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    String this$name = this.name;
                    String other$name = other.name;
                    if(this$name == null) {
                        if(other$name == null) {
                            break label47;
                        }
                    } else if(this$name.equals(other$name)) {
                        break label47;
                    }
 
                    return false;
                }
 
                Integer this$age = this.age;
                Integer other$age = other.age;
                if(this$age == null) {
                    if(other$age != null) {
                        return false;
                    }
                } else if(!this$age.equals(other$age)) {
                    return false;
                }
 
                this.getClass();
                String this$type = "person";
                other.getClass();
                String other$type = "person";
                if(this$type == null) {
                    if(other$type != null) {
                        return false;
                    }
                } else if(!this$type.equals(other$type)) {
                    return false;
                }
 
                return true;
            }
        }
    }
 
    protected boolean canEqual(Object other) {
        return other instanceof TestEntity;
    }
 
    public int hashCode() {
        boolean PRIME = true;
        byte result = 1;
        String $name = this.name;
        int result1 = result * 59 + ($name == null?43:$name.hashCode());
        Integer $age = this.age;
        result1 = result1 * 59 + ($age == null?43:$age.hashCode());
        this.getClass();
        String $type = "person";
        result1 = result1 * 59 + ($type == null?43:$type.hashCode());
        return result1;
    }
}

@Data (common)

@Data direct modification of POJO or beans, getter all the variables, setter all is not final variables. If you do not generate the default mode, you need to directly fill annotation on it. All annotation generated by default are public, if you need different rights modifiers can be used AccessLevel.NONE option. Of course, you can also use staticConstructor @Data option to generate a static method.

=@Setter+@Getter+@EqualsAndHashCode+@NoArgsConstructor

//原始类
@Data
public class TestEntity {
    @Setter(AccessLevel.PRIVATE)
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
 
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
 
    public String getName() {
        return this.name;
    }
 
    public Integer getAge() {
        return this.age;
    }
 
    public String getType() {
        this.getClass();
        return "person";
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
 
    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof TestEntity)) {
            return false;
        } else {
            TestEntity other = (TestEntity)o;
            if(!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    String this$name = this.getName();
                    String other$name = other.getName();
                    if(this$name == null) {
                        if(other$name == null) {
                            break label47;
                        }
                    } else if(this$name.equals(other$name)) {
                        break label47;
                    }
 
                    return false;
                }
 
                Integer this$age = this.getAge();
                Integer other$age = other.getAge();
                if(this$age == null) {
                    if(other$age != null) {
                        return false;
                    }
                } else if(!this$age.equals(other$age)) {
                    return false;
                }
 
                String this$type = this.getType();
                String other$type = other.getType();
                if(this$type == null) {
                    if(other$type != null) {
                        return false;
                    }
                } else if(!this$type.equals(other$type)) {
                    return false;
                }
 
                return true;
            }
        }
    }
 
    protected boolean canEqual(Object other) {
        return other instanceof TestEntity;
    }
 
    public int hashCode() {
        boolean PRIME = true;
        byte result = 1;
        String $name = this.getName();
        int result1 = result * 59 + ($name == null?43:$name.hashCode());
        Integer $age = this.getAge();
        result1 = result1 * 59 + ($age == null?43:$age.hashCode());
        String $type = this.getType();
        result1 = result1 * 59 + ($type == null?43:$type.hashCode());
        return result1;
    }
 
    public String toString() {
        return "TestEntity(name=" + this.getName() + ", age=" + this.getAge() + ", type=" + this.getType() + ")";
    }
 
    private void setName(String name) {
        this.name = name;
    }
}
 

@Builder

Model Builder configured structure. Constructed by the object internal category Builder ().

//原始类
@Builder
public class TestEntity {
 
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    @ConstructorProperties({"name", "age"})
    TestEntity(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
 
    public static TestEntity.TestEntityBuilder builder() {
        return new TestEntity.TestEntityBuilder();
    }
 
    public static class TestEntityBuilder {
        private String name;
        private Integer age;
 
        TestEntityBuilder() {
        }
 
        public TestEntity.TestEntityBuilder name(String name) {
            this.name = name;
            return this;
        }
 
        public TestEntity.TestEntityBuilder age(Integer age) {
            this.age = age;
            return this;
        }
 
        public TestEntity build() {
            return new TestEntity(this.name, this.age);
        }
 
        public String toString() {
            return "TestEntity.TestEntityBuilder(name=" + this.name + ", age=" + this.age + ")";
        }
    }
}
 
//Builder模式使用方法
@Test
public  void test(){
    TestEntity testEntity = TestEntity.builder()
                    .name("java")
                    .age(18)
                    .build();
}

@Value

And @Data corresponding @Value, the main difference between the two is that all annotation inflicted final if the variable without @NonFinal, @ Value of will. Of course, if the case is final, there is no set way to go.

//原始类
@Value
public class TestEntity {
    @Setter(AccessLevel.PRIVATE)
    private String name;
 
    private Integer age;
 
    private final String type = "person";
}
//反编译的类
public final class TestEntity {
    private final String name;
    private final Integer age;
    private final String type = "person";
 
    @ConstructorProperties({"name", "age"})
    public TestEntity(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return this.name;
    }
 
    public Integer getAge() {
        return this.age;
    }
 
    public String getType() {
        this.getClass();
        return "person";
    }
 
    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof TestEntity)) {
            return false;
        } else {
            TestEntity other;
            label44: {
                other = (TestEntity)o;
                String this$name = this.getName();
                String other$name = other.getName();
                if(this$name == null) {
                    if(other$name == null) {
                        break label44;
                    }
                } else if(this$name.equals(other$name)) {
                    break label44;
                }
 
                return false;
            }
 
            Integer this$age = this.getAge();
            Integer other$age = other.getAge();
            if(this$age == null) {
                if(other$age != null) {
                    return false;
                }
            } else if(!this$age.equals(other$age)) {
                return false;
            }
 
            String this$type = this.getType();
            String other$type = other.getType();
            if(this$type == null) {
                if(other$type != null) {
                    return false;
                }
            } else if(!this$type.equals(other$type)) {
                return false;
            }
 
            return true;
        }
    }
 
    public int hashCode() {
        boolean PRIME = true;
        byte result = 1;
        String $name = this.getName();
        int result1 = result * 59 + ($name == null?43:$name.hashCode());
        Integer $age = this.getAge();
        result1 = result1 * 59 + ($age == null?43:$age.hashCode());
        String $type = this.getType();
        result1 = result1 * 59 + ($type == null?43:$type.hashCode());
        return result1;
    }
 
    public String toString() {
        return "TestEntity(name=" + this.getName() + ", age=" + this.getAge() + ", type=" + this.getType() + ")";
    }
}

@Synchronized

Synchronization method

//原始类
public class TestEntity {
    private String name;
 
    private Integer age;
 
    private final String type = "person";
    @Synchronized
    public void write(){
        //do something
    }
}
//反编译的类
public class TestEntity {
    private final Object $lock = new Object[0];
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
 
    public void write() {
        Object var1 = this.$lock;
        synchronized(this.$lock) {
            ;
        }
    }
}

@Cleanup @@SneakyThrows

Automatic call the close method to close the resource.

//原始类
public class TestEntity {
    private String name;
 
    private Integer age;
 
    private final String type = "person";
 
    @SneakyThrows
    public  void  outputStream(){
         @Cleanup OutputStream outputStream = new FileOutputStream(new File("/Users/hello"));
    }
}
//反编译的类
public class TestEntity {
    private String name;
    private Integer age;
    private final String type = "person";
 
    public TestEntity() {
    }
 
    public void outputStream() {
        try {
            FileOutputStream $ex = new FileOutputStream(new File("/Users/hello"));
            if(Collections.singletonList($ex).get(0) != null) {
                $ex.close();
            }
 
        } catch (Throwable var2) {
            throw var2;
        }
    }
}

 

Original link: https: //blog.csdn.net/u013225178/article/details/80721799

Guess you like

Origin www.cnblogs.com/muxi0407/p/11611885.html