寻找有用的 Eclipse Java 代码模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1028858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Seeking useful Eclipse Java code templates
提问by Jon
You can create various Java code templates in Eclipse via
您可以通过以下方式在 Eclipse 中创建各种 Java 代码模板
Window > Preferences > Java > Editor > Templates
窗口 > 首选项 > Java > 编辑器 > 模板
e.g.
例如
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? Include the name and description of it and why it's awesome.
您目前使用哪些有用的 Java 代码模板?包括它的名称和描述,以及为什么它很棒。
I am looking for an original/novel use of a template rather than a built-in existing feature.
我正在寻找模板的原始/新颖用途,而不是内置的现有功能。
- Create Log4J logger
- Get swt color from display
- Syncexec - Eclipse Framework
- Singleton Pattern/Enum Singleton Generation
- Readfile
- Const
- Traceout
- Format String
- Comment Code Review
- String format
- Try Finally Lock
- Message Format i18n and log
- Equalsbuilder
- Hashcodebuilder
- Spring Object Injection
- Create FileOutputStream
- 创建 Log4J 记录器
- 从显示中获取 swt 颜色
- Syncexec - Eclipse 框架
- 单例模式/枚举单例生成
- 读取文件
- 常量
- 追踪
- 格式字符串
- 评论代码
- 字符串格式
- 尝试最后锁定
- 消息格式 i18n 和日志
- 平等建设者
- 哈希码生成器
- Spring 对象注入
- 创建文件输出流
采纳答案by Robert Munteanu
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
日志4J 2
${:import(org.apache.logging.log4j.LogManager,org.apache.logging.log4j.Logger)}
private static final Logger LOG = LogManager.getLogger(${enclosing_type}.class);
Log4J
日志4J
${:import(org.apache.log4j.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);
来源。
JUL
七月
${:import(java.util.logging.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class.getName());
回答by cgp
For log
, a helpful little ditty to add in the member variable.
对于log
,添加到成员变量中的有用小曲。
private static Log log = LogFactory.getLog(${enclosing_type}.class);
回答by Artem Barger
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:
和traceout,因为我经常使用它进行跟踪:
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};
回答by Manuel Selva
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;
}
回答by Jon
Some additional templates here: Link I- Link II
I like this one:
我喜欢这个:
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
}
回答by ist_lion
Nothing fancy for code production - but quite useful for code reviews
代码生产没什么特别的 - 但对代码非常有用
I have my template coderev low/med/high do the following
我的模板 coderev 低/中/高执行以下操作
/**
* Code Review: Low Importance
*
*
* TODO: Insert problem with code here
*
*/
And then in the Tasks view - will show me all of the code review comments I want to bring up during a meeting.
然后在“任务”视图中 - 将显示我想在会议期间提出的所有代码意见。
回答by skaffman
I like a generated class comment like this:
我喜欢这样生成的类评论:
/**
* I...
*
* $Id$
*/
The "I..." immediately encourages the developer to describe what the class does. I does seem to improve the problem of undocumented classes.
“我...”立即鼓励开发人员描述该类的作用。我似乎确实改善了无证类的问题。
And of course the $Id$ is a useful CVS keyword.
当然,$Id$ 是一个有用的 CVS 关键字。
回答by Mario Ortegón
I use this for MessageFormat (using Java 1.4). That way I am sure that I have no concatenations that are hard to extract when doing internationalization
我将它用于 MessageFormat(使用 Java 1.4)。这样我确信在进行国际化时我没有难以提取的连接
i18n
国际化
String msg = "${message}";
Object[] params = {${params}};
MessageFormat.format(msg, params);
Also for logging:
也用于日志记录:
log
日志
if(logger.isDebugEnabled()){
String msg = "${message}"; //NLS-1
Object[] params = {${params}};
logger.debug(MessageFormat.format(msg, params));
}
回答by pjp
strf -> String.format("msg", args)
pretty simple but saves a bit of typing.
strf -> String.format("msg", args)
非常简单,但节省了一些打字的时间。
String.format("${cursor}",)
回答by Scott Stanchfield
A little tip on sysout -- I like to renamed it to "sop". Nothing else in the java libs starts with "sop" so you can quickly type "sop" and boom, it inserts.
关于 sysout 的一个小技巧——我喜欢将它重命名为“sop”。java libs 中没有其他内容以“sop”开头,因此您可以快速键入“sop”并插入。