IDEA巧用Postfix Completion让码速起飞(小技巧)

所属分类: 软件编程 / java 阅读数: 105
收藏 0 赞 0 分享

1. 情景展示

自从做 Java 开发之后,IDEA 编辑器是不可少的。 在 IDEA 编辑器中,有很多高效的代码补全功能,尤其是 Postfix Completion 功能,可以让编写代码更加的流畅。

Postfix completion 本质上也是代码补全,它比 Live Templates 在使用上更加流畅一些,我们可以看一下下面的这张图。

 

2. 设置界面

可以通过如下的方法打开 Postfix 的设置界面,并开启 Postfix。

 

3. 常用的 Postfix 模板

3.1. boolean 变量模板

!: Negates boolean expression

//before
public class Foo {
   void m(boolean b) {
     m(b!);
   }
 }
 
//after
public class Foo {
  void m(boolean b) {
    m(!b);
  }
}

if: Checks boolean expression to be 'true'

//before
public class Foo {
  void m(boolean b) {
    b.if
  }
}

//after
public class Foo {
  void m(boolean b) {
    if (b) {

    }
  }
}

else: Checks boolean expression to be 'false'.

//before
public class Foo {
  void m(boolean b) {
    b.else
  }
}

//after
public class Foo {
  void m(boolean b) {
    if (!b) {

    }
  }
}

3.2. array 变量模板

for: Iterates over enumerable collection.

//before
public class Foo {
  void m() {
    int[] values = {1, 2, 3};
    values.for
  }
}

//after
public class Foo {
  void m() {
    int[] values = {1, 2, 3};
    for (int value : values) {

    }
  }
}

fori: Iterates with index over collection.

//before
public class Foo {
  void m() {
    int foo = 100;
    foo.fori
  }
}

//after
public class Foo {
  void m() {
    int foo = 100;
    for (int i = 0; i < foo; i++) {

    }
  }
}

3.3. 基本类型模板

opt: Creates Optional object.

//before
public void m(int intValue, double doubleValue, long longValue, Object objValue) {
 intValue.opt
 doubleValue.opt
 longValue.opt
 objValue.opt
}

//after
public void m(int intValue, double doubleValue, long longValue, Object objValue) {
 OptionalInt.of(intValue)
 OptionalDouble.of(doubleValue)
 OptionalLong.of(longValue)
 Optional.ofNullable(objValue)
}

sout: Creates System.out.println call.

//before
public class Foo {
 void m(boolean b) {
  b.sout
 }
}

//after
public class Foo {
 void m(boolean b) {
   System.out.println(b);
 }
}

3.4. Object 模板

nn: Checks expression to be not-null.

//before
public class Foo {
  void m(Object o) {
    o.nn
  }
}
//after
public class Foo {
  void m(Object o) {
    if (o != null){

    }
  }
}

null: Checks expression to be null.

//before
public class Foo {
  void m(Object o) {
    o.null
  }
}
//after
public class Foo {
  void m(Object o) {
    if (o != null){

    }
  }
}

notnull: Checks expression to be not-null.

//before
public class Foo {
  void m(Object o) {
    o.notnull
  }
}
//after
public class Foo {
  void m(Object o) {
    if (o != null){

    }
  }
}

val: Introduces variable for expression.

//before
public class Foo {
  void m(Object o) {
    o instanceof String.var
  }
}

//after
public class Foo {
  void m(Object o) {
    boolean foo = o instanceof String;
  }
}

3.5. 其他模板

new: Inserts new call for the class.

//before
Foo.new

//after
new Foo()

return: Returns value from containing method.

//before
public class Foo {
  String m() {
    "result".return
  }
}
//after
public class Foo {
  String m() {
    return "result";
  }
}
更多精彩内容其他人还在看

Spring boot将配置属性注入到bean类中

本篇文章主要介绍了Spring boot将配置属性注入到bean类中,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java正则判断日期格式是否正确的方法示例

这篇文章主要介绍了Java正则判断日期格式是否正确的方法,结合实例形式分析了Java针对日期字符串正则判断的相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

java Future 接口使用方法详解

这篇文章主要介绍了java Future 接口使用方法详解,Future接口是Java线程Future模式的实现,可以来进行异步计算的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java 读取外部资源的方法详解及实例代码

这篇文章主要介绍了Java 读取外部资源的方法详解及实例代码的相关资料,经常有读取外部资源的要求,如配置文件等等需要读取,需要的朋友可以参考下
收藏 0 赞 0 分享

Java正则表达式之split()方法实例详解

这篇文章主要介绍了Java正则表达式之split()方法,结合实例形式较为详细的分析了split方法的功能、使用方法及相关注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java 存储模型和共享对象详解

这篇文章主要介绍了Java 存储模型和共享对象详解的相关资料,对Java存储模型,可见性和安全发布的问题是起源于Java的存储结构及共享对象安全,需要的朋友可以参考下
收藏 0 赞 0 分享

Java使用正则表达式实现找出数字功能示例

这篇文章主要介绍了Java使用正则表达式实现找出数字功能,结合实例形式分析了Java针对数字的匹配查找及非数字替换操作相关实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring核心IoC和AOP的理解

本文主要介绍了Spring核心IoC和AOP的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详解Spring AOP 拦截器的基本实现

本篇文章主要介绍了详解Spring AOP 拦截器的基本实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Mybatis 中的一对一,一对多,多对多的配置原则示例代码

这篇文章主要介绍了 Mybatis 中的一对一,一对多,多对多的配置原则示例代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多