Loop over fields in eclipse code template

自闭症网瘾萝莉.ら 提交于 2019-12-10 15:38:30

问题


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?


回答1:


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




回答2:


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




回答3:


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());
}



来源:https://stackoverflow.com/questions/2616493/loop-over-fields-in-eclipse-code-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!