How to test dynamically if an object supports a type?

J

joes

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
 
A

Andrew McDonagh

joes said:
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));
}
}
 
R

Roland

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_/ /__/
/ \ /_/ / \
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top