Home > Eclipse, Regular Expressions, code > Using Regular Expressions to build ColdFusion getters and setters

Using Regular Expressions to build ColdFusion getters and setters

November 11th, 2008

I’m always looking for ways to make my life a little easier when writing code, and to take the monotony out of the equation wherever I can.  I know there is lots of back-and-forth about whether this method should or should not be used in ColdFusion, but I feel that for the minor incovenience of running a reg ex, creating getters and setters in my CFCs/Objects just make sense if just for the very fact of encapsulation.  Plus if you need to alter anything that occurs during a “set” operation, nothing outside of the object is broken when you make the change within your object.  Anyway, back to business:

This regular expression can be used in Eclipse (my current IDE) and will convert something that looks like:

<cfset variables.firstName = "" />
<cfset variables.lastName = "" />

into

<cffunction name="getfirstName" output="false" access="public" returntype="any">
<cfreturn variables.firstName />
</cffunction>
 
<cffunction name="setfirstName" output="false" access="public" returntype="void">
<cfargument name="val" required="true" />
<cfset variables.firstName = arguments.val />
</cffunction>

The search string is:

<cfset variables.(\w+) =  [^>]+>(\r\n)(\t)

and the replace string is:

<cffunction name="get$1" output="false"  access="public" returntype="any">$2$3$3<cfreturn variables.$1  />$2$3</cffunction>$2$2$3<cffunction name="set$1" output="false"  access="public" returntype="void">$2$3$3<cfargument name="val"  required="true" />$2$3$3<cfset variables.$1 = arguments.val  />$2$3</cffunction>$2$2$

The search string contains a “tab” character at the end. This is really just so I can format the output of the getters and setters, but feel free to modify them however you wish.

Gareth Eclipse, Regular Expressions, code , ,

  1. pmolaro
    March 9th, 2009 at 08:26 | #1

    Can you check the example and the reg ex and be sure it is documented correctly? I pasted in your property tags into a blank CFM page in Eclipse to try this out. Then I used Find/Replace cutting/pasting in your RegEx examples. Eclipse doesn’t find a match.

  2. Gareth
    March 9th, 2009 at 11:46 | #2

    Sorry, forgot to mention in my find/replace that it is actually looking for a carriage return ( that’s what the (\r\n) is doing on the end ) and a tab (\t), so the code would have to be set up something like this

    [tab here]<cfset variables.firstName = “” />[carriage return here]
    [tab here]<cfset variables.lastName = “” />[carriage return here]
    [tab here]

    That’s how I’m adding formatting to the outputted code…I use the carriage returns and tabs to reformat the output (as I don’t think eclipse has a way to do that just with regex terms). If you don’t mind having it all output on one line, just remove the (\r\n)(\t) from the “find” and remove any $2 or $3 from the “replace” statement and it should fix any problems. Then you’ll just have to go through and enter/tab the results around. If you have many variables, it’s easier to add the formatting first, then let the regex handle the rest.

  1. No trackbacks yet.