Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with: Simon Free and Todd Sharp and Shannon Hicks and Charlie Arehart and Sean Corfield

Finding Available Java Constructors / Methods For ColdFusion Objects

By Ben Nadel on
Tags: ColdFusion

I am becoming increasingly interested in the Java world that lives below the surface of ColdFusion. She seems like a really sexy girl and I like playing with her bits. The problem is, I don't really know what is available. You can call GetMetaData() on objects and it gives you some insight, but not everything you need to know. To overcome the limitation of GetMetaData(), I have developed two functions to help in my exploration:

GetJavaClassMethods()
GetJavaClassMethodParameters()

While the first method above is the *real* method of use, let's look at the second method first:

GetJavaClassMethodParameters()

This method takes only one argument, a Java method definition. It takes this definition and pulls out the parameters that need to be passed in and returns them as a comma-delimited list. This is sooo key when compared to GetMetaData() because it shows you how overloaded methods are different from one another.

Let's take a look at the method:

  • <cffunction
  • name="GetJavaClassMethodParameters"
  • access="public"
  • returntype="string"
  • output="false"
  • hint="Returns the arguments that are required for the given Java method call.">
  •  
  • <!--- Define arguments. --->
  • <cfargument name="Method" type="any" />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  • <!--- Get the parameters. --->
  • <cfset LOCAL.Parameters = ARGUMENTS.Method.GetParameterTypes() />
  •  
  • <!--- Set up the results. --->
  • <cfset LOCAL.ParamList = "" />
  •  
  • <!--- Loop over the parameters. --->
  • <cfloop
  • index="LOCAL.ParameterIndex"
  • from="1"
  • to="#ArrayLen( LOCAL.Parameters )#"
  • step="1">
  •  
  • <cfset LOCAL.ParamList = ListAppend(
  • LOCAL.ParamList,
  • LOCAL.Parameters[ LOCAL.ParameterIndex ].GetName()
  • ) />
  •  
  • </cfloop>
  •  
  • <!--- Space out the parameters (visual difference only). --->
  • <cfset LOCAL.ParamList = Replace(
  • LOCAL.ParamList,
  • ",",
  • ", ",
  • "ALL"
  • ) />
  •  
  • <!--- Return the parameter list. --->
  • <cfreturn LOCAL.ParamList />
  • </cffunction>

This function might return, as an example, something like "int, java.lang.String". This would denote that to properly call the Java method, you would need to pass it an integer and a string value (in that order). Remember to use JavaCast() when calling Java methods.

But, that's just a small part of it. Let's take a look at the parent function:

GetJavaClassMethods()

