• Post Reply Bookmark Topic Watch Topic
  • New Topic

instanceof operator  RSS feed

 
Amit Madhoriya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am checking one condition like this :
if(ie.getSource instanceof ReviewMessagesController) {
}
This condition is suppose to be true. But its not getting true. I check the return value of ie.getSource(), its returning com.gpc.client.pointofsale.message.ReviewMessagesController$7$1@92539. I dont know what is the problem.

Method getSource() is returning object of Object class.

Any suggestions will be helpful.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you possibly have more than one class called "ReviewMessagesController"? Or maybe you have the class file for that class in several places in the classpath?
 
Amit Madhoriya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the reply. I checked with your suggestions but this is not the problem. If you will see the object again which is ReviewMessagesController$7$1@4566. It attaching two dollar signs with the object reference. Here is the code snippet again

My condition is running inside thread. When I am compiling the class "ReviewMessagesController" there are sevearl class files are generating for that. One of them is 'ReviewMessagesController$7$1'. I think because of thread these classes are generating. If this is the case then how can I compare my condition.

thanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dollar signs just mean that there are nested classes; it's quite normal to have those. But the class you care about for the instanceof is still ReviewMessagesController, and not one of its nested classes, right?

You could try to delete all existing classes (from everywhere in the classpath), and perform a clean build.
[ October 03, 2008: Message edited by: Ulf Dittmer ]
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
The dollar signs just mean that there are nested classes; it's quite normal to have those.

Well, I guess that is exactly the problem. The event is fired from within a nested class and therefore the instanceof check against the outer class fails since the nested class is no instance of the outer class.

Maybe you could turn the nested classes into static nested classes and perform the instanceof check against the static class names instead.

Reagrds,
Thomas
[ October 03, 2008: Message edited by: Thomas Thevis ]
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or preferrably, if possible, change the event so it will use the outer class as its source.

If you can't, then either use Thomas' advice, or check against the base classes / interfaces these anonymous classes override / implement.
 
Amit Madhoriya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for all of your suggestions. Anyhow I found the solution. The problem was the class "ReviewMessagesController" was firing a event ie inside a thread.. like this :

because of which "ReviewMessagesController$7$1" class was genereating. In this line we were passing "this" as current class object, which was referring to the "ReviewMessagesController$7$1" class object. Instead of this I passed the new MessageController() object and the problem get solved.

Thanks again
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Madhoriya:
Instead of this I passed the new MessageController() object and the problem get solved.


If you passed a new MessageController that you create only for this purpose, that's kind of an ugly solution, don't you think?

To refer to the instance of the enclosing class from within a nested class, you use the syntax "EnclosingClassName.this"; so probably the right solution here is

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:

If you passed a new MessageController that you create only for this purpose, that's kind of an ugly solution, don't you think?


I would in fact say that it's plain *wrong*, since the new instance is in fact *not* the source of the event. Future developers will curse you if they have to maintain that code...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!