跳至主要內容

IDEA常见实用插件

xw大约 7 分钟IDEAIDEA

IDEA常见实用插件汇总

[TOC]

1.Translation

介绍:Translation是一款翻译插件,支持谷歌翻译、有道翻译、百度翻译。

使用:

  • 选中需要翻译的段落,点击鼠标右键进行翻译

  • 弹出翻译框快捷键:CTRL+SHIFT+O,也可以点击IDEA右上角按钮

2. GsonFormatPlus

GsonFormatPlus可以快速将JSON字符串转成实体类对象,快捷键ALT+S

3. arthas idea

arthas idea可以快速生成arthas命令,配合神器arthas使用很香。

4. Lombok

介绍:以简单的注解形式来简化java代码,提高开发人员的开发效率。

常见注解:

  • @Getter/@Setter: 自动产生getter/setter

    import lombok.AccessLevel;
    import lombok.Getter;
    import lombok.Setter;
    
    public class GetterSetterExample {
      /**
       * Age of the person. Water is wet.
       * 
       * @param age New value for this person's age. Sky is blue.
       * @return The current value of this person's age. Circles are round.
       */
      @Getter @Setter private int age = 10;
      
      /**
       * Name of the person.
       * -- SETTER --
       * Changes the name of this person.
       * 
       * @param name The new value.
       */
      @Setter(AccessLevel.PROTECTED) private String name;
      
      @Override public String toString() {
        return String.format("%s (age: %d)", name, age);
      }
    }
    

    编译后代码:

    public class GetterSetterExample {
      /**
       * Age of the person. Water is wet.
       */
      private int age = 10;
    
      /**
       * Name of the person.
       */
      private String name;
      
      @Override public String toString() {
        return String.format("%s (age: %d)", name, age);
      }
      
      /**
       * Age of the person. Water is wet.
       *
       * @return The current value of this person's age. Circles are round.
       */
      public int getAge() {
        return age;
      }
      
      /**
       * Age of the person. Water is wet.
       *
       * @param age New value for this person's age. Sky is blue.
       */
      public void setAge(int age) {
        this.age = age;
      }
      
      /**
       * Changes the name of this person.
       *
       * @param name The new value.
       */
      protected void setName(String name) {
        this.name = name;
      }
    }
    
  • @ToString: 自动重写toString()方法,会打印所有变量

    import lombok.ToString;
    
    @ToString
    public class ToStringExample {
      private static final int STATIC_VAR = 10;
      private String name;
      private Shape shape = new Square(5, 10);
      private String[] tags;
      @ToString.Exclude private int id;
      
      public String getName() {
        return this.name;
      }
      
      @ToString(callSuper=true, includeFieldNames=true)
      public static class Square extends Shape {
        private final int width, height;
        
        public Square(int width, int height) {
          this.width = width;
          this.height = height;
        }
      }
    }
    

    编译后代码:

    import java.util.Arrays;
    
    public class ToStringExample {
      private static final int STATIC_VAR = 10;
      private String name;
      private Shape shape = new Square(5, 10);
      private String[] tags;
      private int id;
      
      public String getName() {
        return this.name;
      }
      
      public static class Square extends Shape {
        private final int width, height;
        
        public Square(int width, int height) {
          this.width = width;
          this.height = height;
        }
        
        @Override public String toString() {
          return "Square(super=" + super.toString() + ", width=" + this.width + ", height=" + this.height + ")";
        }
      }
      
      @Override public String toString() {
        return "ToStringExample(" + this.getName() + ", " + this.shape + ", " + Arrays.deepToString(this.tags) + ")";
      }
    }
    
  • @EqualAndHashCode: 自动生成equal()和hashCode()方法

    
    import lombok.EqualsAndHashCode;
    
    @EqualsAndHashCode
    public class EqualsAndHashCodeExample {
      private transient int transientVar = 10;
      private String name;
      private double score;
      @EqualsAndHashCode.Exclude private Shape shape = new Square(5, 10);
      private String[] tags;
      @EqualsAndHashCode.Exclude private int id;
      
      public String getName() {
        return this.name;
      }
      
      @EqualsAndHashCode(callSuper=true)
      public static class Square extends Shape {
        private final int width, height;
        
        public Square(int width, int height) {
          this.width = width;
          this.height = height;
        }
      }
    }
    

    编译后代码:

    import java.util.Arrays;
    
    public class EqualsAndHashCodeExample {
      private transient int transientVar = 10;
      private String name;
      private double score;
      private Shape shape = new Square(5, 10);
      private String[] tags;
      private int id;
      
      public String getName() {
        return this.name;
      }
      
      @Override public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof EqualsAndHashCodeExample)) return false;
        EqualsAndHashCodeExample other = (EqualsAndHashCodeExample) o;
        if (!other.canEqual((Object)this)) return false;
        if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
        if (Double.compare(this.score, other.score) != 0) return false;
        if (!Arrays.deepEquals(this.tags, other.tags)) return false;
        return true;
      }
      
      @Override public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final long temp1 = Double.doubleToLongBits(this.score);
        result = (result*PRIME) + (this.name == null ? 43 : this.name.hashCode());
        result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
        result = (result*PRIME) + Arrays.deepHashCode(this.tags);
        return result;
      }
      
      protected boolean canEqual(Object other) {
        return other instanceof EqualsAndHashCodeExample;
      }
      
      public static class Square extends Shape {
        private final int width, height;
        
        public Square(int width, int height) {
          this.width = width;
          this.height = height;
        }
        
        @Override public boolean equals(Object o) {
          if (o == this) return true;
          if (!(o instanceof Square)) return false;
          Square other = (Square) o;
          if (!other.canEqual((Object)this)) return false;
          if (!super.equals(o)) return false;
          if (this.width != other.width) return false;
          if (this.height != other.height) return false;
          return true;
        }
        
        @Override public int hashCode() {
          final int PRIME = 59;
          int result = 1;
          result = (result*PRIME) + super.hashCode();
          result = (result*PRIME) + this.width;
          result = (result*PRIME) + this.height;
          return result;
        }
        
        protected boolean canEqual(Object other) {
          return other instanceof Square;
        }
      }
    }
    
  • @NoArgsConstructor: 无参构造器

  • @AllArgsConstructor:所有参数的构造器

  • @RequiredArgsConstructor:只含final修饰参数构造器

  • @Data: 包含@Getter/@Setter/@ToString/@EqualAndHashCode/@RequiredArgsConstructor

  • @Value: 变量设置成final,其他跟@Data一样

  • @Builder: 自动生成流式set写法

    import lombok.Builder;
    import lombok.Singular;
    import java.util.Set;
    
    @Builder
    public class BuilderExample {
      @Builder.Default private long created = System.currentTimeMillis();
      private String name;
      private int age;
      @Singular private Set<String> occupations;
    }
    

    编译后代码:

    import java.util.Set;
    
    public class BuilderExample {
      private long created;
      private String name;
      private int age;
      private Set<String> occupations;
      
      BuilderExample(String name, int age, Set<String> occupations) {
        this.name = name;
        this.age = age;
        this.occupations = occupations;
      }
      
      private static long $default$created() {
        return System.currentTimeMillis();
      }
      
      public static BuilderExampleBuilder builder() {
        return new BuilderExampleBuilder();
      }
      
      public static class BuilderExampleBuilder {
        private long created;
        private boolean created$set;
        private String name;
        private int age;
        private java.util.ArrayList<String> occupations;
        
        BuilderExampleBuilder() {
        }
        
        public BuilderExampleBuilder created(long created) {
          this.created = created;
          this.created$set = true;
          return this;
        }
        
        public BuilderExampleBuilder name(String name) {
          this.name = name;
          return this;
        }
        
        public BuilderExampleBuilder age(int age) {
          this.age = age;
          return this;
        }
        
        public BuilderExampleBuilder occupation(String occupation) {
          if (this.occupations == null) {
            this.occupations = new java.util.ArrayList<String>();
          }
          
          this.occupations.add(occupation);
          return this;
        }
        
        public BuilderExampleBuilder occupations(Collection<? extends String> occupations) {
          if (this.occupations == null) {
            this.occupations = new java.util.ArrayList<String>();
          }
    
          this.occupations.addAll(occupations);
          return this;
        }
        
        public BuilderExampleBuilder clearOccupations() {
          if (this.occupations != null) {
            this.occupations.clear();
          }
          
          return this;
        }
    
        public BuilderExample build() {
          // complicated switch statement to produce a compact properly sized immutable set omitted.
          Set<String> occupations = ...;
          return new BuilderExample(created$set ? created : BuilderExample.$default$created(), name, age, occupations);
        }
        
        @java.lang.Override
        public String toString() {
          return "BuilderExample.BuilderExampleBuilder(created = " + this.created + ", name = " + this.name + ", age = " + this.age + ", occupations = " + this.occupations + ")";
        }
      }
    }
    
  • @Slf4j: 自动生成类的log静态常量

  • val

    
    import java.util.ArrayList;
    import java.util.HashMap;
    import lombok.val;
    
    public class ValExample {
      public String example() {
        val example = new ArrayList<String>();
        example.add("Hello, World!");
        val foo = example.get(0);
        return foo.toLowerCase();
      }
      
      public void example2() {
        val map = new HashMap<Integer, String>();
        map.put(0, "zero");
        map.put(5, "five");
        for (val entry : map.entrySet()) {
          System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
        }
      }
    }
    

    编译后代码:

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    public class ValExample {
      public String example() {
        final ArrayList<String> example = new ArrayList<String>();
        example.add("Hello, World!");
        final String foo = example.get(0);
        return foo.toLowerCase();
      }
      
      public void example2() {
        final HashMap<Integer, String> map = new HashMap<Integer, String>();
        map.put(0, "zero");
        map.put(5, "five");
        for (final Map.Entry<Integer, String> entry : map.entrySet()) {
          System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
        }
      }
    }
    
  • @NonNull:非空校验

    import lombok.NonNull;
    
    public class NonNullExample extends Something {
      private String name;
      
      public NonNullExample(@NonNull Person person) {
        super("Hello");
        this.name = person.getName();
      }
    }
    

    编译后代码:

    import lombok.NonNull;
    
    public class NonNullExample extends Something {
      private String name;
      
      public NonNullExample(@NonNull Person person) {
        super("Hello");
        if (person == null) {
          throw new NullPointerException("person is marked @NonNull but is null");
        }
        this.name = person.getName();
      }
    }
    
  • @Cleanup: 自动清理指定资源

    import lombok.Cleanup;
    import java.io.*;
    
    public class CleanupExample {
      public static void main(String[] args) throws 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);
        }
      }
    }
    

    编译后代码:

    
    import java.io.*;
    
    public class CleanupExample {
      public static void main(String[] args) throws IOException {
        InputStream in = new FileInputStream(args[0]);
        try {
          OutputStream out = new FileOutputStream(args[1]);
          try {
            byte[] b = new byte[10000];
            while (true) {
              int r = in.read(b);
              if (r == -1) break;
              out.write(b, 0, r);
            }
          } finally {
            if (out != null) {
              out.close();
            }
          }
        } finally {
          if (in != null) {
            in.close();
          }
        }
      }
    }
    
  • @SneakThrows: 捕获或抛出已检查的异常

    
    import lombok.SneakyThrows;
    
    public class SneakyThrowsExample implements Runnable {
      @SneakyThrows(UnsupportedEncodingException.class)
      public String utf8ToString(byte[] bytes) {
        return new String(bytes, "UTF-8");
      }
      
      @SneakyThrows
      public void run() {
        throw new Throwable();
      }
    }
    

    编译后代码:

    
    import lombok.Lombok;
    
    public class SneakyThrowsExample implements Runnable {
      public String utf8ToString(byte[] bytes) {
        try {
          return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
          throw Lombok.sneakyThrow(e);
        }
      }
      
      public void run() {
        try {
          throw new Throwable();
        } catch (Throwable t) {
          throw Lombok.sneakyThrow(t);
        }
      }
    }
    
  • @Synchronized :加锁修饰符

    import lombok.Synchronized;
    
    public class SynchronizedExample {
      private final Object readLock = new Object();
      
      @Synchronized
      public static void hello() {
        System.out.println("world");
      }
      
      @Synchronized
      public int answerToLife() {
        return 42;
      }
      
      @Synchronized("readLock")
      public void foo() {
        System.out.println("bar");
      }
    }
    

    编译后代码:

    public class SynchronizedExample {
      private static final Object $LOCK = new Object[0];
      private final Object $lock = new Object[0];
      private final Object readLock = new Object();
      
      public static void hello() {
        synchronized($LOCK) {
          System.out.println("world");
        }
      }
      
      public int answerToLife() {
        synchronized($lock) {
          return 42;
        }
      }
      
      public void foo() {
        synchronized(readLock) {
          System.out.println("bar");
        }
      }
    }
    

