Use of lombok annotations

@Data
most commonly used annotation

@Data
public class Demo {

    private Long id;

    private String name;
}

after compilation

    public class Demo {
        private Long id;
        private String name;

        public Demo() {
        }

        public Long getId() {
            return this.id;
        }

        public String getName() {
            return this.name;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean equals(Object o) {
            if (o == this) {
                return true;
            } else if (!(o instanceof Demo)) {
                return false;
            } else {
                Demo other = (Demo)o;
                if (!other.canEqual(this)) {
                    return false;
                } else {
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if (this$id == null) {
                        if (other$id != null) {
                            return false;
                        }
                    } else if (!this$id.equals(other$id)) {
                        return false;
                    }

                    Object this$name = this.getName();
                    Object other$name = other.getName();
                    if (this$name == null) {
                        if (other$name != null) {
                            return false;
                        }
                    } else if (!this$name.equals(other$name)) {
                        return false;
                    }

                    return true;
                }
            }
        }

        protected boolean canEqual(Object other) {
            return other instanceof Demo;
        }

        public int hashCode() {
            int PRIME = true;
            int result = 1;
            Object $id = this.getId();
            int result = result * 59 + ($id == null ? 43 : $id.hashCode());
            Object $name = this.getName();
            result = result * 59 + ($name == null ? 43 : $name.hashCode());
            return result;
        }

        public String toString() {
            return "Demo(id=" + this.getId() + ", name=" + this.getName() + ")";
        }
    }

@NonNull

    public void setId(@NonNull Long id) {
        this.id = id;
    }

after compilation

    public void setId(@NonNull Long id) {
        if (id == null) {
            throw new NullPointerException("id");
        } else {
            this.id = id;
        }
    }

@RequiredArgsConstructor

@RequiredArgsConstructor
public class Demo {
    @NonNull
    private Long id;

    private String name;
}

after compilation

public class Demo {
    @NonNull
    private Long id;
    private String name;

    public Demo(@NonNull Long id) {
        if (id == null) {
            throw new NullPointerException("id");
        } else {
            this.id = id;
        }
    }
}

@NoArgsConstructor @AllArgsConstructor

@NoArgsConstructor
@AllArgsConstructor
public class Demo {
    @NonNull
    private Long id;

    private String name;
}

after compilation

public class Demo {
    @NonNull
    private Long id;
    private String name;

    public Demo() {
    }

    public Demo(@NonNull Long id, String name) {
        if (id == null) {
            throw new NullPointerException("id");
        } else {
            this.id = id;
            this.name = name;
        }
    }
}

@Builder

When used alone, the constructor with full parameters is automatically generated. There is no default constructor. If you want to have a non-full parameter constructor, you must use it together with the @AllArgsConstructor annotation, and then add it manually or by annotation, otherwise it will not compile.

@Builder
public class Demo {
    @NonNull
    private Long id;

    private String name;
}

after compilation

public class Demo {
    @NonNull
    private Long id;
    private String name;

    Demo(@NonNull Long id, String name) {
        if (id == null) {
            throw new NullPointerException("id");
        } else {
            this.id = id;
            this.name = name;
        }
    }

    public static Demo.DemoBuilder builder() {
        return new Demo.DemoBuilder();
    }

    public static class DemoBuilder {
        private Long id;
        private String name;

        DemoBuilder() {
        }

        public Demo.DemoBuilder id(Long id) {
            this.id = id;
            return this;
        }

        public Demo.DemoBuilder name(String name) {
            this.name = name;
            return this;
        }

        public Demo build() {
            return new Demo(this.id, this.name);
        }

        public String toString() {
            return "Demo.DemoBuilder(id=" + this.id + ", name=" + this.name + ")";
        }
    }
}

@Cleanup

automatically call close()

     public static void main(String[] args) throws FileNotFoundException,IOException {
        @Cleanup InputStream in = new FileInputStream(args[0]);
        @Cleanup OutputStream out = new FileOutputStream(args[1]);
        byte[] b = new byte[10000];
        while (true) {
            int r = in.read(b);
            if (r == -1){
                break;
            }
            out.write(b, 0, r);
        }
    }

after compilation

    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream in = new FileInputStream(args[0]);

        try {
            FileOutputStream out = new FileOutputStream(args[1]);

            try {
                byte[] b = new byte[10000];

                while(true) {
                    int r = in.read(b);
                    if (r == -1) {
                        return;
                    }

                    out.write(b, 0, r);
                }
            } finally {
                if (Collections.singletonList(out).get(0) != null) {
                    out.close();
                }

            }
        } finally {
            if (Collections.singletonList(in).get(0) != null) {
                in.close();
            }

        }
    }

@Synchronized

lock the method

public class Demo {
    private final Object writeLock = new Object();

    @Synchronized
    public static void method1(){
        System.out.println("method1");
    }

    @Synchronized
    public void method2(){
        System.out.println("method1");
    }

    @Synchronized("writeLock")
    public void method3(){
        System.out.println("method1");
    }
}

after compilation

public class Demo {
    private static final Object $LOCK = new Object[0];
    private final Object $lock = new Object[0];
    private final Object writeLock = new Object();

    public Demo() {
    }

    public static void method1() {
        Object var0 = $LOCK;
        synchronized($LOCK) {
            System.out.println("method1");
        }
    }

    public void method2() {
        Object var1 = this.$lock;
        synchronized(this.$lock) {
            System.out.println("method1");
        }
    }

    public void method3() {
        Object var1 = this.writeLock;
        synchronized(this.writeLock) {
            System.out.println("method1");
        }
    }
}

@Wither

To assign a final value, there must be a full-parameter constructor

@AllArgsConstructor
public class Demo {

    @Wither
    private final int number;

}

after compilation

public class Demo {
    private final int number;

    public Demo(int number) {
        this.number = number;
    }

    public Demo withNumber(int number) {
        return this.number == number ? this : new Demo(number);
    }
}

@Log4j2@Log@Log4j@CommonsLog@Slf4j@XSLF4j

@Log4j2
public class Demo {

    public static void main(String[] args) {
        log.error("error");
    }

}

after compilation

public class Demo {
    private static final Logger log = LogManager.getLogger(Demo.class);

    public Demo() {
    }

    public static void main(String[] args) {
        log.error("error");
    }
}

Guess you like

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