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


当前回答

这里有更多的模板。

包括:

从特定的日期创建一个日期对象 创建一个新的泛型数组列表 日志设置 指定级别的日志 创建一个新的通用HashMap 遍历映射,打印键和值 使用SimpleDateFormat解析时间 逐行读取文件 记录并重新抛出捕获的异常 打印代码块的执行时间 创建定时定时器 将字符串写入文件

其他回答

日志记录器声明的模板很棒。

我还为我经常使用的日志级别创建了linfo、ldebug、lwarn和lerror。

lerror:

logger.error(${word_selection}${});${cursor}

我使用以下模板进行Android开发:

详细(日志)

Log.v(TAG, ${word_selection}${});${cursor}

调试(Logd)

Log.d(TAG, ${word_selection}${});${cursor}

(信息的人类学)

Log.i(TAG, ${word_selection}${});${cursor}

Warn (Logw)

Log.w(TAG, ${word_selection}${});${cursor}

错误(包厢)

Log.e(TAG, ${word_selection}${});${cursor}

Assert (Loga)

Log.a(TAG, ${word_selection}${});${cursor}

TAG是我在每个活动中定义的常量。

Bean属性

private ${Type} ${property};

public ${Type} get${Property}() {
    return ${property};
}

public void set${Property}(${Type} ${property}) {
    ${propertyChangeSupport}.firePropertyChange("${property}", this.${property},     this.${property} = ${property});
}

PropertyChangeSupport

private PropertyChangeSupport ${propertyChangeSupport} = new PropertyChangeSupport(this);${:import(java.beans.PropertyChangeSupport,java.beans.PropertyChangeListener)}
public void addPropertyChangeListener(PropertyChangeListener listener) {
  ${propertyChangeSupport}.addPropertyChangeListener(listener);
}

public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
  ${propertyChangeSupport}.addPropertyChangeListener(propertyName, listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
  ${propertyChangeSupport}.removePropertyChangeListener(listener);
}

public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
  ${propertyChangeSupport}.removePropertyChangeListener(propertyName, listener);
}

在GUI线程上调用代码

我将下面的模板绑定到快捷方式slater,以便在GUI线程上快速分派代码。

${:import(javax.swing.SwingUtilities)}
SwingUtilities.invokeLater(new Runnable() {      
      @Override
      public void run() {
        ${cursor}
      }
    });

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)}