Home > DeveloperSection > Forums > Useful Eclipse Java Code Templates
naomi burke
naomi burke

Total Post:28

Points:196
Posted on    July-16-2015 10:29 PM

 Java Java 
Ratings:


 1 Reply(s)
 461  View(s)
Rate this:
You can create various Java code templates in Eclipse via the

Window->Preferences->Java -> Editor -> Templates
e.g.

sysout is expanded to:

System.out.println(${word_selection}${});${cursor}
You can activate this by typing sysout followed by CTRL+SPACE

What useful Java code templates do you currently use?
Include the name and description of it and why it's awesome.


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    July-17-2015 8:25 AM

Create Log4J logger:

${:import(org.apache.log4j.Logger)}
private static final Logger LOGGER = Logger.getLogger(${enclosing_type}.class);
It both creates the Logger with a proper category and imports it.

For those using SLF4J:

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


Read file:
${:import(java.nio.file.Files,
          java.nio.file.Paths,
          java.nio.charset.Charset,
          java.io.IOException,
          java.io.BufferedReader)}
try (BufferedReader in = Files.newBufferedReader(Paths.get(${fileName:var(String)}),
                                                 Charset.forName("UTF-8"))) {
    String line = null;
    while ((line = in.readLine()) != null) {
        ${cursor}
    }
} catch (IOException e) {
    // ${todo}: handle exception
}

Don't want to miss updates? Please click the below button!

Follow MindStick