How to test dynamically if an object supports a type?

Discussion in 'Java' started by joes, Feb 20, 2005.

  1. joes

    joes Guest

    Hello

    I am looking for a way in Java how I can test dynamically if an object
    is from a given type or interface. Normally you can use the
    "instanceof" operator but the operator requires a type information
    which can not be a parameterized.
    i.e.

    //example1
    boolean testIfSameType(Object o1, Object o2)
    {
    return (o1 instanceof o2); // ends in a compilation error

    //examle2
    boolean testIfSupportsType(Object o1, Class service)
    {
    return (o1 instance of service); // also of course not possible...
    }


    Before I like to spend some time to write my own "instanceof" I like
    to hear if there are any other ways of some experts... i.e. new ways
    in Java 1.5 ?

    If I have to program myself I would go with Java reflection and
    comparing the class names which is of course not efficient nor it is
    easy to realise own support of automatical super typing (extended
    interfaces and classes) and security management.
    Exists there already some libraries which may provide my needed
    functionality?

    Any helpful hints or exeperiences are welcome...

    Thank you regards
    Mark Egloff
     
    joes, Feb 20, 2005
    #1
    1. Advertising

  2. joes wrote:
    > Hello
    >
    > I am looking for a way in Java how I can test dynamically if an object
    > is from a given type or interface. Normally you can use the
    > "instanceof" operator but the operator requires a type information
    > which can not be a parameterized.
    > i.e.
    >
    > //example1
    > boolean testIfSameType(Object o1, Object o2)
    > {
    > return (o1 instanceof o2); // ends in a compilation error
    >
    > //examle2
    > boolean testIfSupportsType(Object o1, Class service)
    > {
    > return (o1 instance of service); // also of course not possible...
    > }
    >
    >
    > Before I like to spend some time to write my own "instanceof" I like
    > to hear if there are any other ways of some experts... i.e. new ways
    > in Java 1.5 ?
    >
    > If I have to program myself I would go with Java reflection and
    > comparing the class names which is of course not efficient nor it is
    > easy to realise own support of automatical super typing (extended
    > interfaces and classes) and security management.
    > Exists there already some libraries which may provide my needed
    > functionality?
    >
    > Any helpful hints or exeperiences are welcome...
    >
    > Thank you regards
    > Mark Egloff


    No need to roll your own, just use the Class object ....

    the following works....hth

    public class InstanceofTests extends TestCase {

    public void testIfSameType() {
    assertTrue(testIfSameType("", ""));
    assertTrue(testIfSameType(new JTable(), new JTable()));
    }

    //example1
    boolean testIfSameType(Object o1, Object o2) {
    return o1.getClass().isInstance(o2);
    }

    public void testIfSupportsType() {
    assertTrue(testIfSupportsType(new JTable(), JTable.class));
    assertTrue(testIfSupportsType("", String.class));
    }

    // examle2
    boolean testIfSupportsType(Object o1, Class service) {
    return (o1.getClass().equals(service));
    }
    }
     
    Andrew McDonagh, Feb 20, 2005
    #2
    1. Advertising

  3. joes

    Roland Guest

    On 20-2-2005 23:35, Andrew McDonagh wrote:

    > joes wrote:
    >
    >> Hello
    >>
    >> I am looking for a way in Java how I can test dynamically if an object
    >> is from a given type or interface. Normally you can use the
    >> "instanceof" operator but the operator requires a type information
    >> which can not be a parameterized.
    >> i.e.
    >> //example1
    >> boolean testIfSameType(Object o1, Object o2)
    >> {
    >> return (o1 instanceof o2); // ends in a compilation error
    >>
    >> //examle2
    >> boolean testIfSupportsType(Object o1, Class service)
    >> {
    >> return (o1 instance of service); // also of course not possible...
    >> }
    >>
    >> Before I like to spend some time to write my own "instanceof" I like
    >> to hear if there are any other ways of some experts... i.e. new ways
    >> in Java 1.5 ?
    >>
    >> If I have to program myself I would go with Java reflection and
    >> comparing the class names which is of course not efficient nor it is
    >> easy to realise own support of automatical super typing (extended
    >> interfaces and classes) and security management.
    >> Exists there already some libraries which may provide my needed
    >> functionality?
    >>
    >> Any helpful hints or exeperiences are welcome...
    >>
    >> Thank you regards
    >> Mark Egloff

    >
    >
    > No need to roll your own, just use the Class object ....
    >
    > the following works....hth
    >
    > public class InstanceofTests extends TestCase {
    >
    > public void testIfSameType() {
    > assertTrue(testIfSameType("", ""));
    > assertTrue(testIfSameType(new JTable(), new JTable()));
    > }
    >
    > //example1
    > boolean testIfSameType(Object o1, Object o2) {
    > return o1.getClass().isInstance(o2);
    > }
    >
    > public void testIfSupportsType() {
    > assertTrue(testIfSupportsType(new JTable(), JTable.class));
    > assertTrue(testIfSupportsType("", String.class));
    > }
    >
    > // examle2
    > boolean testIfSupportsType(Object o1, Class service) {
    > return (o1.getClass().equals(service));
    > }
    > }

    OP (Mark Egloff aka joes) also wanted to test against super types and
    interface types. For this, Class.isAssignableFrom(Class) can be used.
    The modified test then becomes:

    import java.io.Serializable;
    import javax.swing.JTable;
    import junit.framework.TestCase;
    public class InstanceofTests extends TestCase {

    public void testIfSameType() {
    assertTrue(testIfSameType("", ""));
    assertTrue(testIfSameType(new JTable(), new JTable()));
    }
    //example1
    boolean testIfSameType(Object o1, Object o2) {
    return o1.getClass().isInstance(o2);
    }

    public void testIfSupportsType() {
    JTable aJTable = new JTable();
    assertTrue(testIfSupportsType(aJTable, JTable.class));
    assertTrue(testIfSupportsType(aJTable, Serializable.class));

    String aString = "";
    assertTrue(testIfSupportsType(aString, String.class));
    assertTrue(testIfSupportsType(aString, Serializable.class));
    assertFalse(testIfSupportsType(aString, Number.class));

    Integer anInteger = new Integer(0);
    assertTrue(testIfSupportsType(anInteger, Integer.class));
    assertTrue(testIfSupportsType(anInteger, Number.class));
    assertTrue(testIfSupportsType(anInteger, Serializable.class));
    assertFalse(testIfSupportsType(anInteger, String.class));
    }
    // examle2
    boolean testIfSupportsType(Object o1, Class type) {
    return type.isAssignableFrom(o1.getClass());
    }
    }
    --
    Regards,

    Roland de Ruiter
    ___ ___
    /__/ w_/ /__/
    / \ /_/ / \
     
    Roland, Feb 21, 2005
    #3
    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. MrB
    Replies:
    2
    Views:
    425
    =?Utf-8?B?Tmljaw==?=
    Jan 26, 2004
  2. Replies:
    12
    Views:
    4,003
    Simon Biber
    Jun 27, 2006
  3. Howard Gardner
    Replies:
    17
    Views:
    528
    Howard Gardner
    Apr 25, 2006
  4. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    707
    Skybuck Flying
    Oct 4, 2009
  5. David Mitchell
    Replies:
    10
    Views:
    260
Loading...

Share This Page