Is it possible to test an object against a component type and/or inherited type?
Asked Answered
J

4

6

Update: Based on the answers I initially went the route of using IsInstanceOf() which was designed for this need. However it turned out to be extremely inefficient for some unknown reason. In debugging the app later I ended up just setting some properties on the object to use instead of IsInstanceOf resulting in orders of magnitude speed improvement.

What I am trying to do is test an object in ColdFusion to see what type of component it is. Something like...

<cfif isValid( "compath.dog", currentObj)>
    ...do something specific with dog objects...
</cfif>

I thought this was possible but receive an error saying the type I am passing does not correspond to one in the valid list of types...

Valid type arguments are: any, array, Boolean, date, numeric, query, string, struct, UUID, GUID, binary, integer, float, eurodate, time, creditcard, email, ssn, telephone, zipcode, url, regex, range , component, or variableName.

Is there a way to accomplish this in ColdFusion?

Jawbone answered 18/5, 2009 at 19:34 Comment(1)
I believe where the docs say, "component" in that list, you can literally enter "component" and it will evaluate whether or not the variable is a component. To check if it is a specific component class, use Sam Farmer's example below.Mime
A
8

You could also use IsInstanceOf(). Though you must still use the full path, it can also be used to determine inheritance or identify components that implement a particular interface.

<cfif IsInstanceOf(obj, "compath.Dog")>
   yes. it is a dog component {woof}
<cfelse>
    some other type of component 
</cfif>

<cfif IsInstanceOf(obj, "compath.AnimalInterface")>
     yes. it implements the animal interface
<cfelse>
     no. it must be vegetable or mineral ...
</cfif>
Alesiaalessandra answered 18/5, 2009 at 22:53 Comment(1)
Thanks! This is exactly what I'm looking for. It does actually allow you to use a relative component path for type comparison which is what I wanted.Jawbone
H
8

You could use GetMetaData to find the type. Some quick code:

<cfif GetMetaData(currentObj).type eq "compath.dog">
Helmet answered 18/5, 2009 at 19:56 Comment(1)
It looks like GetMetaData may hold the solution. The "type" property just says "component" but there are other properties that do have the full inheritance path such as "name" and "fullname". However, I thought there was a way to test for relative types instead of fullpaths. I may just be remembering incorrectly though.Jawbone
A
8

You could also use IsInstanceOf(). Though you must still use the full path, it can also be used to determine inheritance or identify components that implement a particular interface.

<cfif IsInstanceOf(obj, "compath.Dog")>
   yes. it is a dog component {woof}
<cfelse>
    some other type of component 
</cfif>

<cfif IsInstanceOf(obj, "compath.AnimalInterface")>
     yes. it implements the animal interface
<cfelse>
     no. it must be vegetable or mineral ...
</cfif>
Alesiaalessandra answered 18/5, 2009 at 22:53 Comment(1)
Thanks! This is exactly what I'm looking for. It does actually allow you to use a relative component path for type comparison which is what I wanted.Jawbone
T
3

you could use name or fullname from the getmetadata() function.

<cfif GetMetaData(currentObj).name eq "compath.dog">
    ...do something specific with dog objects...
</cfif>

or

<cfif GetMetaData(currentObj).fullname eq "compath.dog">
    ...do something specific with dog objects...
</cfif>

docs are here getmetadata() on what getmetadata() returns depending on the object type.

Triable answered 18/5, 2009 at 21:10 Comment(0)
L
1

Dan, feel free to rip the code out of MXUnit which does exactly what you need to do. We do it in our assertIsTypeOf() assertion. See here for details: http://code.google.com/p/mxunit/source/browse/mxunit/trunk/framework/MXUnitAssertionExtensions.cfc

The reason you're seeing the performance hit with isInstanceOf() most likely has to do with setting this.customTagPaths in your Application.cfc. I hit this myself and filed a bug on it recently. Hopefully it'll get fixed in CF10, whenever that is.

Lazarolazaruk answered 30/3, 2011 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.