Dynamic Type Checking... Is this possible?

I want to do a dynamic check for objec types in a datastructure. Something very similar to instanceof but using dynamic type parameter.

Syntactically speaking...

Instead of writing several methods like

publicboolean isOfTypeXYZ( )

{

return (thisinstanceof XYZ);

}

publicboolean isOfTypeABC()

{

return (thisinstanceof ABC);

}

...

I want one single method like

public isOfType ( Class a_type)

{

return (this ***isoftype**** a_type);

}

Where isoftype

can somehow dynamically figure out if the object is of given type (class)

For those who want to know why I'd need this kind of contrived behaviour let me give a simple example (similar to my real issue).

Suppose I have a swing GUI application which has several laid out components like Containers, JPanels, JToolBars, JMenuItems, JButtons, JToggleButtons, JRadioButtons etc.

Now starting from the JFrame in this containment hierarchy, I want to find all instances of JButtons or say all instances of JRadioButtons or say all instances of JMenuItem or all instances of JComponents ...

I do not want to write a special method for each possible type I might encounter.

I want a more generic method like

searchComponents( Class a_class)

that I can invoke by just changing its argument to convey the type.

Unfortunately getClassName() will not work as that gives the most derived type of the object. I want something very much like instanceof wherein I can check for an intermediate super class as well. (eg. All RadioButtons are AbstractButton and should return true for either type).

I tried to search for this in the forums but couldn't find something close to this. (There are several discussions about dynamic type casting which are different).

[2456 byte] By [007jba] at [2007-10-2 13:22:18]
# 1
Is this what you want?searchComponents( Class a_class) {// browse components...if (a_class.isInstance(component)) {// do something}}
leonida at 2007-7-13 10:59:01 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2
Or if you are comparing objects, you can use the instanceof operator.- Saish
Saisha at 2007-7-13 10:59:01 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...