This week's book giveaways are in the iOS and Features new in Java 8 forums. We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line! See this thread and this one for details.
The only reason why I don't want it to be a parameter is purely aesthetic. Basically foo() is called from within a method. My goal is to be able to identify in which method foo was called from. So currently I am indicating that by an integer. Another option would be to somehow identify my enclosing method by reflection or something.
Originally posted by Pho Tek: Basically foo() is called from within a method. My goal is to be able to identify in which method foo was called from.
That's sounds generally like a bad idea to begin with. Can you tell us more about why you want to do that?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
if your only reason for wanting to find out the calling method is to get at that integer, and your only use for the integer is to identify the calling method, then you definitely want either reflection or the Exception.getStackTrace() trick. probably the latter.
Joined: Jul 11, 2001
Originally posted by M Beck: then you definitely want either reflection or the Exception.getStackTrace() trick. probably the latter.
Except that, as far as I know, it isn't guaranteed to work.
Joined: Jan 14, 2005
Originally posted by Ilja Preuss: Except that, as far as I know, it isn't guaranteed to work.
well, drat, that means no method i know of is guaranteed to do this. i recommended getStackTrace() over reflection because, one, reflection is a large and complex additional API to worry about whereas exceptions are around in any event; and, two, security managers can at times override parts of the reflection API, including for all i know perhaps the part that would help with this. but if getting a stack trace from an exception might randomly fail, too, then i dunno how to do this reliably.
Joined: Jul 11, 2001
Originally posted by M Beck: well, drat, that means no method i know of is guaranteed to do this.
As far as I can tell, that is because there is none...
It's a bad design idea, anyway - it provides a type of coupling that you simply should try to avoid. If at all, it's good for debugging kind of things, such as logging - and for those, it doesn't hurt that much that it isn't guaranteed to work reliably...