test if obj implemenst interface (provided as interface.class)

C

cyberco

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
 
V

Vova Reznik

cyberco said:
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);
 
S

su_dang

Is it a requirement that testObj has to implement ALL the interfaces
interfaceObj implements?

Su Dang
 
C

cyberco

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
 
M

Mike Schilling

cyberco said:
Coincidently I just bumped into what might be the solution:

========================

return interfaceObj.getClass().isAssignableFrom(testObj.getClass());

No. Use instanceof.
 
P

Patricia Shanahan

cyberco said:
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

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
 
C

cyberco

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top