How to Write Your Own Template
Here are the steps that I used to create my own template which generates a spring RowMapper for a JdbcDaoSupport class.
Start with exporting templates using menu Fast Code | Templates | Export Templates:
Choosing templates-config.xml (or all) creates a new project in my workspace:
<template type="Create Spring Detail Mapper"> <description></description> <variation></variation> <class-pattern></class-pattern> <getter-setter>getter-setter</getter-setter> <allowed-file-extensions>java</allowed-file-extensions> <number-required-classes>1</number-required-classes> <allow-multiple-variation>false</allow-multiple-variation> <template-body> <![CDATA[ private class ${class_name}Mapper implements RowMapper <${class_name}>{ public ${class_name} mapRow(ResultSet rs, int line) throws SQLException { ${class_name} inst = new ${class_name}(); #foreach ($field in ${fields}) #if (${field.type.endsWith("String")}) inst.${field.setter}(rs.getString("")); #else inst.${field.setter}(rs.getInt("")); // type not String, assume int #end #end return inst; } } ]]> </template-body> </template>To test the result I import the template back into fast code using menu Fast Code | Templates | Import Templates, then open up a java class that needs the mapper and execute the template using menu Fast Code | Templates | Create New Snippet. Pick my template, class, fields and presto:
private class ContactBeanMapper implements RowMapper<ContactBean> { public ContactBean mapRow(ResultSet rs, int line) throws SQLException { ContactBean inst = new ContactBean(); inst.setName(rs.getString("")); inst.setPhone(rs.getString("")); return inst; } }This may not seem like a big deal because I only have two fields in class ContactBean. It really pays off when I have a dozen or more fields.
I did find a few quirks with the plugin - it looks like the custom templates need to be reloaded every time eclipse is restarted, and the import/export mechanism seems to make a service call to localhost so make sure that the eclipse network proxy preference is configured correctly.
In short - I can create a new template whenever I want to generate some code or configuration file based on the definition of a java class. In another blog post, I use it to generate a javascript clone of a java bean.
I am trying to use Fast code eclipse plug-in in my small application and an error has occurred. I will share my error please solve it….
ReplyDeleteThanks for your response.
DeleteCould you please email with the exact error so that we can look into it.
Please contact us by clicking this link: http://www.contactify.com/b5a90
Thanks,
GD
i am using eclipse Indigo 3.7 i am keep on receiving the following error Ooops Action was not executed. Problem creating similar class null
ReplyDeleteThanks for your response.
DeleteCould you please email with the exact error so that we can look into it.
Please contact us by clicking this link: http://www.contactify.com/b5a90
Thanks,
GD
Perhaps you should try to contact the owner of the plugin, maybe he could help you out.
ReplyDeletehttp://fast-code.sourceforge.net/about.htm