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:
Launch code in new window » Download code as text file »
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:
Launch code in new window » Download code as text file »
Running this, we get the following output:
ColdFusion Array Class Hierarchy
coldfusion.runtime.Array
java.util.Vector
java.util.AbstractList
java.util.AbstractCollection
java.lang.Object
Java String Array Class Hierarchy
[Ljava.lang.String;
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:
Launch code in new window » Download code as text file »
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.
Download Code Snippet ZIP File
Comments (3) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Colin Moock's ActionScript 3.0 Event
The Darjeeling Limited Starring Owen Wilson, Adrien Brody, And Jason Schwartzman
Very nice that CF8 added that ability. I did notice that you opted to copy the CF vector-based array to the actual java value array manually. Most all Java data structures offer the ability to export as an array. In fact, you can call ".toArray()" on the vector class and get an array of objects. If you needed a strict array of strings, you create the array like you did, but instead of copying manually, just use ".copyInto". I've posted an example on me blog. Cheers Mr. Nadel! Keep up the great blogging!
http://blog.adampresley.com/2007/11/12/on-coldfusion-7-arrays/
Posted by Adam Presley on Nov 12, 2007 at 9:28 AM
@Adam,
Very cool. I have never used the .CopyInto() method before. I like it. And I guess because the Java array is strictly typed, that ColdFusion will make sure to cast to that type (just to make sure everything runs smoothly). Despite not knowing about that function, one of my fears is that ColdFusion doesn't always cast properly which is why I have become a much more liberal user of JavaCast() when interfacing with Java.
Anyway, your way is much short and I like it! Thanks.
Posted by Ben Nadel on Nov 12, 2007 at 6:33 PM
You are correct about the casting. The function uses the basics of polymorphism as it expects to copy into an array of Objects. The String class is based off Object, and therefore meets the requirements and will cast up. It's cool what you can do with the java side of CF. :)
Posted by Adam Presley on Nov 12, 2007 at 8:06 PM