• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to retrive from WhichClass this method has been called  RSS feed

 
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an Idea, actually I recently faced a problem where I have a method in a class, and there are two type of operations can be done in this method depending on the Call type from which class this method has been called.

Could anybody throw some light on this factor.. I am giving an example

Now the problem summary is, Is there any way exists where we can get this information that which class called this method Add(); And according to this condition we can perform either doSomething1(); or doSomething2();.

Pls let me know if something is confusing.

This will be very usefull for me.


[ July 06, 2006: Message edited by: Ankur Sharma ]
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use instanceof

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx Satou kurinosuke ,

But think one scenerio, I am passing here my class instance, which can be misused further.

I had thought this scenerio earlier, but due to above reason I left this idea, because passing a Object may leads to misuse.

Any Other way around..???

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another method could be to pass the Class type of the caller :


You could use getName() instead of newInstance(), and check the name of the class.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't there any other way in reflection API's ???

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same thing is happening there we are passing an Object of this Class in the Called Class.

Which may leads to misuse of this object.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Same thing is happening there we are passing an Object of this Class in the Called Class. Which may leads to misuse of this object.

No, because you're only passing a reference to a Class, not to the object itself. How do you think misuse will happen ?
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Fine and Thankx for this reply,

Here we are using Interface. But what if we cann't use Interface.???

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here we are using Interface. But what if we cann't use Interface.???

We actually don't need it in the second sample. I just like using interfaces

 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taking it back to first principles:

1) You have a method which behaves differently depending on where it gets called from? THen it should be two methods. I don't like the sound of this at all.

2) If you want to find out the class which called this one, have a look at the Log4J code, it already does this. Basically you create an exception and then parse the stack trace to find the next step up but do not throw the Exception.

Dave
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) You have a method which behaves differently depending on where it gets called from? THen it should be two methods. I don't like the sound of this at all.

I don't either
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java 5 you no longer need to create an exception. You can use the getStackTraceElements from System (You don't need a Throwable).
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
me neither
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Satou kurinosuke,

Thankx a lot it's working, actually the constraint about you are talking :

You have a method which behaves differently depending on where it gets called from? THen it should be two methods. I don't like the sound of this at all.


This was my design constraint, that's why I cann't use two different methods.

Yes But David, said right, That's what really I want to know...

David could you please throw some more light..



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!