powered by Jive Software

Determining type of IQ packet

I’'m creating message handler for the IQ packet. There does not seem to be a method on the IQ class that returns whether the IQ packet is a Registration packet, an Authentication packet, a Roster packet, or some other IQ packet.

I could use “instanceof” to test what kind of subclass the IQ packet is, but that’'s kind of ugly.

There is a getQueryXML() method which returns the XML containing the info I need to determine the type of packet, but I would have to parse it manually.

Does anybody know of a good way of determining the type of IQ packet?

Thanks,

Aman

i would advocate using instanceof despite any aesthetic qualms.

loki

You could also set up separate packetlisteners with the appropriate PacketTypeFilter for each one.

Of course, if you look at PacketTypeFilter, it uses Class.isInstance()…

Just out of curiosity (being a relative Java newbie), why would using instanceof be considered “ugly”. Is it the Java equivalent in reputation to “GoTo”?

John

That’‘s a good idea; I didn’‘t know why I didn’'t think of that.

I think it’‘s considered “ugly” because it doesn’'t properly use polymorphism to determine the type of class. For example, a “getType()” method could be provided by the superclass which subclasses overload and return the proper type. Of course, “getClass()” is one such method, which is why your idea is good.

I don’'t see anything wrong with instanceof for this use case.

-Matt