Created
October 20, 2012 21:42
-
-
Save mnuessler/3924920 to your computer and use it in GitHub Desktop.
Templates for the Eclipse IDE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-members" deleted="false" description="tear down after test" enabled="true" name="after">@${afterType:newType(org.junit.After)} | |
public void tearDown() throws Exception { | |
${cursor} | |
}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-members" deleted="false" description="tear down after test class" enabled="true" name="afterclass">@${afterType:newType(org.junit.AfterClass)} | |
public void tearDownAfterClass() throws Exception { | |
${cursor} | |
}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-statements" deleted="false" description="assert that" enabled="true" name="assert">${matchersImport:importStatic('org.hamcrest.Matchers.*')}${assertThatImport:importStatic('org.junit.Assert.assertThat')}assertThat(${word_selection}${}, ${matcher:newType(org.hamcrest.Matchers)}.${cursor});</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-members" deleted="false" description="set up before test" enabled="true" name="before">@${beforeType:newType(org.junit.Before)} | |
public void setUp() throws Exception { | |
${cursor} | |
}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-members" deleted="false" description="set up before test class" enabled="true" name="beforeclass">@${beforeType:newType(org.junit.BeforeClass)} | |
public void setUpBeforeClass() throws Exception { | |
${cursor} | |
}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-members" deleted="false" description="generate equals, hashCode and toString with builder by reflection" enabled="true" name="builder equals hashcode reflection">${importEqualsBuilder:import(org.apache.commons.lang3.builder.EqualsBuilder)}@Override | |
public boolean equals(Object obj) { | |
return EqualsBuilder.reflectionEquals(this, obj); | |
} | |
${importHashCodeBuilder:import(org.apache.commons.lang3.builder.HashCodeBuilder)}@Override | |
public int hashCode() { | |
return HashCodeBuilder.reflectionHashCode(${17}, ${37}, this); | |
} | |
${importToStringBuilder:import(org.apache.commons.lang3.builder.ToStringBuilder, org.apache.commons.lang3.builder.ToStringStyle)}@Override | |
public String toString() { | |
return ToStringBuilder.reflectionToString(this, ToStringStyle.${DEFAULT_STYLE}); | |
} | |
${cursor}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-members" deleted="false" description="generate equals, hashCode and toString with builder" enabled="true" name="builder equals hashcode">${importEqualsBuilder:import(org.apache.commons.lang3.builder.EqualsBuilder)}@Override | |
public boolean equals(Object obj) { | |
if (obj == null) { return false; } | |
if (obj == this) { return true; } | |
if (obj.getClass() != getClass()) { return false; } | |
${type:enclosing_type} rhs = (${type}) obj; | |
return new EqualsBuilder() | |
.appendSuper(super.equals(obj)) | |
.append(this.${field1:field}, rhs.${field1}) | |
.append(this.${field2:field}, rhs.${field2}) | |
.append(this.${field3:field}, rhs.${field3})${cursor} | |
.isEquals(); | |
} | |
${importHashCodeBuilder:import(org.apache.commons.lang3.builder.HashCodeBuilder)}@Override | |
public int hashCode() { | |
return new HashCodeBuilder(${17}, ${37}) | |
.append(${field1}) | |
.append(${field2}) | |
.append(${field3}) | |
.toHashCode(); | |
} | |
${importToStringBuilder:import(org.apache.commons.lang3.builder.ToStringBuilder)}${importToStringStyle:import(org.apache.commons.lang3.builder.ToStringStyle)}@Override | |
public String toString() { | |
return new ToStringBuilder(this, ToStringStyle.${DEFAULT_STYLE}) | |
.append("${field1}", ${field1}) | |
.append("${field2}", ${field2}) | |
.append("${field3}", ${field3}) | |
.toString(); | |
}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-statements" deleted="false" description="iterate over map" enabled="true" name="for">for (${iteratorType:newType(java.util.Map)}.Entry<${argType0:argType(map, 0)}, ${argType1:argType(map, 1)}> ${entry} : ${map:localVar(java.util.Map)}.entrySet()) { | |
${argType0} key = ${entry}.getKey(); | |
${argType1} value = ${entry}.getValue(); | |
${cursor} | |
} | |
</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-statements" deleted="false" description="if local var != null block" enabled="true" name="ifnotnull">if (${localVar} != null) { | |
${cursor} | |
}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-statements" deleted="false" description="if local var == null block" enabled="true" name="ifnull">if (${localVar} == null) { | |
${cursor} | |
}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-statements" deleted="false" description="log debug message" enabled="true" name="logd">${log:field(org.slf4j.Logger)}.debug("${msg}"${}); | |
${cursor}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-statements" deleted="false" description="log error message" enabled="true" name="loge">${log:field(org.slf4j.Logger)}.error("${msg}"${}); | |
${cursor}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-statements" deleted="false" description="log info message" enabled="true" name="logi">${log:field(org.slf4j.Logger)}.info("${msg}"${}); | |
${cursor}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-statements" deleted="false" description="log message" enabled="true" name="log">${log:field(org.slf4j.Logger)}.${level:link(debug, info, warn, error)}("${msg}"${}); | |
${cursor}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-statements" deleted="false" description="log warning message" enabled="true" name="logw">${log:field(org.slf4j.Logger)}.warn("${msg}"${}); | |
${cursor}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-members" deleted="false" description="slf4j logger" enabled="true" name="logger">private static final Logger LOG = LoggerFactory.getLogger(${enclosing_type}.class);${:import(org.slf4j.Logger, org.slf4j.LoggerFactory)}${cursor}</template></templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-members" deleted="false" description="test method (JUnit 4)" enabled="true" id="org.eclipse.jdt.ui.templates.test_junit4" name="test">@${testType:newType(org.junit.Test)} | |
public void ${testName}() throws Exception { | |
${assertImport:importStatic('org.junit.Assert.*')}${matchersImport:importStatic('org.hamcrest.Matchers.*')}${mockImport:importStatic('org.easymock.EasyMock.*')}${cursor} | |
}</template></templates> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Eclipse Templates
Eclipse comes with a bunch of code templates that can save you a lot
of typing. Most notably the templates
sysout
(expands toSystem.out.println()
) andfor
/foreach
to easily iterate over acollection or an array.
It is also easy to add custom templates. This repository contains some
of the templates I created and which might be useful for others too.
Java Templates
Category: Logging
Create SLF4J Logger
Name:
logger
Description: Creates an SLF4J logger instance with the name of the enclosing class.
Example:
This code
expands to
Log message
Name:
log
Description: Creates a log statement. Lets you select the level from a
list of choices.
Example:
This code
expands to
Log debug message
Name:
logd
Description: Same as
log
but with fixed DEBUG level.Log info message
Name:
logi
Description: Same as
log
but with fixed INFO level.Log warning message
Name:
logw
Description: Same as
log
but with fixed WARN level.Log error message
Name:
loge
Description: Same as
log
but with fixed ERROR level.Category: JUnit Tests
Test method
Name:
test
Description: Creates a JUnit test method body annotated with
@Test
and prompts you for the test method name.
This templates is an enhancement of the standard template
Test
shipped with Eclipse. Additional to the standard template, the
following static imports are added:
org.junit.Assert.*
org.hamcrest.Matchers.*
org.easymock.EasyMock.*
Example:
This code
expands to
Test Assertion
Name:
assert
Description: Creates a JUnit assertion body. Adds useful static imports.
Example:
This code
expands to
Test Setup
Name:
before
Description: Creates a test setup method body.
Example:
This code
expands to
Test Teardown
Name:
after
Description: Creates a test teardown method body.
Example:
This code
expands to
Test Setup Before Class
Name:
beforeclass
Description: Creates a test setup-before-class method body.
Example:
This code
expands to
Test Teardown After Class
Name:
afterclass
Description: Creates a test teardown-after-class method body.
Example:
This code
expands to
Category: Misc
Iterate over Map
Name:
formap
Description: Iterates over the entries of a
java.util.Map
with aforeach loop.
Example:
This code
expands to
Check for null
Name:
ifnull
Description: Creates a block to execute when a local variable is
null.
Example:
This code
expands to
Check for not-null
Name:
ifnotnull
Description: Creates a block to execute when a local variable is
not null.
Example:
This code
expands to
Equals- / HashCode- / ToStringBuilder
Name:
builder
Description: Creates
equals()
,hashCode()
andtoString()
methodswhich make use of the builder classes from commons-lang. When
typing field names (up to 3), those are inserted accordingly in each
of the 3 methods.
Example:
This code
expands to
Equals- / HashCode- / ToStringBuilder with Reflection
Name:
builder equals hashcode reflection
Description: Creates
equals()
,hashCode()
andtoString()
methodswhich make use of the builder classes from commons-lang using
their reflection-based methods.
Example:
This code
expands to
Installation