lombok注解的使用

@Data
最常用注解

@Data
public class Demo {

    private Long id;

    private String name;
}

编译后

    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;
    }

编译后

    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;
}

编译后

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;
}

编译后

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

单独使用时自动生成全参的构造器,没有默认构造器了,想要有非全参构造器必须与@AllArgsConstructor注解同时使用,然后手动或注解添加,否则将无法编译

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

    private String name;
}

编译后

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

自动调用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);
        }
    }

编译后

    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

给方法加锁

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");
    }
}

编译后

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

给final赋值,必须有全参的构造器

@AllArgsConstructor
public class Demo {

    @Wither
    private final int number;

}

编译后

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");
    }

}

编译后

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

    public Demo() {
    }

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

猜你喜欢

转载自blog.csdn.net/weixin_42115175/article/details/80174278