How can I test whether an Object implements a certain Interface that is
provided as Interface.class? I've tried the following:
====================================
boolean test(Object interfaceObj, Object testObj) {
//return true if testObj implements interfaceObj
}
test(MyInterface.class, someObj);
====================================
I think I've tried all variations of instance of and getClass() but
somehow I can't find the solution. The problem is with the fact that
I'm using MyInterface.class instead of an object.
Any suggestions?
Cheers,
cyberco
Vova Reznik - 23 Feb 2006 19:52 GMT
> How can I test whether an Object implements a certain Interface that is
> provided as Interface.class? I've tried the following:
>
> ====================================
> boolean test(Object interfaceObj, Object testObj) {
//return true if testObj implements interfaceObj
return (testObje instanceof interfaceObj);
> }
> test(MyInterface.class, someObj);
[quoted text clipped - 8 lines]
> Cheers,
> cyberco
su_dang@hotmail.com - 23 Feb 2006 20:07 GMT
how about
return testObj.getClass().isInstance(interfaceObj);
Su Dang
cyberco - 23 Feb 2006 20:16 GMT
that's the wrong way around (if I'm correct)
su_dang@hotmail.com - 23 Feb 2006 19:52 GMT
Is it a requirement that testObj has to implement ALL the interfaces
interfaceObj implements?
Su Dang
> How can I test whether an Object implements a certain Interface that is
> provided as Interface.class? I've tried the following:
[quoted text clipped - 14 lines]
> Cheers,
> cyberco
cyberco - 23 Feb 2006 20:01 GMT
Coincidently I just bumped into what might be the solution:
========================
return interfaceObj.getClass().isAssignableFrom(testObj.getClass());
========================
This tests whether interfaceObj is the same or a superclass/interface
of testObj. Quoting the API doc:
"Determines if the class or interface represented by this Class object
is either the same as, or is a superclass or superinterface of, the
class or interface represented by the specified Class parameter. It
returns true if so; otherwise it returns false. If this Class object
represents a primitive type, this method returns true if the specified
Class parameter is exactly this Class object; otherwise it returns
false."
I guess I learned something new today :)
Thanks,
cyberco
Mike Schilling - 23 Feb 2006 21:19 GMT
> Coincidently I just bumped into what might be the solution:
>
> ========================
>
> return interfaceObj.getClass().isAssignableFrom(testObj.getClass());
No. Use instanceof.
Patricia Shanahan - 24 Feb 2006 00:31 GMT
> How can I test whether an Object implements a certain Interface that is
> provided as Interface.class? I've tried the following:
[quoted text clipped - 14 lines]
> Cheers,
> cyberco
It would be easier if you could guarantee that the first test argument
is a Class:
boolean test(Class interfaceClass, Object testObj) {
return interfaceClass.isInstance(testObj);
}
In what cases will you need to call it with the interface NOT
represented by its Class?
Patricia
cyberco - 25 Feb 2006 12:03 GMT
Patricia: actually, I CAN be sure that the method is always called with
a Class, so your suggestion (changing the interfaceClass parameter from
type Object to type Class) is indeed a good solution. Thanks for
pointing that out!