ColdFusion, like many programming languages, provides a number of introspection and decision functions that allow for meta-programming. For example, I've built my own memory leak detector that walks the in-memory cache of components at the end of every ColdFusion request (in my local development environment). And this morning, I released a Content Security Policy (CSP) compatible version of CFDump that can recurse down through any data structure. In both of these cases, I had to polyfill a decision function that can determine if a given value is a ColdFusion component. And while I've mentioned this User Defined Function (UDF) before, I don't think I've ever outlined it.
In the following CFML code, I showcase two different strategies:
-
Using my
isComponent()decision function. -
Using the native
isInstanceOf()decision function.
When you define a ColdFusion component (CFC), the runtime transparently makes your CFC extend a shared, base CFC in the WEB-INF folder. Even when you use the extends attribute in your component definition, the root of your inheritance hierarchy still extends from this globally shared Component.cfc.
To be honest, I've never relied on this base Component.cfc for anything. And I'm not convinced that my ColdFusion applications would referencing the correct component path. But I'm including it here in the demo as an alternate perspective.
<cfscript>
data1 = "";
data2 = {};
// Component definition.
data3 = createObject( "component", "Test" );
// Component instance.
data4 = new Test();
// Check data types using our polyfill decision function.
writeDump([
isComponent_1: isComponent( data1 ),
isComponent_2: isComponent( data2 ),
isComponent_3: isComponent( data3 ),
isComponent_4: isComponent( data4 ),
]);
// Also, all ColdFusion components quietly extend a base component in the `WEB-INF`
// folder named, `Component.cfc`. I'm not sure how dependable that is; but, we can
// also use the isInstanceOf() method to see if the given value is an instance of this
// globally inherited component.
writeDump([
isInstanceOf_1: isInstanceOf( data1, "Component" ),
isInstanceOf_2: isInstanceOf( data2, "Component" ),
isInstanceOf_3: isInstanceOf( data3, "Component" ),
isInstanceOf_4: isInstanceOf( data4, "Component" ),
]);
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
/**
* I determine if the given value is a ColdFusion component instance.
*/
private boolean function isComponent( any value = "" ) {
if ( ! isObject( value ) ) {
return false;
}
// The isObject() function will return true for both components and Java objects.
// As such, we need to go one step further to see if we can get at the component
// metadata before we can truly determine if the value is a ColdFusion component.
try {
var metadata = getMetadata( value );
return ( metadata?.type == "component" );
} catch ( any error ) {
return false;
}
}
</cfscript>
The core of the decision method is looking at the metadata of the given value. I'm including the isObject() call as an efficiency gain to short-circuit metadata collection. That said, when we run this CFML code, we get the following output:
As you can see, both my isComponent() decision function polyfill and the native isInstanceOf() decision function agree. The major difference being that mine doesn't depend on "spooky action at a distance".
Most ColdFusion applications never need to test for a value's "componentness". But, it's very helpful in all the tooling that goes into and around a ColdFusion application. It's definitely a function that should exist natively.
Reader Comments
I've filed a feature request ticket with the Adobe Tracker:
CF-4235810: Add an isComponent() decision function.
Post A Comment — ❤️ I'd Love To Hear From You! ❤️
Post a Comment →