Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > test if obj implemenst interface (provided as interface.class)

Reply
Thread Tools

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

 
 
cyberco
Guest
Posts: n/a
 
      02-23-2006
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

 
Reply With Quote
 
 
 
 
Vova Reznik
Guest
Posts: n/a
 
      02-23-2006
cyberco wrote:
> 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);
> ====================================
>
> 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
>

 
Reply With Quote
 
 
 
 
su_dang@hotmail.com
Guest
Posts: n/a
 
      02-23-2006
Is it a requirement that testObj has to implement ALL the interfaces
interfaceObj implements?

Su Dang

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


 
Reply With Quote
 
cyberco
Guest
Posts: n/a
 
      02-23-2006
Coincidently I just bumped into what might be the solution:

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

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

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

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

 
Reply With Quote
 
su_dang@hotmail.com
Guest
Posts: n/a
 
      02-23-2006
how about

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

Su Dang

 
Reply With Quote
 
cyberco
Guest
Posts: n/a
 
      02-23-2006
that's the wrong way around (if I'm correct)

 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      02-23-2006

"cyberco" <> wrote in message
news: oups.com...
> Coincidently I just bumped into what might be the solution:
>
> ========================
>
> return interfaceObj.getClass().isAssignableFrom(testObj.g etClass());


No. Use instanceof.


 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      02-24-2006
cyberco wrote:
> 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
 
Reply With Quote
 
cyberco
Guest
Posts: n/a
 
      02-25-2006
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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Obj* ptr = new Obj(X) Phil Endecott C++ 5 06-03-2005 10:48 PM
How do you convert a string obj to a file obj? Matthew Thorley Python 7 05-04-2005 09:04 PM
Descriptors: why __get__(obj,typ=None) instead of __get__(obj,typ) Shalabh Chaturvedi Python 2 02-20-2004 08:26 PM
difference between Convert.ToString(obj) and CType(obj, String) Mark Kamoski ASP .Net 3 08-08-2003 11:09 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57