This takes a ColdFusion object and returns all of its available Java methods. I don't think all of these can be used exactly - it takes a bit of trial to get it right. The method list is returned in a query that has both public methods AND constructors as well as the parameter list discussed above and the return types.

  • <cffunction
  • name="GetJavaClassMethods"
  • access="public"
  • returntype="query"
  • output="false"
  • hint="This takes a ColdFusion object and returns a query of all the available Java methods including constructors.">
  •  
  • <!--- Define argument. --->
  • <cfargument name="CFObject" type="any" required="yes" />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  • <!--- Create a query for the methods. --->
  • <cfset LOCAL.MethodQuery = QueryNew(
  • "name, return_type, parameters, is_constructor"
  • ) />
  •  
  • <!--- Get an array of all constructors of the class object. --->
  • <cfset LOCAL.ClassConstructors = ARGUMENTS.CFObject.GetClass().GetConstructors() />
  •  
  • <!--- Get an array of all PUBLIC methods of the class object. --->
  • <cfset LOCAL.ClassMethods = ARGUMENTS.CFObject.GetClass().GetMethods() />
  •  
  • <!--- Add enough rows to the query for each method. --->
  • <cfset QueryAddRow(
  • LOCAL.MethodQuery,
  • (
  • ArrayLen( LOCAL.ClassConstructors ) +
  • ArrayLen( LOCAL.ClassMethods )
  • )) />
  •  
  • <!--- Loop over constructors. --->
  • <cfloop
  • index="LOCAL.ConstructorIndex"
  • from="1"
  • to="#ArrayLen( LOCAL.ClassConstructors )#"
  • step="1">
  •  
  • <!--- Set the name of the method. --->
  • <cfset LOCAL.MethodQuery[ "name" ][ LOCAL.ConstructorIndex ] = LOCAL.ClassConstructors[ LOCAL.ConstructorIndex ].GetName() />
  •  
  • <!--- Set the return type. --->
  • <cfset LOCAL.MethodQuery[ "return_type" ][ LOCAL.ConstructorIndex ] = ARGUMENTS.CFObject.GetClass().GetName() />
  •  
  • <!--- Set the parameters. --->
  • <cfset LOCAL.MethodQuery[ "parameters" ][ LOCAL.ConstructorIndex ] = GetJavaClassMethodParameters( LOCAL.ClassConstructors[ LOCAL.ConstructorIndex ] ) />
  •  
  • <!--- Set this as being a constructor. --->
  • <cfset LOCAL.MethodQuery[ "is_constructor" ][ LOCAL.ConstructorIndex ] = 1 />
  •  
  • </cfloop>
  •  
  •  
  • <!--- Set offset for record count. --->
  • <cfset LOCAL.Offset = (LOCAL.ConstructorIndex - 1) />
  •  
  • <!--- Loop over methods. --->
  • <cfloop
  • index="LOCAL.MethodIndex"
  • from="1"
  • to="#ArrayLen( LOCAL.ClassMethods )#"
  • step="1">
  •  
  • <!--- Set the name of the method. --->
  • <cfset LOCAL.MethodQuery[ "name" ][ LOCAL.Offset + LOCAL.MethodIndex ] = LOCAL.ClassMethods[ LOCAL.MethodIndex ].GetName() />
  •  
  • <!--- Set the return type. --->
  • <cfset LOCAL.MethodQuery[ "return_type" ][ LOCAL.Offset + LOCAL.MethodIndex ] = LOCAL.ClassMethods[ LOCAL.MethodIndex ].GetReturnType().GetName() />
  •  
  • <!--- Set the parameters. --->
  • <cfset LOCAL.MethodQuery[ "parameters" ][ LOCAL.Offset + LOCAL.MethodIndex ] = GetJavaClassMethodParameters( LOCAL.ClassMethods[ LOCAL.MethodIndex ] ) />
  •  
  • <!--- Set this as not being a constructor. --->
  • <cfset LOCAL.MethodQuery[ "is_constructor" ][ LOCAL.Offset + LOCAL.MethodIndex ] = 0 />
  •  
  • </cfloop>
  •  
  • <!--- Perform a query of queries to get proper sorting. --->
  • <cfquery name="LOCAL.SortedMethodQuery" dbtype="query">
  • SELECT
  • name,
  • return_type,
  • parameters,
  • is_constructor
  • FROM
  • [LOCAL].MethodQuery
  • ORDER BY
  • is_constructor DESC,
  • name ASC,
  • parameters ASC,
  • return_type ASC
  • </cfquery>
  •  
  • <!--- Return the full sorted method query. --->
  • <cfreturn LOCAL.SortedMethodQuery />
  • </cffunction>

As you can see, the Method Query is built from the ground-up based on the constructors and then the public methods of the Java class that lives under the passed-in ColdFusion object. To see this in action, the following code:

  • <!--- Build a test string. --->
  • <cfset strTest = "ben nadel is one cool dude!" />
  •  
  • <!--- Dump out the Java methods. --->
  • <cfdump var="#GetJavaClassMethods( strTest )#" />

... would produce the following dump:


 
 
 

 
Java Method List For ColdFusion Objects  
 
 
 

Let's compare that to the standard GetMetaData() dump:


 
 
 

 
GetMetaData() Methods For ColdFusion Objects  
 
 
 

How cool is that?!? My method list is not only alphabetically listed (no worries, it was my pleasure), it shows you what kind of arguments you need to send in. No more guessing! Let's the exploration begin!



Looking For A New Job?

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

Reader Comments

Ben, I wanted to let you know I adapted these functions into cfscript (what can I say, I dig the quasi-ECMA thing) and they have been a life saver. I'm working on a gi-normous project to bridge our company's CF-based software with a third-party vendor's system via their web service. Unfortunately, the 3p's documentation is Cruddy McCrap. Their sample code and docs are often out of date or just plain wrong. Now I can see precisely what parameter types are expected. We'll see if this works in comment form, but here it is (if you're curious about some of the variable names I often prefix my variables with a letter to indicate the expected datatype s=string n=numeric k=struct etc)...

