What you've got there won't handle classes that extend com.dross.MyClass.
You'll need to check the class of both supertypes and potentially interfaces
as well (though not for this specific case).

I wouldn't try and throw the native CF exception, as that could well change,
thereby breaking all your code with ClassNotFoundExceptions.  Those
exceptions should all be caught in development, because they're programming
bugs.  Just use a custom type.

Finally, if you move that validation to a method in your root component.cfc,
then all your classes will automatically inherit the ability to validate
java arguments.  The method would just need the object and a class/interface
name.

Other than that, I think you're on the right track, if validating the type
of the passed object is of great import.  Personally, I'm happy to just let
CF complain when I use an object in an inappropriate way, because it's not
of the right type, rather than explicitly checking when it's passed.  CF is
a loosely typed language, after all.

Cheers,
barneyb

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of David Ross
> Sent: Tuesday, June 29, 2004 11:36 AM
> To: [EMAIL PROTECTED]
> Subject: [CFCDev] passing java objects to cffunction
> 
> Is there a best practice for type validation when passing java objects
> to a cffunction? I posted this on my blog, but figured I'd 
> get a better
> response here.
> 
> <cffunction name="doSomeThingWithJava" access="public"
> returntype="void">
>     <cfargument name="javaObject" type="any" required="true" 
> hint="I am
> a java object, I must be of type: com.dross.myClass"/>
>     <cfset var someVar = 0/>
>     <cftry>
>         <cfif arguments.javaObject.getClass().getName() NEQ
> "com.dross.myClass">
>             <cfthrow/>
>         </cfif>
>         <cfcatch>
>             <cfthrow
> type="coldfusion.runtime.UDFMethod$InvalidArgumentTypeException" 
>                message="The argument javaObject passed to function
> doSomeThingWithJava() is not of type com.dross.myClass" 
>                detail="Check and make sure the argument you 
> are passing
> is of the correct type"/>
>         </cfcatch> 
>     </cftry>
> <!--- function body --->
> </cffunction>
> 
> Does anyone see a better way to do this?
> 
> -Dave
> 
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email
> to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' 
> in the message of the email.
> 
> CFCDev is run by CFCZone (www.cfczone.org) and supported
> by Mindtool, Corporation (www.mindtool.com).
> 
> An archive of the CFCDev list is available at 
> www.mail-archive.com/[EMAIL PROTECTED]
> 

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]

Reply via email to