寻求有用的Eclipse Java代码模板[关闭]

本文翻译自:Seeking useful Eclipse Java code templates [closed]

You can create various Java code templates in Eclipse via 您可以通过Eclipse在Eclipse中创建各种Java代码模板

Window > Preferences > Java > Editor > Templates 窗口>首选项> Java>编辑器>模板

eg 例如

sysout is expanded to: sysout扩展为:

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

You can activate this by typing sysout followed by CTRL+SPACE 您可以通过键入sysout然后按CTRL+SPACE来激活它

What useful Java code templates do you currently use? 您目前使用哪些有用的Java代码模板? Include the name and description of it and why it's awesome. 包括它的名称和描述以及为什么它很棒。

I am looking for an original/novel use of a template rather than a built-in existing feature. 我正在寻找原型/新颖的模板使用而不是内置的现有功能。

  • Create Log4J logger 创建Log4J记录器
  • Get swt color from display 从显示中获取swt颜色
  • Syncexec - Eclipse Framework Syncexec - Eclipse框架
  • Singleton Pattern/Enum Singleton Generation Singleton Pattern / Enum Singleton Generation
  • Readfile READFILE
  • Const 常量
  • Traceout Traceout
  • Format String 格式字符串
  • Comment Code Review 评论代码评论
  • String format 字符串格式
  • Try Finally Lock 尝试最后锁定
  • Message Format i18n and log 消息格式i18n和日志
  • Equalsbuilder Equalsbuilder
  • Hashcodebuilder Hashcodebuilder
  • Spring Object Injection 弹簧对象注入
  • Create FileOutputStream 创建FileOutputStream

#1楼

参考:https://stackoom.com/question/4JeU/寻求有用的Eclipse-Java代码模板-关-闭


#2楼

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

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

#3楼

One of my beloved is foreach : 我心爱的人之一是foreach

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

And traceout , since I'm using it a lot for tracking: 并且跟踪 ,因为我正在使用它进行跟踪:

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

I just thought about another one and have found it over the Internet some day, const : 我只想到另一个,并且有一天在互联网上找到它, const

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

#4楼

The following code templates will both create a logger and create the right imports, if needed. 如果需要,以下代码模板将创建记录器并创建正确的导入。

SLF4J SLF4J

${:import(org.slf4j.Logger,org.slf4j.LoggerFactory)}
private static final Logger LOG = LoggerFactory.getLogger(${enclosing_type}.class);

Log4J 2 Log4J 2

${:import(org.apache.logging.log4j.LogManager,org.apache.logging.log4j.Logger)} 
private static final Logger LOG = LogManager.getLogger(${enclosing_type}.class); 

Log4J Log4J的

${:import(org.apache.log4j.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);

Source . 来源 。

JUL JUL

${:import(java.util.logging.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class.getName());

#5楼

Get an SWT color from current display: 从当前显示中获取SWT颜色:

Display.getCurrent().getSystemColor(SWT.COLOR_${cursor})

Suround with syncexec 与syncexec共存

PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable(){
    public void run(){
        ${line_selection}${cursor}
    }
});

Use the singleton design pattern: 使用单例设计模式:

/**
 * The shared instance.
 */
private static ${enclosing_type} instance = new ${enclosing_type}();

/**
 * Private constructor.
 */
private ${enclosing_type}() {
    super();
}

/**
 * Returns this shared instance.
 *
 * @returns The shared instance
 */
public static ${enclosing_type} getInstance() {
    return instance;
}

#6楼

Some additional templates here: Link I - Link II 这里还有一些额外的模板: Link I - Link II

I like this one: 我喜欢这一个:

readfile ReadFile的

 ${:import(java.io.BufferedReader,  
           java.io.FileNotFoundException,  
           java.io.FileReader,  
           java.io.IOException)}  
 BufferedReader in = null;  
 try {  
    in = new BufferedReader(new FileReader(${fileName}));  
    String line;  
    while ((line = in.readLine()) != null) {  
       ${process}  
    }  
 }  
 catch (FileNotFoundException e) {  
    logger.error(e) ;  
 }  
 catch (IOException e) {  
    logger.error(e) ;  
 } finally {  
    if(in != null) in.close();  
 }  
 ${cursor} 

UPDATE : The Java 7 version of this template is: 更新 :此模板的Java 7版本是:

${:import(java.nio.file.Files,
          java.nio.file.Paths,
          java.nio.charset.Charset,
          java.io.IOException,
          java.io.BufferedReader)}
try (BufferedReader in = Files.newBufferedReader(Paths.get(${fileName:var(String)}),
                                                 Charset.forName("UTF-8"))) {
    String line = null;
    while ((line = in.readLine()) != null) {
        ${cursor}
    }
} catch (IOException e) {
    // ${todo}: handle exception
}

你可能感兴趣的:(java,eclipse,templates,ide,code-generation)