Getting and Using ColdFusions Underlying Java Classes

Published: {ts '2011-07-07 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/getting-and-using-coldfusions-underlying-java-classes/

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.

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.

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

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".

Listing The Available Methods For This Object Type

Now that we have access to the class and we know the class name, we can now look at what methods are available to use on this object type. To do this we use "getMethods()" on the class.

You will note that I have used an array reference of 1 at the end of the "getMethods()" call. This is because the "getMethods()" call returns (for me anyway) an array of 25 identical java class objects. Since these are identical and because cfdump slows my browser so much when showing this many structures, it was beneficial to only dump the first item in the array.

Listing The Available Methods For Manipulating Data Within The Object

It is important to note that the above listed methods for the object types are used to manipulate the object itself not the data within it. This tripped me up as well until I realised that there are two sets of methods. Those to create and manipulate a class object and the methods within the class object used to manipulated the stored data in that class object.

To get the methods that we need to manipulate the data within a particular class object we create a java object of the particular class name and pass it to cfdump.

Getting The SuperClass

Finally it is interesting to note that you can also access the "SuperClass" from which this object type inherited. To do this simply use the "getSuperClass()" method.

So there you have it. With this information we now have potential new avenues open to us as CF developers when faced with unusual problems.