5. Git Commit Template

作用:git提交规范插件

使用:

解释:

  • Type
  • feat: 新功能
  • fix: 修补bug
  • docs: 文档
  • style: 格式调整,不影响代码的变动和运行
  • refactor: 代码重构
  • perf: 性能优化
  • test: 增加测试或修改测试
  • build: 影响构建系统或外部依赖项的更改(maven,gradle,npm 等等)
  • ci: 对CI配置文件和脚本的更改
  • chore: 对非 src 和 test 目录的修改
  • revert: 回滚提交
  • Scope: 提交的影响范围,比如哪个模块
  • Short description: commit 目的的简短描述,主要介绍此次代码变更的主要内容
  • Long description: commit目的的详细描述
  • Breaking changes:重要改动
  • Closed issues: 关闭issues

6. Alibaba Java Coding Guidelines

介绍:阿里巴巴的编码规约检查插件。

使用:

7. Jrebel

jrebel是一款热部署插件,修改代码后不需要重启系统。激活链接:jrebel激活说明open in new window,热部署快捷键:CTRL+SHIFT+F9

8. IDEA自带生成测试用例插件

介绍:可以快速生成测试用例类及方法

使用:选择类或方法使用快捷键ALT+ENTER

9. GenerateAllSetter

介绍:GenerateAllSetter可以快速生成对象的set方法 。

使用:选中需要生成的对象,快捷键ALT+回车。

10. codota(Tabnine)

作用:代码提示补全插件,比IDEA自带的代码提示更加丝滑;提供类API示例

使用:

11. SequenceDiagram

介绍:快速生成时序图的插件。

使用:选中方法,右键生成时序图。

12. CodeGlance3

介绍:一款非常好用的代码地图插件,可以在右侧生成一个竖向可拖动的代码缩略区,可以快速定位代码的同时,并且提供放大镜功能

13. POJO to JSON

介绍:对象转JSON工具

14 .Key promoter X

介绍: Key promoter X是一款快捷键提示插件,可以帮助开发人员掌握常见的快捷键使用。

15. Mybatis log plugin

介绍:mybatis log plugin是一款收费插件,可以将打印的sql日志生成可执行的sql语句。破解包放在附件,在线下载链接: https://pan.baidu.com/s/1FTgtJiyzxxaNxWLyX4OgZw,密码: w7w8

16. Free Mybatis plugin

介绍:mybatis插件,提供代码生成、mapper和xml之间跳转等功能