Intellij IDEA使用技巧,可以让你的代码飞起来!

开发 前端
Java开发过程中往往需要编写固定格式的代码,比如声明私有变量、logger或者bean等。对于这种小规模的代码生成,我们可以使用IDEA提供的Live Templates功能。

1 背景

Java开发过程中往往需要编写固定格式的代码,比如声明私有变量、logger或者bean等。对于这种小规模的代码生成,我们可以使用IDEA提供的Live Templates功能。一开始我以为它只是一个简单的Code Snippet,后来发现它支持变量函数配置,可以支持非常复杂的代码生成。下面介绍一下Live Templates的用法。

2 基本使用

IDEA自带很多常用的动态模板,在Java代码中输入fori,回车会出现:

for (int i = 0; i < ; i++) {

}
  • 1.
  • 2.
  • 3.

按 T​ab 跳入每个空白并手动填写值。

更多 IDEA 操作如下:

public class Example {
    // geti 生成单例语句
    public static Example getInstance() {
        return new Example();
    }

    // prsf 生成 private static final
    private static final ...;

    // psf 生成 public static final
    public static final ...;

    // psfi 生成 public static final int
    public static final int ...;

    // psfs 生成 public static final String
    public static final String ...;

    // main 或者 psvm 生成 main 函数
    public static void main(String[] args) {
        // fori 生成 for 循环
        for (int i = 0; i < 10; i++) {

        }

        // C 生成 Callable
        Callable<Object> callable = new Callable<Object>() {
            public Object call() throws Exception {

            }
        };

        // I 生成遍历语句
        for (Object o :) {

        }

        // ifn 生成 if null 语句
        if (callable == null) {

        }

        // inn 生成 if not null 语句
        if (callable != null) {

        }

        // inst 生成 instanceof 语句
        if (callable instanceof Object) {
            Object o = (Object) callable;

        }

        // itar 生成数组遍历语句
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];

        }

        // itco 生成 java.util.Collection 遍历语句
        for (Iterator iterator = collection.iterator(); iterator.hasNext(); ) {
            Object next =  iterator.next();

        }

        // iten 生成 java.util.Enumeration 遍历语句
        while (enumeration.hasMoreElements()) {
            Object nextElement = enumeration.nextElement();

        }

        // iter 生成 Iterable 或 数组 遍历语句
        for (String arg : args) {

        }

        // itit 生成 java.util.Iterator 遍历语句
        while (iterator.hasNext()) {
            Object next = iterator.next();

        }

        // itli 生成 java.util.List 遍历语句
        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);

        }

        // ittok 生成遍历 token 的语句
        for (StringTokenizer stringTokenizer = new StringTokenizer(); stringTokenizer.hasMoreTokens(); ) {
            String s = stringTokenizer.nextToken();

        }

        // lazy 生成延迟初始化语句
        if (callable == null) {
            callable = new Callable<Object>();
        }

        // lst 生成获取数组最后一个元素语句
        args[args.length - 1];

        // mn 生成 Math.min
        min = Math.min(min, max);

        // mx 生成 Math.max
        max = Math.max(max, min);

        // ritar 生成降序遍历数组的语句
        for (int i = args.length - 1; i >= 0; i--) {
            String arg = args[i];

        }

        // RL 生成 ReadWriteLock.readLock()
        ReadWriteLock.readLock().lock();
        try {

        } finally {
            ReadWriteLock.readLock().unlock();
        }

        // serr 生成 System.err.println()
        System.err.println();

        // serrc 生成 System.err::println
        System.err::println;

        // souf 生成 System.out.printf("")
        System.out.printf("");

        // sout 生成 System.out.println()
        System.out.println();

        // soutc 生成 System.out::println
        System.out::println;

        // soutm 生成打印当前类和方法的语句
        System.out.println("Example.main");

        // soutp 生成打印当前方法的参数名字和值
        System.out.println("args = " + Arrays.deepToString(args));

        // soutv 生成打印一个值的语句
        System.out.println("callable = " + callable);

        // St 生成 String
        String ;

        // thr 生成 throw new
        throw new;

        // toar 生成 java.util.Collection 元素到数组的语句
        .toArray(new Object[0]);

        // WL 生成 WriteLock.writeLock() 语句
        ReentrantReadWriteLock.WriteLock.writeLock().lock();
        try {

        } finally {
            ReentrantReadWriteLock.WriteLock.writeLock().unlock();
        }

    }
}
  • 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.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.

3 自定义模板

毕竟官方内置的模板不能满足我们个人编码风格的需求。Live Templates 提供了可变函数供我们自定义。

简单用法

添加自定义模板,首先需要填写触发词(即Abbreviation),描述可选,然后定义模板的上下文,点击define选择Java,这样编辑Java的时候就会触发当前模板,定义好上下文后,就可以填写模板了。

比如定义一个私有变量:

通过输入 privateField 就可以输出以下代码:

/**
 * $COMMENT$
 */
@Getter
@Setter
private $TYPE$ $NAME$;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

模板支持定义变量,$$包围的字符代表一个变量。$END$ 是一个特殊的预定义变量,表示光标最后跳转的位置。每个变量的位置都可以跳转到。

4 进阶用法

如果你用过vim的Code Sinppet插件,你会发现函数可以在模板中执行,当然强大的Live Templates也支持,IDEA可以感知代码的语义,比如参数当前编辑的功能。但这就是让我们玩得开心的原因。我们从易到难探索模板函数的功能。

前面我们提到的变量可以绑定到函数上,配置方法如上图所示。

  • 快速声明变量

声明变量是一个常见的操作,尤其是需要声明需要注解的变量时。注释的时候,这些代码写起来很枯燥。这是我定义的模板:

  • logger 的快速声明

明 logger 也是一个常见的操作。现在我们使用一个函数 className() 来实现。顾名思义,它的作用就是返回当前的类名。

5 总结

上面我们简单介绍了常用的模板函数。其实IDEA还有很多其他的模板函数。有关详细信息,请参阅创建和编辑模板变量。IDEA 是一个非常强大的工具。善用工具可以大大提高工作效率,把精力集中在关键的事情上,而不是把时间浪费在写重复的代码上。一些更高级的用法还有待发现。好好利用它,也可以省下很多重复写代码的时间。


责任编辑:华轩 来源: 今日头条
相关推荐

2011-04-13 10:51:58

MATLAB

2020-09-29 07:54:05

Express 飞起

2022-09-02 08:21:24

idea插件

2024-11-25 18:00:00

C#代码编程

2021-01-04 15:11:57

开发 IDEA代码

2024-11-27 09:46:34

2019-11-05 10:35:57

SpringBoot调优Java

2024-06-12 12:28:23

2021-07-13 07:52:03

SQL面试COUNT(*)

2011-05-20 11:12:01

数据库DB2优化

2013-01-07 09:34:43

CodeLoveBAT

2011-02-25 08:39:11

QFabric数据中心Juniper

2025-01-17 09:23:31

2011-05-11 11:32:35

数据库DB2优化技巧

2016-01-19 17:03:59

数据中心网络华为

2019-03-25 08:05:35

Elasticsear优化集群

2023-03-31 15:10:32

PythonVSCode程序员

2011-09-27 13:25:05

Web

2016-05-11 09:18:21

AWS云数据仓库Redshift

2022-10-09 18:14:31

订单系统分库分表
点赞
收藏

51CTO技术栈公众号