Ask Ben: Get Arguments For ColdFusion User Defined Functions

Posted December 1, 2008 at 2:56 PM

Tags: ColdFusion, Ask Ben

I know that with a CFC you can view the cfc and it will give you a listing of the methods, arguments, etc. But I have single page that has a ton of different UDF's within it. I can do a cfdump of the variables struct, which will then give me a great look at all the UDF's. Is there an way to do:

<cfloop collection="#variables#" item="key">
#lcase(key)#
</cfloop>

and have it give me the arguments that are required? I am working on some documentation, so that would make my life 100x easier.

The key to this problem is ColdFusion's GetMetaData() method. This method allows us to get data about many objects in ColdFusion, including method arguments. To use this, all we need to do is modify your example code such that it picks out the methods in the page and then gathers their meta data:

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

  • <!---
  • Loop over all the keys in our VARIABLES scope. This could be
  • *any* scope, but for this test, we are using the local scope
  • of my *test* page.
  • --->
  • <cfloop
  • item="strKey"
  • collection="#VARIABLES#">
  •  
  • <!---
  • Check to see if this value is a user defined function.
  • If so, then we can inspect it.
  • --->
  • <cfif IsCustomFunction( VARIABLES[ strKey ] )>
  •  
  •  
  • <!--- Get a pointer to the method. --->
  • <cfset fnMethod = VARIABLES[ strKey ] />
  •  
  • <!--- Get the meta data of the method. --->
  • <cfset objMetaData = GetMetaData( fnMethod ) />
  •  
  • <!--- Dump out the meta data. --->
  • <cfdump
  • var="#objMetaData#"
  • label="Method: #strKey#"
  • />
  •  
  •  
  • </cfif>
  •  
  • </cfloop>

Because the VARIABLES scope will have a ton of information in it, we utilize ColdFusion's IsCustomFunction() method to make sure that we select only the user defined functions. Once we know that the data key in question is a method, we get a pointer to the function. Since user defined methods (UDFs) in ColdFusion are first-class objects, this pointer to the function now acts in lieu of the original function. We then get the meta data on this function and dump it out. On my test page, this creates the following output:

 
 
 
 
 
 
Output Generated For User Defined Functions With ColdFusion's GetMetaData() Function. 
 
 
 

As you can see, not only does this give us all the information defined in the CFFunction tag, it also gives us an ordered array of the method arguments. Now, my functions are well documented in their tags, so this output is robust; but, realize that if you leave data out of a tag (or use CFScript function definitions), then much of this data will not come through in the GetMetaData() function as it cannot be determined.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



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

Reader Comments

ike
Dec 1, 2008 at 4:23 PM // reply »
78 Comments

I'm not certain, but I think part of the plan with the CF9 beta has been to allow us to define data types (and possibly default / required values) when declaring functions with cfscript. If that turns out to be true then that should hopefully make that extra metadata accessible this way as well, but of course, not with current versions. :)


Dec 1, 2008 at 4:36 PM // reply »
6,371 Comments

@Ike,

I believe you are correct - but I'll believe it when I see it :)


Dec 1, 2008 at 6:03 PM // reply »
165 Comments

In Addition to getMetadata() - you also have getComponentMetadata() - http://livedocs.adobe.com/coldfusion/8/htmldocs/functions_e-g_33.html

Kudos to Ray Camden for pointing it out elsewhere.


Dec 1, 2008 at 10:30 PM // reply »
39 Comments

Thanks so much, Ben.


Dec 2, 2008 at 7:44 AM // reply »
6,371 Comments

@Todd,

I could definitely see that as useful when you are creating documentation since you do not need an actual instance of the object in order to get the meta data.

@Brandon,

My pleasure man. I hope this helped a bit.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »