Calling Java Class Methods Directly From ColdFusion Objects

Posted August 16, 2006 at 8:44 AM

Tags: ColdFusion

I was on my way back from vacation yesterday, driving down Route 95 South, when suddenly it hit me: Why not just call Java methods directly from the ColdFusion objects we use? Let's look at string manipulation, since it is a large part of programming. In the past, if I wanted to create a Java String object from a ColdFusion String object, I would instantiate a new Java String object:

 Launch code in new window » Download code as text file »

  • <cfset jstrText = CreateObject(
  • "java",
  • "java.lang.String"
  • ).Init(
  • strText
  • ) />

This would take the ColdFusion string "strText" and use it to construct a new Java String object "jstrText". But, what I realized on the road was that the ColdFusion object ALREADY IS a Java String object. The Java String object exists - there is no need to create a new one. What's not obvious is that those Java methods are available from the ColdFusion String object.

One of the big reasons I like Java String objects is that their Regular Expression replaces are MUCH faster than that of ColdFusion. So, let's take this text for example. I am going to build a ColdFusion string:

 Launch code in new window » Download code as text file »

  • <!--- Save text in a buffer variable. --->
  • <cfsavecontent variable="strText">
  •  
  • As Anna stepped out of the water into the
  • cool light of the moon, I could just make out
  • curves of her hip through the wet threads of
  • her summer dress.
  •  
  • </cfsavecontent>

Now, you can run regular expression replaces the ColdFusion way:

 Launch code in new window » Download code as text file »

  • <cfset strResult = REReplace(
  • strText,
  • "([a-z]{1,3})",
  • "{\1}",
  • "ALL"
  • ) />

BUT, you can also call the Java methods directly as well:

 Launch code in new window » Download code as text file »

  • <cfset jstrResult = strText.ReplaceAll(
  • "([a-z]{1,3})",
  • "{$1}"
  • ) />

How cool is that? I'll tell you: wicked cool! Not only that, the Java String::ReplaceAll() method is faster than the ColdFusion REReplace() method. And, we didn't incur the overhead of instantiating a new Java String object to work with the regular expressions. Again, wicked cool!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Aug 24, 2006 at 3:01 PM // reply »
30 Comments

That's a great idea. I've written many CF apps over the years that had a lot of regex replacement. This can get to be a significant source of overhead. Have you seen or personally done any tests to measure the performance increases of Java regex replacement vs ColdFusion?


Aug 25, 2006 at 7:59 AM // reply »
74 Comments

Jeremy,

In my experience, yes, Java regular expressions are faster. But, honestly, it's not just about the speed; it about the flexibility. Java regular expression are simply more powerful than ColdFusion regular expressions. Of particular note is that Java regular expressions can handle positive and negative look behinds, which I believe ColdFusion can handle none.

Now, don't get me wrong, I use REFind(), REReplace() all the time. But there are certainly situations where the Java regular expression is awesome.


Feb 2, 2007 at 9:56 PM // reply »
164 Comments

I'll add, in response to Jeremy's comment, that the biggest source of performance problems with regular expressions is developers who don't know how to write regular expressions in ways that optimize the performance.


Feb 3, 2007 at 7:50 PM // reply »
6,516 Comments

Steve,

I just got finished commenting on another of your comments.... I simply didn't think much about optimization because I didn't realize that tweaks could really be done. My eyes have been opened.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »