Seeking Useful Eclipse Java Code Templates

Seeking useful Eclipse Java code templates

The following code templates will both create a logger and create the right imports, if needed.

SLF4J

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

Log4J 2

${:import(org.apache.logging.log4j.LogManager,org.apache.logging.log4j.Logger)} 
private static final Logger LOG = LogManager.getLogger(${enclosing_type}.class);

Log4J

${:import(org.apache.log4j.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);

Source.

JUL

${:import(java.util.logging.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class.getName());

java code templates eclipse

Here i am assuming that you are trying to import java templates

  • If xml file begins with <?xml version="1.0" encoding="UTF-8" standalone="no"?><templates>
    then it is for 'Code Templates'
    Location: Windows -> Preferences -> Java -> Code Style -> Code Templates

  • If xml file begins with

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="12">
<profile kind="CodeFormatterProfile"

then these are for Formatter

Location: Windows -> Preferences -> Java -> Code Style -> Formatter

  • If xml file begins with
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="2">
<profile kind="CleanUpProfile"

Location: Windows -> Preferences -> Java -> Code Style -> Clean Up

  • For .epf files, select File -> Import -> General -> Preferences (now select file) and follow the steps.

Loop over fields in eclipse code template

Since I didn't find what I was looking for, I put it in my plugin. It supports variations.
If you have class called Company with fields companyName, companyType, etc. it will generate print statements as shown below if you select logger.debug variation :

if (logger.isDebugEnabled()) {
logger.debug("Company Name " + company.getCompanyName());
logger.debug("Company Type " + company.getCompanyType());
}

template-variation

eclipse code templates - indentation

I don't think you need to concentrate on templates for this. You can use Formatter for this.

Create your own formatter in Windows > Preferences > Java > Code Style > Formatter and enable Save Actions in Windows > Preferences > Java > Editor > Save Actions (Select Format Source code and Format all lines options) and here you choose your formatter created before.

Once you insert the logger template editor becomes dirty then save the editor(press Ctrl + S) then automatic indentation will happen as per the settings in formatter.

Refer this and this for formatter creation. In Indentation tab use the number of space you like. I used Tab Policy = Spaces only and Indentation size = 4.

eclipse - Code Templates what are the variables available?

Using

${enclosing_type}, ${enclosing_method} 

Will solve your problem.



Related Topics



Leave a reply



Submit