Monday, March 12, 2012

Using the Fast Code Eclipse Plugin

I recently discovered an eclipse plugin that I've been needing since I started coding. On several occasions I started down the path of writing my own but quickly got lost in the world of eclipse and ran out of time. Out of the box, the fast code plugin generates many types of code/xml, but the part that I really like is its support for custom templates.

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:

Next, I altered the exported templates-config.xml file and added my own custom template:
    <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.
Please note that generic comments containing links with the intent of self promotion will be flagged as spam and deleted.

5 comments:

  1. 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….

    ReplyDelete
    Replies
    1. Thanks for your response.
      Could 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

      Delete
  2. 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

    ReplyDelete
    Replies
    1. Thanks for your response.
      Could 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

      Delete
  3. Perhaps you should try to contact the owner of the plugin, maybe he could help you out.

    http://fast-code.sourceforge.net/about.htm

    ReplyDelete

Are you about to post a generic comment that has nothing to do with this post? Something like "Hey thanks for this very valuable information, BTW here's my website". If so, it will be marked as spam and deleted within 24 hours.

Note: Only a member of this blog may post a comment.