29 Mart 2013 Cuma

Useful Eclipse Code Templates


Eclipse has a feature called 'Code Templates' that allows to write common code patterns/snippets quickly. As a simple example, you can write a loop only writing two or three characters and then hitting enter. It is also possible to create and use more complex templates. In this article I will share 3 custom templates that I found very useful and use frequently.

If you want to read further about code templates look at this article at DZone:

This template generates a logger field definiton for the enclosing class.

Template

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

Generated Code

private static final Logger logger = LoggerFactory.getLogger(EnclosingClass.class);


Iterate Over Map.entrySet()

Template

${:import(java.util.Map.Entry)}
for (Entry<${keyType:argType(map, 0)}, ${valueType:argType(map, 1)}> ${entry} : ${map:var(java.util.Map)}.entrySet())
{
    ${keyType} ${key} = ${entry}.getKey();
    ${valueType} ${value} = ${entry}.getValue();
    ${cursor}
}

Generated Code

for (Entry<String, String> entry : properties.entrySet())
{
    String key = entry.getKey();
    String value = entry.getValue();
    |
}
Screenshot


JUnit Test Method

Template

@${testType:newType(org.junit.Test)}
public void test${name}() throws Throwable
{
    ${staticImport:importStatic('org.junit.Assert.*')}${cursor} 
}

Generated Code

@Test
public void testname() throws Throwable
{
    |   
}


More...

Want more? See answers to following StackOverflow question:



Hiç yorum yok:

WSO2 ESB + ClientAccesPolicy.xml + CrossDomain.xml

ClientAccesPolicy.xml and CrossDomain.xml files have to be found at root of your services Silverlight and Flex clients to be able to acce...