以下模板不加说明,均为eclipse自带,或者由安装插件时自动添加的
1. 日志对象logger。例如使用slf4j可使如下模板:
${:import(org.slf4j.Logger,org.slf4j.LoggerFactory)}
private static final Logger _logger = LoggerFactory.getLogger(${enclosing_type}.class);
来自:http://stackoverflow.com/questions/1028858/useful-eclipse-java-code-templates
2. 学用流程语句之:do while
模板:
1
do
{ ${line_selection}${cursor} }
while
(${condition:var(
boolean
)});
没有其它上下文条件下的效果。 效果:
1
2
3
do
{
}
while
(condition);
3.else块:
模板:else { ${cursor} }
4.elseif 块:
模板:
else if (${condition:var(boolean)}) { ${cursor} }
5. ExceptionHandler
模板:
${:import(org.springframework.web.bind.annotation.ExceptionHandler)}@ExceptionHandler(${Exception}.class) public ${void} ${handleException}(${Exception} ${exception}) { ${line_selection}${cursor} }
6.fall-through
模板://$$FALL-THROUGH$$
7.for
(1)
for (int ${index} = 0; ${index} < ${array}.length; ${index}++) { ${line_selection}${cursor} }
(2)
for (int ${index} = 0; ${index} < ${array}.length; ${index}++) { ${array_type} ${array_element} = ${array}[${index}]; ${cursor} }
(3)
for (${iteratorType:newType(java.util.Iterator)} ${iterator} = ${collection}.iterator(); ${iterator}.hasNext(); ) { ${type:elemType(collection)} ${name:newName(type)} = (${type}) ${iterator}.next(); ${cursor} }
9. foreach
for (${iterable_type} ${iterable_element} : ${iterable}) { ${cursor} }
10. GET method
${x:import(org.springframework.web.bind.annotation.RequestMapping, org.springframework.web.bind.annotation.RequestMethod)}@RequestMapping(value="${cursor}${path}", method = RequestMethod.GET) public ${String} ${get}(${Object} ${model}) { // TODO Auto generated method stub return null; }
11.if
if (${condition:var(boolean)}) { ${line_selection}${cursor} }
12. ifelse
if (${condition:var(boolean)}) { ${cursor} } else { }
13. InitBinder method
${:import(org.springframework.web.bind.annotation.InitBinder, org.springframework.web.bind.WebDataBinder)}@InitBinder public void ${initBinder}(WebDataBinder ${binder}) { ${line_selection}${cursor} }
14.instanceof
if (${name:var} instanceof ${type}) { ${type} ${new_name} = (${type})${name}; ${cursor} }
15. lazy
if (${name:var} == null) { ${name} = new ${type}(${arguments}); ${cursor} } return ${name};
16.main
public static void main(String[] args) { ${cursor} }
17. new
${type} ${name} = new ${type}(${arguments});
18. POST method
${x:import(org.springframework.web.bind.annotation.RequestMapping, org.springframework.web.bind.annotation.RequestMethod, org.springframework.validation.BindingResult)}@RequestMapping(value="${cursor}${path}", method = RequestMethod.POST) public ${String} ${post}(${Object} ${formObject}, ${BindingResult} ${bindingResult}) { // TODO Auto generated method stub return null; }
19. private_method
private ${return_type} ${name}(${}) { ${cursor} }
20. private_static_method
private static ${return_type} ${name}(${}) { ${cursor} }
21. protected_method
protected ${return_type} ${name}(${}) { ${cursor} }
22. public_method
public ${return_type} ${name}(${}) { ${cursor} }
23. PUT method
${x:import(org.springframework.web.bind.annotation.RequestMapping, org.springframework.web.bind.annotation.RequestMethod, org.springframework.validation.BindingResult)}@RequestMapping(value="${cursor}${path}", method = RequestMethod.PUT) public ${String} ${post}(${Object} ${formObject}, ${BindingResult} ${bindingResult}) { // TODO Auto generated method stub return null; }
24.runnable
new Runnable() { public void run() { ${line_selection} } }
25.static_final
${visibility:link(public,protected,private)} static final ${type:link(String,int)} ${NAME};
26.switch
switch (${key}) { case ${value}: ${cursor} break; default: break; }
27:synchronized
synchronized (${mutex:var}) { ${line_selection} }
28.sysout
System.out.println(${word_selection}${});${cursor}
29. systrace
System.out.println("${enclosing_type}.${enclosing_method}()");
30.test
public void test${name}() throws Exception { ${cursor} }
31.Test
@${testType:newType(org.junit.Test)} public void ${testName}() throws Exception { ${staticImport:importStatic('org.junit.Assert.*')}${cursor} }
32.toarray
(${type:elemType(collection)}[]) ${collection}.toArray(new ${type}[${collection}.size()])
33.try
try { ${line_selection}${cursor} } catch (${Exception} ${exception_variable_name}) { // ${todo}: handle exception }
34.while
(1)while (${en:var(java.util.Enumeration)}.hasMoreElements()) { ${type:argType(en)} ${elem:newName(type)} = (${type}) ${en}.nextElement(); ${cursor} }
(2)while (${it:var(java.util.Iterator)}.hasNext()) { ${type:argType(it)} ${elem:newName(type)} = (${type}) ${it}.next(); ${cursor} }
(3)while (${condition:var(boolean)}) { ${line_selection}${cursor} }
以下模板来自:http://www.sproutee.com/mytemplates.xml
35.list
${:import(java.util.List, java.util.ArrayList)} List<${argType}> ${newName} = new ArrayList<${argType}>();
36.map
${:import(java.util.Map, java.util.HashMap)} Map<${argType},${argType}> ${newName} = new HashMap<${argType},${argType}>();
37 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) { log.error(e) ; } catch (IOException e) { log.error(e) ; } finally { if(in != null) in.close(); } ${cursor}