LiveTemplate IDEA ,java语言,提供10个有用的示例
时间: 2024-06-05 10:07:54 浏览: 22
好的,以下是10个有用的Live Template示例:
1. `psvm`:创建一个public static void main(String[] args)方法。
2. `sout`:创建一个System.out.println()语句。
3. `ifn`:创建一个if(null == object)语句。
4. `fori`:创建一个for循环,迭代次数为指定的数量。
5. `iter`:创建一个增强型for循环,迭代方式为指定的集合、数组或者Iterable对象。
6. `cast`:创建一个强制转换语句。
7. `tryc`:创建一个try-catch语句块。
8. `method`:创建一个新的方法声明。
9. `field`:创建一个新的类属性声明。
10. `enum`:创建一个新的枚举常量。
以上是一些常用的Live Template示例,可以帮助您在编写Java代码时更高效地工作。您也可以根据自己的需求创建自定义的Live Template。
相关问题
LiveTemplate IDEA ,java语言,利用groovy 自定义,提供10个有用的方式
LiveTemplate是IntelliJ IDEA中的一个非常实用的功能,它可以帮助我们在编写代码时快速输入常用的代码片段。下面是Java语言中利用Groovy自定义LiveTemplate的10个有用方式:
1. 快速生成Getter和Setter方法
```groovy
groovyScript("def propertyName = _1.takeRight(_1.length() - 1).capitalize(); return ['get' + propertyName, 'set' + propertyName]")
```
2. 快速生成日志输出语句
```groovy
groovyScript("_1 = _1 ?: 'message'; return \"log.info('$1: {}', ${_2})\"")
```
3. 快速生成try-catch语句
```groovy
try {
$SELECTION$
} catch(Exception e) {
e.printStackTrace()
}
```
4. 快速生成if语句
```groovy
if ($CONDITION$) {
$THEN$
}
```
5. 快速生成for循环语句
```groovy
for (int i = 0; i < $SIZE$; i++) {
$BODY$
}
```
6. 快速生成while循环语句
```groovy
while ($CONDITION$) {
$BODY$
}
```
7. 快速生成switch语句
```groovy
switch ($VAR$) {
case $VALUE1$:
$BODY1$
break;
case $VALUE2$:
$BODY2$
break;
default:
$DEFAULT$
break;
}
```
8. 快速生成注释
```groovy
// $TODO$
```
9. 快速生成Lambda表达式
```groovy
($PARAM$) -> {
$BODY$
}
```
10. 快速生成枚举类
```groovy
enum $NAME$ {
$VALUES$
}
```
LiveTemplate IDEA ,java语言,利用groovy 生成单元测试
在 IntelliJ IDEA 中,你可以使用 Live Template 功能来快速生成单元测试代码。具体实现过程如下:
1. 打开 IntelliJ IDEA,进入 Settings -> Editor -> Live Templates 页面,点击 “+” 按钮,新建一个 Live Template。
2. 在 Abbreviation 栏中输入一个简短的缩写,例如 “test”,在 Template Text 栏中输入以下代码:
```groovy
@Test
void test$testName$() {
$END$
}
```
其中,`$testName$` 是一个变量,在使用 Live Template 时会被替换成用户输入的测试方法名。
3. 在 Applicable In 栏中,勾选 Java,并在下方的 Expression 栏中输入以下代码:
```groovy
groovyScript("_editor.selectionStart != _editor.selectionEnd", "java.lang.Boolean")
```
这段代码的作用是检查当前光标所在的位置是否有选中文本,如果有则只在选中的文本中生成测试代码,否则在光标所在行生成代码。
4. 点击 OK 按钮保存 Live Template。
5. 在 Java 源代码中输入缩写 “test”,然后按下 Tab 键,即可生成单元测试代码。在输入方法名时,可以通过 Tab 键切换到下一个变量位置,便于快速输入。
例如,在下面的代码中输入 “test” 并按下 Tab 键,即可生成一个名为 “testAddition()” 的测试方法:
```java
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
```
生成的测试代码如下:
```java
@Test
void testAddition() {
// TODO: test goes here
}
```
6. 在生成的测试方法中,补充需要测试的代码,并运行测试,即可得到测试结果。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)