├── .gitignore ├── README.md └── java ├── template-java-after.xml ├── template-java-afterclass.xml ├── template-java-assert.xml ├── template-java-before.xml ├── template-java-beforeclass.xml ├── template-java-builder-equals-hashcode-reflection.xml ├── template-java-builder-equals-hashcode.xml ├── template-java-formap.xml ├── template-java-ifnotnull.xml ├── template-java-ifnull.xml ├── template-java-log-debug.xml ├── template-java-log-error.xml ├── template-java-log-info.xml ├── template-java-log-msg.xml ├── template-java-log-warn.xml ├── template-java-logger.xml └── template-java-test-method.xml /.gitignore: -------------------------------------------------------------------------------- 1 | \#*# 2 | *~ 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Eclipse Templates # 2 | 3 | Eclipse comes with a bunch of code templates that can save you a lot 4 | of typing. Most notably the templates `sysout` (expands to 5 | `System.out.println()`) and `for` / `foreach` to easily iterate over a 6 | collection or an array. 7 | 8 | It is also easy to add custom templates. This repository contains some 9 | of the templates I created and which might be useful for others too. 10 | 11 | ## Java Templates ## 12 | 13 | ### Category: Logging ### 14 | 15 | #### Create SLF4J Logger #### 16 | 17 | *Name:* `logger` 18 | 19 | *Description:* Creates an [SLF4J][slf4j] logger instance with the name of the enclosing class. 20 | 21 | *Example:* 22 | 23 | This code 24 | 25 | ```java 26 | public class Foo { 27 | logger 28 | } 29 | ``` 30 | 31 | expands to 32 | 33 | ```java 34 | import org.slf4j.Logger; 35 | import org.slf4j.LoggerFactory; 36 | 37 | public class Foo { 38 | private static final Logger LOG = LoggerFactory.getLogger(Foo.class); 39 | } 40 | ``` 41 | 42 | #### Log message #### 43 | 44 | *Name:* `log` 45 | 46 | *Description:* Creates a log statement. Lets you select the level from a 47 | list of choices. 48 | 49 | *Example:* 50 | 51 | This code 52 | 53 | ```java 54 | import org.slf4j.Logger; 55 | import org.slf4j.LoggerFactory; 56 | 57 | public class Foo { 58 | private static final Logger LOG = LoggerFactory.getLogger(Foo.class); 59 | 60 | public void bar() { 61 | log 62 | } 63 | } 64 | ``` 65 | 66 | expands to 67 | 68 | ```java 69 | import org.slf4j.Logger; 70 | import org.slf4j.LoggerFactory; 71 | 72 | public class Foo { 73 | private static final Logger LOG = LoggerFactory.getLogger(Foo.class); 74 | 75 | public void bar() { 76 | LOG.debug(""); 77 | } 78 | } 79 | ``` 80 | 81 | #### Log debug message #### 82 | 83 | *Name:* `logd` 84 | 85 | *Description:* Same as `log` but with fixed DEBUG level. 86 | 87 | #### Log info message #### 88 | 89 | *Name:* `logi` 90 | 91 | *Description:* Same as `log` but with fixed INFO level. 92 | 93 | #### Log warning message #### 94 | 95 | *Name:* `logw` 96 | 97 | *Description:* Same as `log` but with fixed WARN level. 98 | 99 | #### Log error message #### 100 | 101 | *Name:* `loge` 102 | 103 | *Description:* Same as `log` but with fixed ERROR level. 104 | 105 | ### Category: JUnit Tests ### 106 | 107 | #### Test method #### 108 | 109 | *Name:* `test` 110 | 111 | *Description:* Creates a JUnit test method body annotated with `@Test` 112 | and prompts you for the test method name. 113 | 114 | This templates is an enhancement of the standard template `Test` 115 | shipped with Eclipse. Additional to the standard template, the 116 | following static imports are added: 117 | 118 | * `org.junit.Assert.*` 119 | * `org.hamcrest.Matchers.*` 120 | * `org.easymock.EasyMock.*` 121 | 122 | *Example:* 123 | 124 | This code 125 | 126 | ```java 127 | public class FooTest { 128 | } 129 | ``` 130 | 131 | expands to 132 | 133 | ```java 134 | import static org.junit.Assert.*; 135 | import static org.hamcrest.Matchers.*; 136 | import static org.easymock.Easymock.*; 137 | 138 | public class FooTest { 139 | 140 | @Test 141 | public void testX() throws Exception { 142 | } 143 | 144 | } 145 | ``` 146 | 147 | #### Test Assertion #### 148 | 149 | *Name:* `assert` 150 | 151 | *Description:* Creates a JUnit assertion body. Adds useful static imports. 152 | 153 | *Example:* 154 | 155 | This code 156 | 157 | ```java 158 | public class FooTest { 159 | 160 | @Test 161 | public void testBar() throws Exception { 162 | assert 163 | } 164 | 165 | } 166 | ``` 167 | 168 | expands to 169 | 170 | ```java 171 | import static org.junit.Assert.assertThat; 172 | import static org.hamcrest.Matchers.*; 173 | 174 | public class FooTest { 175 | 176 | @Test 177 | public void testBar() throws Exception { 178 | assertThat( , Matchers. ); 179 | } 180 | 181 | } 182 | ``` 183 | 184 | #### Test Setup #### 185 | 186 | *Name:* `before` 187 | 188 | *Description:* Creates a test setup method body. 189 | 190 | *Example:* 191 | 192 | This code 193 | 194 | ```java 195 | public class FooTest { 196 | 197 | before 198 | 199 | } 200 | ``` 201 | 202 | expands to 203 | 204 | ```java 205 | import org.junit.Before; 206 | 207 | public class FooTest { 208 | 209 | @Before 210 | public void setUp() throws Exception { 211 | 212 | } 213 | 214 | } 215 | ``` 216 | 217 | #### Test Teardown #### 218 | 219 | *Name:* `after` 220 | 221 | *Description:* Creates a test teardown method body. 222 | 223 | *Example:* 224 | 225 | This code 226 | 227 | ```java 228 | public class FooTest { 229 | 230 | after 231 | 232 | } 233 | ``` 234 | 235 | expands to 236 | 237 | ```java 238 | import org.junit.After; 239 | 240 | public class FooTest { 241 | 242 | @After 243 | public void tearDown() throws Exception { 244 | 245 | } 246 | 247 | } 248 | ``` 249 | 250 | #### Test Setup Before Class #### 251 | 252 | *Name:* `beforeclass` 253 | 254 | *Description:* Creates a test setup-before-class method body. 255 | 256 | *Example:* 257 | 258 | This code 259 | 260 | ```java 261 | public class FooTest { 262 | 263 | beforeclass 264 | 265 | } 266 | ``` 267 | 268 | expands to 269 | 270 | ```java 271 | import org.junit.BeforeClass; 272 | 273 | public class FooTest { 274 | 275 | @BeforeClass 276 | public void setUpBeforeClass() throws Exception { 277 | 278 | } 279 | 280 | } 281 | ``` 282 | 283 | #### Test Teardown After Class #### 284 | 285 | *Name:* `afterclass` 286 | 287 | *Description:* Creates a test teardown-after-class method body. 288 | 289 | *Example:* 290 | 291 | This code 292 | 293 | ```java 294 | public class FooTest { 295 | 296 | afterclass 297 | 298 | } 299 | ``` 300 | 301 | expands to 302 | 303 | ```java 304 | import org.junit.AfterClass; 305 | 306 | public class FooTest { 307 | 308 | @AfterClass 309 | public void tearDownAfterClass() throws Exception { 310 | 311 | } 312 | 313 | } 314 | ``` 315 | 316 | ### Category: Misc ### 317 | 318 | #### Iterate over Map #### 319 | 320 | *Name:* `formap` 321 | 322 | *Description:* Iterates over the entries of a `java.util.Map` with a 323 | foreach loop. 324 | 325 | *Example:* 326 | 327 | This code 328 | 329 | ```java 330 | Map myMap = new HashMap(); 331 | 332 | formap> 333 | ``` 334 | 335 | expands to 336 | 337 | ```java 338 | Map myMap = new HashMap(); 339 | 340 | for (Map.Entry< , > entry : myMap.entrySet()) { 341 | 342 | } 343 | ``` 344 | 345 | #### Check for null #### 346 | 347 | *Name:* `ifnull` 348 | 349 | *Description:* Creates a block to execute when a local variable is 350 | null. 351 | 352 | *Example:* 353 | 354 | This code 355 | 356 | ```java 357 | Integer i = null; 358 | 359 | ifnull> 360 | ``` 361 | 362 | expands to 363 | 364 | ```java 365 | Integer i = null; 366 | 367 | if (i == null) { 368 | 369 | } 370 | ``` 371 | 372 | #### Check for not-null #### 373 | 374 | *Name:* `ifnotnull` 375 | 376 | *Description:* Creates a block to execute when a local variable is 377 | not null. 378 | 379 | *Example:* 380 | 381 | This code 382 | 383 | ```java 384 | Integer i = null; 385 | 386 | ifnotnull> 387 | ``` 388 | 389 | expands to 390 | 391 | ```java 392 | Integer i = null; 393 | 394 | if (i != null) { 395 | 396 | } 397 | ``` 398 | 399 | #### Equals- / HashCode- / ToStringBuilder #### 400 | 401 | *Name:* `builder` 402 | 403 | *Description:* Creates `equals()`, `hashCode()` and `toString()` methods 404 | which make use of the builder classes from [commons-lang][lang]. When 405 | typing field names (up to 3), those are inserted accordingly in each 406 | of the 3 methods. 407 | 408 | *Example:* 409 | 410 | This code 411 | 412 | ```java 413 | public class Foo { 414 | 415 | builder 416 | 417 | } 418 | ``` 419 | 420 | expands to 421 | 422 | ```java 423 | import org.apache.commons.lang3.builder.EqualsBuilder; 424 | import org.apache.commons.lang3.builder.HashCodeBuilder; 425 | import org.apache.commons.lang3.builder.ToStringBuilder; 426 | 427 | public class Foo { 428 | 429 | @Override 430 | public boolean equals(Object obj) { 431 | if (obj == null) { return false; } 432 | if (obj == this) { return true; } 433 | if (obj.getClass() != getClass()) { return false; } 434 | Foo rhs = (Foo) obj; 435 | return new EqualsBuilder() 436 | .appendSuper(super.equals(obj)) 437 | .append(this.field1, rhs.field1) 438 | .append(this.field2, rhs.field2) 439 | .append(this.field3, rhs.field3) 440 | .isEquals(); 441 | } 442 | 443 | @Override 444 | public int hashCode() { 445 | return new HashCodeBuilder(17, 37) 446 | .append(field1) 447 | .append(field2) 448 | .append(field3) 449 | .toHashCode(); 450 | } 451 | 452 | @Override 453 | public String toString() { 454 | return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE) 455 | .append("field1", field1) 456 | .append("field2", field2) 457 | .append("field3", field3) 458 | .toString(); 459 | } 460 | 461 | } 462 | ``` 463 | 464 | #### Equals- / HashCode- / ToStringBuilder with Reflection #### 465 | 466 | *Name:* `builder equals hashcode reflection` 467 | 468 | *Description:* Creates `equals()`, `hashCode()` and `toString()` methods 469 | which make use of the builder classes from [commons-lang][lang] using 470 | their reflection-based methods. 471 | 472 | *Example:* 473 | 474 | This code 475 | 476 | ```java 477 | public class Foo { 478 | 479 | builder 480 | 481 | } 482 | ``` 483 | 484 | expands to 485 | 486 | ```java 487 | import org.apache.commons.lang3.builder.EqualsBuilder; 488 | import org.apache.commons.lang3.builder.HashCodeBuilder; 489 | import org.apache.commons.lang3.builder.ToStringBuilder; 490 | 491 | public class Foo { 492 | 493 | @Override 494 | public boolean equals(Object obj) { 495 | return EqualsBuilder.reflectionEquals(this, obj); 496 | } 497 | 498 | @Override 499 | public int hashCode() { 500 | return HashCodeBuilder.reflectionHashCode(17, 37, this); 501 | } 502 | 503 | @Override 504 | public String toString() { 505 | return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE); 506 | } 507 | 508 | } 509 | ``` 510 | 511 | ## Installation ## 512 | 513 | 514 | 515 | [slf4j]: http://www.slf4j.org/ 516 | [lang]: http://commons.apache.org/lang/ 517 | -------------------------------------------------------------------------------- /java/template-java-after.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-afterclass.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-assert.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-before.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-beforeclass.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-builder-equals-hashcode-reflection.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-builder-equals-hashcode.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-formap.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-ifnotnull.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-ifnull.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-log-debug.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-log-error.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-log-info.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-log-msg.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-log-warn.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-logger.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/template-java-test-method.xml: -------------------------------------------------------------------------------- 1 | --------------------------------------------------------------------------------