Ask Ben: Get Arguments For ColdFusion User Defined Functions

Posted December 1, 2008 at 2:56 PM by Ben Nadel

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:

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



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 »
11,314 Comments

@Ike,

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


Dec 1, 2008 at 6:03 PM // reply »
211 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 »
42 Comments

Thanks so much, Ben.


Dec 2, 2008 at 7:44 AM // reply »
11,314 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 A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 19, 2013 at 10:41 PM
Referencing ColdFusion Query Columns In A Loop Using Both Array And Dot Notation
Burdock-roots Are you going fat day by day? You need to be good for your family and make some money too. So we bring for you a best product that helps you to be more energetic every day. You will b ... read »
Jun 19, 2013 at 9:52 PM
Working With Inherited Collections In AngularJS
I recognize the applicability of your solution, and how easy it makes to share data across multiple views or even "submodules" of rather simple application. But it seems to me that it creat ... read »
Jun 19, 2013 at 9:38 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Alesei, Glad you like it. Even after working with AngularJS for months, I still get a bunch of unexpected, "$digest is already in progress". So hard to debug sometimes! ... read »
Jun 19, 2013 at 9:36 PM
Working With Inherited Collections In AngularJS
@Mike, The relationship of $scope values is definitely an interesting thing! But it's not simple - it really forces you to understand prototypal inheritance, which is not at all a simple topic! Gla ... read »
Jun 19, 2013 at 9:35 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Joe, Oh, super interesting! I had only thought to url-encode the signature; but I think that's because the S3 docs actually have a special NOTE telling you to do so. It would have never occurred t ... read »
Jun 19, 2013 at 9:32 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Richard, Glad you like! Hopefully I'll have some more interesting stuff coming. This morning, I blogged a bit more about generating the pre-signed, query string authenticated URLs; but, then deeme ... read »
Jun 19, 2013 at 9:31 PM
Filter vs. ngHide With ngRepeat In AngularJS
@Mike, Honestly, in the majority of cases, I would say there isn't going to be a difference. Both approaches have trade-offs. If you use the filter, then you have fewer DOM elements and fewer $scop ... read »
Jun 19, 2013 at 2:01 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
I have coincidentally been beating my head against the S3 API for the last week or so. One big "gotcha" I had to work around was file names and paths containing spaces. Remember to URL Enco ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools