1. 云栖社区>
  2. 博客>
  3. 正文


fibojie 2015-11-28 19:25:00 浏览843


推荐Intelij IDEA,有社区版本的,可以免费使用。同时还与很多语法和编译器的支持,比如Markdown、VM、Bash、Thrift、Protobuf。




  • 插件安装
    集成开发环境中,需要安装对应的开发插件, 插件安装参照对应的集成开发环境中插件的安装方法,搜索lombok,安装。
  • maven库
  • 启用注解处理


      Finally! Hassle-free final local variables.
      会自动解析变量的返回类型,并且返回的变量列行是final类型。final String foo = example.get(0); var foo = example.get(0);

      How I learned to stop worrying and love the NullPointerException.

 public NonNullExample(@NonNull Person person) {  = person.getName();

public NonNullExample(@NonNull Person person) {   
    if (person == null) {
       throw new NullPointerException("person");
    } = person.getName();

      Automatic resource management: Call your close() methods safely with no hassle.

@Cleanup InputStream in = new FileInputStream(args[0]);
//当in变量超出作用域后,会调用对应的close方法。如果没有close方法,也可以指定对应的处理方法, @Cleanup("method name")

@Getter / @Setter
      Never write public int getFoo() {return foo;} again.

      No need to start a debugger to see your fields: Just let lombok generate a toString for you!

@ToString(callSuper=true, includeFieldNames=true)

      Equality made easy: Generates hashCode and equals implementations from the fields of your object.

@EqualsAndHashCode(exclude={"id", "shape"}) //不对id,shape属性进行比较

@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
      Constructors made to order: Generates constructors that take no arguments, one argument per final / non-null field, or one argument for every field.

      All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor!

      Immutable classes made very easy.

      The @Builder annotation produces complex builder APIs for your classes. 可以在程序中使用 Person.builder().name(“Adam Savage”).build();这样的类似的方法来构建对应的实体。

 public class BuilderExample {
   private String name;
   private int age;
   @Singular private Set<String> occupations;


    Iterable, Collection, and List (backed by a compacted unmodifiable ArrayList in the general case).
    Set, SortedSet, and NavigableSet (backed by a smartly sized unmodifiable HashSet or TreeSet in the general case).
    Map, SortedMap, and NavigableMap (backed by a smartly sized unmodifiable HashMap or TreeMap in the general case).

    ImmutableCollection and ImmutableList (backed by the builder feature of ImmutableList).
    ImmutableSet and ImmutableSortedSet (backed by the builder feature of those types).
    ImmutableMap, ImmutableBiMap, and ImmutableSortedMap (backed by the builder feature of those types).

To boldly throw checked exceptions where no one has thrown them before!


      Laziness is a virtue!使用了getter这个annotation可以在实际使用到cached的时候生成cached,同时,Lombok会自动去管理线程安全的问题,不会存在重复赋值的问题。

@Getter(lazy=true) private final double[] cached = expensive();

 private double[] expensive() {
     double[] result = new double[1000000];
     for (int i = 0; i < result.length; i++) {
       result[i] = Math.asin(i);
     return result;

Captain’s Log, stardate 24435.7: “What was that line again?”

Configuration system
Lombok, made to order: Configure lombok features in one place for your entire project or even your workspace.