USEFUL ECLIPSE JAVA CODE TEMPLATES

naomi burke

Total Post:28

Points:196
Posted by  naomi burke
Java 
 795  View(s)
Ratings:
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.
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Useful Eclipse Java Code Templates

    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
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!