您可以在Eclipse中创建各种Java代码模板

窗口>首选项> Java >编辑器>模板

e.g.

Sysout扩展为:

System.out.println(${word_selection}${});${cursor}

您可以通过输入sysout加上CTRL+SPACE来激活它

您目前使用哪些有用的Java代码模板?包括它的名称和描述,以及为什么它很棒。

我正在寻找一个模板的原始/新颖的使用,而不是一个内置的现有功能。

创建Log4J日志记录器 从显示中获取swt颜色 Syncexec - Eclipse框架 单例模式/Enum单例生成 Readfile 常量 Traceout 格式字符串 注释代码审查 字符串格式 尝试最终锁定 消息格式i18n和log Equalsbuilder Hashcodebuilder Spring对象注入 创建FileOutputStream


当前回答

对于代码生产来说没什么特别的,但是对于代码审查来说非常有用

我有我的模板coderev low/med/high执行以下操作

/**
 * Code Review: Low Importance
 * 
 *
 * TODO: Insert problem with code here 
 *
 */

然后在Tasks视图中-将显示我想在会议期间提出的所有代码审查注释。

其他回答

我的良人中,有一人是弟兄。

for (${iterable_type} ${iterable_element} : ${iterable}) {
    ${cursor}
}

还有traceout,因为我经常用它来跟踪:

System.out.println("${enclosing_type}.${enclosing_method}()");

我刚刚想到了另一个,有一天在网上找到了它,const:

private static final ${type} ${name} = new ${type} ${cursor};

strf ->字符串。Format ("msg", args)非常简单,但节省了一些输入。

String.format("${cursor}",)

一种新的JUnit测试方法:

 @${testType:newType(org.junit.Test)}
 public void ${testname}() throws Exception {
     ${staticImport:importStatic('org.junit.Assert.*')}${cursor}

     String expected = "" ;
     String actual = "" ;

     Assert.assertEquals(expected, actual);
}

我看到了一个创建基本测试类的模板的答案,如果你更喜欢这种方法,这里有一些单独的调用:

使用@Before import创建setUp方法

${:import(org.junit.Before)}
@Before
public final void setUp() {
  ${cursor}
}

使用@Test import创建新的测试方法

${:import(org.junit.Test)}
@Test
public final void test${newName} () {
${cursor}
}

我使用以下代码来帮助在类型和dto之间进行JAXB转换:

将现有变量转换为返回值类型的模板(与形参一起使用)

${return_type} ${name} = null;

if (${var} != null) {
    ${name} = new ${return_type}();
    ${cursor}
}
return ${name};

对于log,在成员变量中添加一个有用的小调。

private static Log log = LogFactory.getLog(${enclosing_type}.class);