• lombok在IntelliJ IDEA下的使用


    lombok是一款可以精减java代码、提升开发人员生产效率的辅助工具,利用注解在编译期自动生成setter/getter/toString()/constructor之类的代码。代码越少,意味着出bug的可能性越低。

    官网地址:https://projectlombok.org/ 首页有一段几分钟的演示视频,看完就明白是怎么回事了。

    先来二段对比代码:

    这是用lombok后的java代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    import lombok.*;
    import lombok.extern.slf4j.Slf4j;
    import java.io.ByteArrayInputStream;
    import java.io.*;
    import java.util.ArrayList;
     
    @Data
    @Slf4j
    @AllArgsConstructor
    public class Something {
     
        private String name;
        private final String country;
        private final Object lockObj = new Object();
     
        public void sayHello(@NonNull String target) {
            String content = String.format("hello,%s", target);
            System.out.println(content);
            log.info(content);
        }
     
        public void addBalabala() {
            val list = new ArrayList<String>();
            list.add("haha");
            System.out.println(list.size());
        }
     
        @SneakyThrows(IOException.class)
        public void closeBalabala() {
            @Cleanup InputStream is = new ByteArrayInputStream("hello world".getBytes());
            System.out.println(is.available());
        }
     
        @Synchronized("lockObj")
        public void lockMethod() {
            System.out.println("test lock method");
        }
    }

    等效于下面这段java代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    import java.beans.ConstructorProperties;
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Collections;
    import lombok.NonNull;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
     
    public class Something {
        private static final Logger log = LoggerFactory.getLogger(Something.class);
        private String name;
        private final String country;
        private final Object lockObj = new Object();
     
        public void sayHello(@NonNull String target) {
            if(target == null) {
                throw new NullPointerException("target");
            else {
                String content = String.format("hello,%s"new Object[]{target});
                System.out.println(content);
                log.info(content);
            }
        }
     
        public void addBalabala() {
            ArrayList list = new ArrayList();
            list.add("haha");
            System.out.println(list.size());
        }
     
        public void closeBalabala() {
            try {
                ByteArrayInputStream $ex = new ByteArrayInputStream("hello world".getBytes());
     
                try {
                    System.out.println($ex.available());
                finally {
                    if(Collections.singletonList($ex).get(0) != null) {
                        $ex.close();
                    }
     
                }
     
            catch (IOException var6) {
                throw var6;
            }
        }
     
        public void lockMethod() {
            Object var1 = this.lockObj;
            synchronized(this.lockObj) {
                System.out.println("test lock method");
            }
        }
     
        public String getName() {
            return this.name;
        }
     
        public String getCountry() {
            return this.country;
        }
     
        public Object getLockObj() {
            return this.lockObj;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public boolean equals(Object o) {
            if(o == this) {
                return true;
            else if(!(o instanceof Something)) {
                return false;
            else {
                Something other = (Something)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;
                    }
     
                    String this$country = this.getCountry();
                    String other$country = other.getCountry();
                    if(this$country == null) {
                        if(other$country != null) {
                            return false;
                        }
                    else if(!this$country.equals(other$country)) {
                        return false;
                    }
     
                    Object this$lockObj = this.getLockObj();
                    Object other$lockObj = other.getLockObj();
                    if(this$lockObj == null) {
                        if(other$lockObj != null) {
                            return false;
                        }
                    else if(!this$lockObj.equals(other$lockObj)) {
                        return false;
                    }
     
                    return true;
                }
            }
        }
     
        protected boolean canEqual(Object other) {
            return other instanceof Something;
        }
     
        public int hashCode() {
            boolean PRIME = true;
            byte result = 1;
            String $name = this.getName();
            int result1 = result * 59 + ($name == null?0:$name.hashCode());
            String $country = this.getCountry();
            result1 = result1 * 59 + ($country == null?0:$country.hashCode());
            Object $lockObj = this.getLockObj();
            result1 = result1 * 59 + ($lockObj == null?0:$lockObj.hashCode());
            return result1;
        }
     
        public String toString() {
            return "Something(name=" this.getName() + ", country=" this.getCountry() + ", lockObj=" this.getLockObj() + ")";
        }
     
        @ConstructorProperties({"name""country"})
        public Something(String name, String country) {
            this.name = name;
            this.country = country;
        }
    }

    大概减少了2/3的代码,各种注解的详细用法,请参考:https://projectlombok.org/features/index.html

    IDEA下使用时,可以通过插件的形式安装,插件下载地址:https://github.com/mplushnikov/lombok-intellij-plugin/releases 

    然后

    Plugins -> Install plugin from disk... 选择下载的zip包安装,重启idea即可。

    另外,还有一个关键设置:

    为了让设置生效,建议再重启一次idea,然后就可以开心的编码了,可以ide里可以直接看到生成的方法:(下图中打红圈的都是自动生成的)

      

  • 相关阅读:
    北京最好的体检中心
    算法初级面试题04——递归/非递归遍历二叉树、直观打印二叉树、寻找后继前驱节点、序列化/反序列化、折纸问题、判断是否平衡/搜索/完全二叉树、求完全二叉的节点数
    算法初级面试题03——打印链表公共部分、判断链表是否为回文、按值划分链表为小于等于大于、复制随机指针链表、两链表相交判断的一系列问题
    算法初级面试题03——队列实现栈、栈实现队列、转圈打印矩阵、旋转矩阵、反转链表、之字打印矩阵、排序矩阵中找数
    算法初级面试题02——荷兰国旗问题、随机快速排序、堆排序、桶排序、相邻两数的最大差值问题、工程中的综合排序算法面试题
    算法初级面试题01——认识时间复杂度、对数器、 master公式计算时间复杂度、小和问题和逆序对问题
    JAVAEE——SpringBoot日志篇:日志框架SLF4j、日志配置、日志使用、切换日志框架
    JAVAEE——SpringBoot配置篇:配置文件、YAML语法、文件值注入、加载位置与顺序、自动配置原理
    JAVAEE——SpringBoot入门:简介、微服务、环境准备、helloworld与探究、快速构建项目
    JAVAEE——宜立方商城14:项目部署规划、Tomcat热部署、反向代理的配置
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/6862660.html
Copyright © 2020-2023  润新知