Finding Available Java Constructors / Methods For ColdFusion Objects

Posted August 17, 2006 at 3:57 PM

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:

 Launch code in new window » Download code as text file »

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

 Launch code in new window » Download code as text file »

  • <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:

 Launch code in new window » Download code as text file »

  • <!--- 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!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Sep 27, 2006 at 11:16 AM // reply »
30 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>


Sep 27, 2006 at 11:17 AM // reply »
30 Comments

Oops...well so much for using the <pre> tag. :-)


Oct 3, 2006 at 7:16 AM // reply »
7,203 Comments

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.


Jan 27, 2007 at 10:45 PM // reply »
92 Comments

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.


Jan 28, 2007 at 8:44 PM // reply »
95 Comments

You are the man! Simple as that :-)


Post Comment  |  Ask Ben

Recent Blog Comments
Feb 8, 2010 at 6:25 PM
Muscle: Confessions Of An Unlikely Bodybuilder By Samuel Wilson Fussell
Hello Ben, Talk about synchronicity!! I just saw a copy of this book last night. Was intrigued with it and decided to see what the author was up to these days. Did a google search and arrived here se ... read »
Feb 8, 2010 at 4:47 PM
How To Create GStrings In Javascript By Extending Core Data Types
@Garrett, Very interesting. I'll have to give that a look. I don't think I have seen that before. ... read »
Feb 8, 2010 at 4:43 PM
How To Create GStrings In Javascript By Extending Core Data Types
Sick stuff Ben! Ironically there is an interesting project called fusebox that takes on the idea of "safely" extending JavaScript. http://github.com/jdalton/fusebox ... read »
Feb 8, 2010 at 4:19 PM
Converting An IP Address To An Integer Using MySQL (Thanks Julian Halliwell)
@Rob Yes, as I said it's CF9 that seems to return byte arrays from this and certain other MySQL functions such as GREATEST(). To run Ben's code as is you just need to enable multiple queries in you ... read »
Feb 8, 2010 at 4:04 PM
Creating Microsoft Excel Documents With ColdFusion And XML
@Ben, Thanks for the tip regarding your XML article and using BufferedInputStream instead of FileInputStream. I wish excel files were written in plain text just like your XML example. Unfortunately ... read »
Feb 8, 2010 at 3:51 PM
How To Create GStrings In Javascript By Extending Core Data Types
@Robert, Thanks my man. This is the first time I have ever tried to extend a core Javascript object. Technically, I have extended the "Object" class (pretty much how all prototypal inheritance work ... read »
Feb 8, 2010 at 3:37 PM
How To Create GStrings In Javascript By Extending Core Data Types
This is some impressive solution. I've to try it myself to understand the concept better and say more about it, but nice job! PS: Wicked pictures! ... read »
Feb 8, 2010 at 3:20 PM
Converting An IP Address To An Integer Using MySQL (Thanks Julian Halliwell)
@Julian, I book marked this post so I could make a very similar post. A few years ago I experimented quite a bit with the INET functions and BaseN conversions in MySQL after finding coldfusion lim ... read »