Loop over fields in eclipse code template
Asked Answered
M

3

6

Can you do loops in code templates, e.g I want to print all the fields in a class. Is there a template for that?

Mcclimans answered 11/4, 2010 at 8:45 Comment(1)
I'm also looking for this. I thought gist.github.com/pascalalich/436952ccf778e9181ba8 was what I wanted but on Eclipse 4.5.1 it's not printing out the existing fields when I try to use it.Furlough
M
4

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

Mcclimans answered 8/10, 2010 at 18:48 Comment(1)
Looks like this plugin is getting more and more useful.Riehl
T
7

Assuming you're talking about Java > Editor > Templates a reference (maybe outdated) is here

This SO question useful-eclipse-java-code-templates containes some examples.

To iterate over class members using a toString() style generator you may find this useful: toString() Generator: Format Templates

Transient answered 11/4, 2010 at 10:25 Comment(1)
The link 'useful-eclipse-java-code-templates' just loops back to this question for me. I think the link was meant to go here: #1029358.Pasho
C
4

Eclipse can generate toString(), which includes a print out of all fields and their string values, if that's what you are after.

Carlos answered 11/4, 2010 at 9:2 Comment(0)
M
4

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

Mcclimans answered 8/10, 2010 at 18:48 Comment(1)
Looks like this plugin is getting more and more useful.Riehl

© 2022 - 2024 — McMap. All rights reserved.