您可以在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
插入测试方法时应给出
我最近在与一个非常优秀的开发人员和朋友结对编程时看到了一个类似的版本,我认为它可以成为这个列表的一个很好的补充。
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)}
为一个事件创造一切
因为在Java中创建事件有点麻烦——所有这些接口、方法和东西都要为一个事件编写——我制作了一个简单的模板来创建一个事件所需的一切。
${:import(java.util.List, java.util.LinkedList, java.util.EventListener, java.util.EventObject)}
private final List<${eventname}Listener> ${eventname}Listeners = new LinkedList<${eventname}Listener>();
public final void add${eventname}Listener(${eventname}Listener listener)
{
synchronized(${eventname}Listeners) {
${eventname}Listeners.add(listener);
}
}
public final void remove${eventname}Listener(${eventname}Listener listener)
{
synchronized(${eventname}Listeners) {
${eventname}Listeners.remove(listener);
}
}
private void raise${eventname}Event(${eventname}Args args)
{
synchronized(${eventname}Listeners) {
for(${eventname}Listener listener : ${eventname}Listeners)
listener.on${eventname}(args);
}
}
public interface ${eventname}Listener extends EventListener
{
public void on${eventname}(${eventname}Args args);
}
public class ${eventname}Args extends EventObject
{
public ${eventname}Args(Object source${cursor})
{
super(source);
}
}
如果您有共享单个eventtobject的事件,只需删除模板插入的自定义事件,并更改raise___()和____()上的适当部分。
我已经使用泛型接口和泛型类编写了一个漂亮、小巧、优雅的事件机制,但由于Java处理泛型的方式,它无法工作。=(
编辑:
1)我遇到了线程在事件发生时添加/删除侦听器的问题。List在使用时不能被修改,所以我在侦听器列表被访问或使用的地方添加了同步块,锁定在列表本身上。
我最喜欢的几个是……
1: Javadoc,插入关于该方法是Spring对象注入方法的文档。
Method to set the <code>I${enclosing_type}</code> implementation that this class will use.
*
* @param ${enclosing_method_arguments}<code>I${enclosing_type}</code> instance
2:调试窗口,创建FileOutputStream并将缓冲区的内容写入文件。
用于当你想要比较缓冲区与过去的运行(使用BeyondCompare),或者如果你不能查看缓冲区的内容(通过inspect),因为它太大…
java.io.FileOutputStream fos = new java.io.FileOutputStream( new java.io.File("c:\\x.x"));
fos.write(buffer.toString().getBytes());
fos.flush();
fos.close();
我刚刚注意到@邓肯琼斯已经有这个模板,但添加${line_selection}和使用Shift + Alt + Z是一个有用的策略。
这可能只是我正在从事的项目中一些糟糕设计的一点hack fix,但我有很多情况下,一些遗留代码正在修改AWT线程之外的Swing组件,并导致间歇性错误,所以为了快速修补这些问题,我使用:
${:import(javax.swing.SwingUtilities)}
// Ensure that any Swing components will be invoked only from the AWT thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
${line_selection}${cursor}
}
});
因此,我可以突出显示违规语句,并使用Shift + Alt + Z环绕。我把这个模板称为swinvoke。
根据所选单词创建一个新的JUnit测试用例:
这需要一个记录器(称为_logger:在这个线程中也有一个非常好的模板)。
我是这个模板的忠实粉丝,因为它使我很容易在我想到它们的那一刻就快速地创建未实现的测试用例。他们会坐在那里不及格,提醒我要测试的案子。
${:import(org.junit.Test, org.junit.Assert)}
@Test
public void fooTest() throws Throwable {
try {
${cursor}
Assert.fail("Not Implemented");
} catch (Throwable e) {
_logger.error("Failed test", e);
throw e;
}
}
要使用它,输入测试用例的名称(比如testSerializeObject),突出显示单词,并点击Ctrl + Space(或者您为代码辅助配置的任何东西)。
我最喜欢的测试用例模板是记录异常然后重新抛出它们的模板,因为我喜欢在控制台中而不是在JUnit的异常查看器中查看异常。
你喜欢系统吗?在你的测试日志文件中,你总是可以使用类似的东西:
${:import(org.junit.Test, org.junit.Assert)}
@Test
public void ${word_selection}() throws Exception {
try {
${cursor}
Assert.fail("Not Implemented");
} catch (Exception e) {
System.out.println("Failed test");
e.printStackTrace();
throw e;
}
}