您可以在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


当前回答

List_methods——为列表生成添加、删除、计数和包含的方法

public void add${listname}(${listtype} toAdd){
    get${listname}s().add(toAdd);
}

public void remove${listname}(${listtype} toRemove){
    get${listname}s().remove(toRemove);
}

public ${listtype} get${listname}(int index){
    return get${listname}s().get(index);
}

public int get${listname}Count(){
    return get${listname}s().size();
}

public boolean contains${listname}(${listtype} toFind){
    return get${listname}s().contains(toFind);
}

${cursor}

id——为简单的JPA @Id插入注释、导入、字段和getter

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

public Long getId(){
    return id;
}

${cursor}
${:import (javax.persistence.GenerationType,javax.persistence.GeneratedValue,javax.persistence.Id)}

其他回答

从向量到数组

${array_type}[] ${v:var(Vector)}Array = new ${array_type}[${v}.size()];
${v}.copyInto(${v}Array);

静态导入Hamcrest测试

如果你想使用JUnit 4.8.2的新特性(assertThat, is, hasItems,等等…),这里有一个模板来生成带有必要hamcrest导入的@Test方法。

@${testType:newType(org.junit.Test)}
public void ${testName}() throws Exception {
    // Arrange
    ${staticImport:importStatic('org.hamcrest.MatcherAssert.*','org.hamcrest.Matchers.*')}${cursor} 
    // Act

    // Assert

}

在写测试的时候,我已经用过很多次了。

什么是安排-行动-断言?

用于SWT和插件开发的内部侦听器类:

${imports:import(org.eclipse.swt.widgets.Listener)}
private class ${className} implements Listener{

    @Override
    public void handleEvent(Event e) {
        final Widget w = e.widget;
    }
}

这将打印整个对象(假设你已经初始化了一个log4j LOGGER对象):

  ${:import(org.codehaus.jackson.map.ObjectMapper)}
  // If check to avoid argument evaluation costs
  if (LOGGER.isDebugEnabled()) {
        try {
            LOGGER.debug("Object ${Object}: " + "\n"
                + new ObjectMapper().writeValueAsString(${Object}));
        } catch (JsonGenerationException e) {
            LOGGER.info(e.toString());
        } catch (JsonMappingException e) {
            LOGGER.info(e.toString());
        } catch (IOException e) {
            LOGGER.info(e.toString());
        }
  }

插入测试方法时应给出

我最近在与一个非常优秀的开发人员和朋友结对编程时看到了一个类似的版本,我认为它可以成为这个列表的一个很好的补充。

This template will create a new test method on a class, following the Given - When - Then approach from the behavior-driven development (BDD) paradigm on the comments, as a guide for structuring the code. It will start the method name with "should" and let you replace the rest of the dummy method name "CheckThisAndThat" with the best possible description of the test method responsibility. After filling the name, TAB will take you straight to the // Given section, so you can start typing your preconditions.

我把它映射到三个字母“tst”,描述为“测试方法应该在什么时候给出”;)

我希望你能像我看到它时一样觉得它很有用:

@Test
public void should${CheckThisAndThat}() {
    Assert.fail("Not yet implemented");
    // Given
    ${cursor}

    // When


    // Then

}${:import(org.junit.Test, org.junit.Assert)}