有没有办法自定义 Eclipse 中的默认导入?
例如,如果我默认打开一个新的 JUnit 测试类,我会得到以下导入:
import static org.junit.Assert.*;
import org.junit.Test;
我想得到什么:
import static org.junit.Assert.*;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
不幸的是,Eclipse 在重构和创建新实体时非常缺乏代码生成的可定制性。
您可能想查看Eclipse 优化导入以包括静态导入 https://stackoverflow.com/questions/288861/eclipse-optimize-imports-to-include-static-imports有关如何使内容帮助查找预定义类中的静态方法的信息。这可能就是你真正想要的。在接受的答案中,乔伊·吉布森写道,您可以添加org.hamcrest.Matchers
to 窗口 » 首选项 » Java » 编辑器 » 内容辅助 » 收藏夹.
Another solution to the specific problem of statically importing Hamcrest methods, is to configure a Code Template named hamcrest instead. That way you can simply type ham and follow up with ctrl + space to get the import at the top.
模板应该看起来像
${staticImport:importStatic('org.hamcrest.Matchers.*')}${cursor}
一个更方便的技巧是将此模板添加到已经存在的模板中test
生成新测试方法的代码模板。如果您将此模板更改为:
@${testType:newType(org.junit.Test)}
public void ${testName}() throws Exception {
${staticImport1:importStatic('org.hamcrest.Matchers.*')}
${staticImport2:importStatic('org.junit.Assert.*')}${cursor}
}
每次创建新的测试方法时都使用它,您将永远不必关心再次手动添加 hamcrest 导入。
Image to show where you configure it:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)