Getting and Using ColdFusions Underlying Java Classes

Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

Since ColdFusion objects and variables are based on java class objects it is possible to use the underlying java methods on the objects. First though we need to figure out what those java methods are. This can be accomplished as follows.

Getting The Java Class

The first thing we need to do is get access to the underlying class of a ColdFusion object. For example a query object, a structure, an array or a variable.

To do this we create an instance of the target object type and dump a call to the "getClass()" method on it. This will show all the available methods we can call on this object type.

<!--- Create instances of some ColdFusion object types --->
<cfset queryObject = queryNew("col1,col2","varchar,varchar")>
<cfset structObj = structNew()>
<cfset arrayObj = arrayNew(1)>
<cfset simpleVar = "hello">
<cfset simpleVarNum = val(2)>

<!--- Call the getClass() method on each object and dump the results --->
<cfdump var="#queryObject.getClass()#">
<cfdump var="#structObj.getClass()#">
<cfdump var="#arrayObj.getClass()#">
<cfdump var="#simpleVar.getClass()#">
<cfdump var="#simpleVarNum.getClass()#">

From the results of this dump we see that this is a "java.lang.class" object. This however is not the name of the underlying class of this object. However by looking at the listed methods we can see that there exists a getName() method. Calling this method on the result of the getClass() method returns us the java class name of the ColdFusion object type.

<!--- Call getName() to get the name of the associated java class for this object type --->
<cfdump var="#queryObject.getClass().getName()#">
<cfdump var="#structObj.getClass().getName()#">
<cfdump var="#arrayObj.getClass().getName()#">
<cfdump var="#simpleVar.getClass().getName()#">
<cfdump var="#simpleVarNum.getClass().getName()#">

From this we can see that our ColdFusion object types map to the following java class's.

  • query -> coldfusion.sql.QueryTable
  • struct -> coldfusion.runtime.Struct
  • array -> java.util.Vector
  • simple value -> java.lang.String
  • simple numeric value -> java.lang.Double

Note: You will note that the variable "simpleVarNum" was set using the "val()" function. The reason for this is that by default ColdFusion saves the value as a string and only converts it to a numeric datatype if a numeric operation is called on it. Thus using "<cfset simpleVarNum = 2>" would result in an object on class "java.lang.string".

1 2

Related Blog Postings

Reader Comments

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing