NOTE: This post is not so much for other people, but rather more for myself as a lookup. I have seen examples of this, but just wanted to walk through it so that I could fully understand how it works.
Anyone who has ever had to pass a Java array into a method has quickly come to find out that a ColdFusion array is not a Java array. Furthermore, attempting to pass a ColdFusion array into a method expecting a Java array will throw an error probably telling you that no such method exists. The problem is that ColdFusion arrays are not Java arrays but are, in fact, some form of Vector List. This is what gives us the ability easily resize arrays and store mixed data types within them.
Creating a Java array is insanely easy in ColdFusion 8 and somewhat burdensome in ColdFusion 7. Let's take a look at how this can be done in ColdFusion 7 first (so that we can then see how awesome ColdFusion 8 is). To create and manipulate Java arrays, we are going to use the Java Array Reflection class, java.lang.reflect.Array. This class, which is composed of all static methods, acts as the go-between for our ColdFusion code and the Java array. So for example, instead of setting a value directly into the Java array, we tell the Java array reflection class to do this for us by providing the array, the index, and the value we want to set.
In this example, we are going to build a simple ColdFusion array of string values and then convert it into a Java array:
<!--- Create a ColdFusion array of strings values. ---> <cfset arrValue = ArrayNew( 1 ) /> <!--- Populate the array. ---> <cfset arrValue[ 1 ] = "What's up hot stuff?" /> <cfset arrValue[ 2 ] = "You come here a lot?" /> <cfset arrValue[ 3 ] = "You from out of town?" /> <cfset arrValue[ 4 ] = "Want a little of this?" /> <cfset arrValue[ 5 ] = "How 'bout a little of that?" /> <!--- Now that we have a ColdFusion array, we need to create a Java array of the data. We cannot act on that array directly since ColdFusoin doesn't play with arrays the same way Java does. So, we have to use the Array Reflect class to get Java to do the heavy lifting for us. ---> <!--- The first thing we need to do is get the Java class of the data type that we are going to store in the array. In our scenario, we are going to store an array of strings. Therefore, let's get a reference to the string class. ---> <cfset objStringClass = CreateObject( "java", "java.lang.String" ).GetClass() /> <!--- Now, we need a reference to Java's array reflection class that we will use to do the array manipulation. These are all static methods so we don't need to initialize the reflection instance (ie. we are not calling Init() on it). ---> <cfset objReflect = CreateObject( "java", "java.lang.reflect.Array" ) /> <!--- Using the Java array reflection class, we are going to create a Java Array object. To do so, we need to the class (defined above) and the length of the array (gotten from the ColdFusion array). We need the length because Java arrays cannot be as easily resized as ColdFusion's arrays which are really collections. ---> <cfset arrJavaValue = objReflect.NewInstance( objStringClass, JavaCast( "int", ArrayLen( arrValue ) ) ) /> <!--- Now that we have our Java array, we need to loop over the values we have in our ColdFusion array and copy them in to the Java array. ---> <cfloop index="intIndex" from="1" to="#ArrayLen( arrValue )#" step="1"> <!--- Use the Java reflect class to set the string value into the java array at the approriate index. Remember that the Java arrays are zero-based, and so, we must subract one from our ColdFusion index to map the proper index. ---> <cfset objReflect.Set( arrJavaValue, JavaCast( "int", (intIndex - 1) ), JavaCast( "string", arrValue[ intIndex ] ) ) /> </cfloop> <!--- Dump out the ColdFusion array. ---> <cfdump var="#arrValue#" label="ColdFusion Array" /> <!--- Dump out the Java String array. ---> <cfdump var="#arrJavaValue#" label="Java String Array" />
Running the above code, you will see that we get very similar CFDump outputs for both the ColdFusion array and the Java array:
But, don't let the output fool you; these are two very different objects and they have completely different sets of behavior. Let's take a look at their class inheritance hierarchy:
<h4> ColdFusion Array Class Hierarchy </h4> <p> #arrValue.GetClass().GetName()#<br /> #arrValue.GetClass().GetSuperClass().GetName()#<br /> #arrValue.GetClass().GetSuperClass().GetSuperClass().GetName()#<br /> #arrValue.GetClass().GetSuperClass().GetSuperClass().GetSuperClass().GetName()#<br /> #arrValue.GetClass().GetSuperClass().GetSuperClass().GetSuperClass().GetSuperClass().GetName()#<br /> </p> <h4> Java String Array Class Hierarchy </h4> <p> #arrJavaValue.GetClass().GetName()#<br /> </p>
Running this, we get the following output:
ColdFusion Array Class Hierarchy
Java String Array Class Hierarchy
As you can see, the ColdFusion array is an instance of the coldfusion.runtime.Array which inherits from Vector and Collection. Our Java array on the other hand is simply an array of string objects. NOTE: The "[" in the class name denotes that it is an array of the given type.
So, taking a ColdFusion array and converting it into a Java array is not the easiest thing in ColdFusion 7 (and earlier). Luckily, ColdFusion 8 has made some very sexy improvements to the JavaCast() method. Now, you can have JavaCast() return an array of objects rather than just a single value. To do this, you simply need to append the  array notation after the desired data type:
<!--- Convert our ColdFusion array into a Java array. ---> <cfset arrJavaValue = JavaCast( "string", arrValue ) />
That's all there is to it! Notice that we are asking to get the Java type "string"; this is an array of strings. That simple upgrade to JavaCast() in ColdFusion 8 has taken dozens of lines of code and reduced it to three. Way to go Adobe!
The example above was done for Strings, but this can be done with any type of object. As long as you can get the object's Class object for use in the NewInstance() call, you can create an array that can take any type of data.
Want to use code from this post? Check out the license.