<pre>
function getJavaClassMethodParameters(sMethod) {
//Returns the arguments for the given Java method call.
var kLocal = StructNew();
var i = 0;

kLocal.Parameters = Arguments.sMethod.GetParameterTypes();
kLocal.ParamList = "";

for (i=1; i lte ArrayLen(kLocal.Parameters); i=i+1) {
kLocal.ParamList = ListAppend(kLocal.ParamList, kLocal.Parameters[i].GetName());
}
kLocal.ParamList = Replace(kLocal.ParamList, ",", ", ", "ALL");
return kLocal.ParamList;
}

function getJavaClassMethods(oObject) {
//This takes an object and returns a query of all the available Java methods including constructors.
var kLocal = StructNew();
var i = 0;

kLocal.MethodQuery = QueryNew("name, returntype, parameters, isconstructor");
try {
kLocal.ClassConstructors = Arguments.oObject.GetClass().GetConstructors();
}
catch (Any e) {
kLocal.ClassConstructors = ArrayNew(1); // no constructors found.
}
try {
kLocal.ClassMethods = Arguments.oObject.GetClass().GetMethods();
}
catch (Any e) {
kLocal.ClassMethods = ArrayNew(1);
}

if (ArrayLen(kLocal.ClassConstructors) + ArrayLen(kLocal.ClassMethods) gt 0) {
QueryAddRow(kLocal.MethodQuery, (ArrayLen(kLocal.ClassConstructors) + ArrayLen(kLocal.ClassMethods)));
}

for (i=1; i lte ArrayLen(kLocal.ClassConstructors); i=i+1) {
kLocal.MethodQuery["name"][i] = kLocal.ClassConstructors[i].GetName();
kLocal.MethodQuery["returntype"][i] = Arguments.oObject.GetClass().GetName();
kLocal.MethodQuery["parameters"][i] = GetJavaClassMethodParameters(kLocal.ClassConstructors[i]);
kLocal.MethodQuery["is
constructor"][i] = 1;
}
kLocal.Offset = (i - 1);

for (i=1; i lte ArrayLen(kLocal.ClassMethods); i=i+1) {
kLocal.MethodQuery["name"][kLocal.Offset + i] = kLocal.ClassMethods[i].GetName();
kLocal.MethodQuery["returntype"][kLocal.Offset + i] = kLocal.ClassMethods[i].GetReturnType().GetName();
kLocal.MethodQuery["parameters"][kLocal.Offset + i] = GetJavaClassMethodParameters(kLocal.ClassMethods[i]);
kLocal.MethodQuery["is
constructor"][kLocal.Offset + i] = 0;
}

return getJavaClassMethodsQuery(kLocal.MethodQuery);
}

</pre>

Reply to this Comment

Jeremy,

Sorry it has taken me so long to comment on your stuff. I think it's awesome. I especially like the TRY / CATCH blocks you put in. I started to realize that some of these things do throw errors and I have been too lazy to fix it.

Well done dude, well done.

Reply to this Comment

Just to let you know in the first code block you don't need to do a replace on the delimiter to space it out. You can simply provide the comma and the space as the delimiter in the listAppend function call. For example, you could do just this: ListAppend("myString", myList, ", ") and it'll work just fine.

Reply to this Comment

Hey, this is strange! Coldfusion thinks a string is the same as a number...

<cfset cftype="marc bakker is cool-a-dude">
<cfdump var="#getJavaClassMethods(cftype)#">

gives me the same underlying Java constructors and methodnames as

<cfset cftype=5>
<cfdump var="#getJavaClassMethods(cftype)#">

Why is this??

Marc

Reply to this Comment

@Marc,

ColdFusion seems to like storing most simple data types as Strings. It will, from time to time, depending on the type of simple value, use a different underlying data type. But, a lot of the time it is a string.

For example:

<cfset x = 1.5 />

... will result in a string. But, this:

<cfset x = createTimeSpan( 1, 12, 0, 0 ) />

... will result in a Double (also 1.5). Also, I believe that now() will return a date/time stamp.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.