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

Discussion in 'Java' started by cyberco, Feb 23, 2006.

  1. cyberco

    cyberco Guest

    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
    cyberco, Feb 23, 2006
    #1
    1. Advertising

  2. cyberco

    Vova Reznik Guest

    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
    >
    Vova Reznik, Feb 23, 2006
    #2
    1. Advertising

  3. cyberco

    Guest

    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
    , Feb 23, 2006
    #3
  4. cyberco

    cyberco Guest

    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
    cyberco, Feb 23, 2006
    #4
  5. cyberco

    Guest

    how about

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

    Su Dang
    , Feb 23, 2006
    #5
  6. cyberco

    cyberco Guest

    that's the wrong way around (if I'm correct)
    cyberco, Feb 23, 2006
    #6
  7. "cyberco" <> wrote in message
    news:...
    > Coincidently I just bumped into what might be the solution:
    >
    > ========================
    >
    > return interfaceObj.getClass().isAssignableFrom(testObj.getClass());


    No. Use instanceof.
    Mike Schilling, Feb 23, 2006
    #7
  8. 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
    Patricia Shanahan, Feb 24, 2006
    #8
  9. cyberco

    cyberco Guest

    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!
    cyberco, Feb 25, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    3
    Views:
    15,651
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Phil Endecott

    Obj* ptr = new Obj(X)

    Phil Endecott, Jun 3, 2005, in forum: C++
    Replies:
    5
    Views:
    450
    Mark P
    Jun 3, 2005
  3. Shalabh Chaturvedi
    Replies:
    2
    Views:
    431
    Mike C. Fletcher
    Feb 20, 2004
  4. Matthew Thorley

    How do you convert a string obj to a file obj?

    Matthew Thorley, May 4, 2005, in forum: Python
    Replies:
    7
    Views:
    491
    Peter Otten
    May 4, 2005
  5. alf
    Replies:
    9
    Views:
    391
Loading...

Share This Page