Results 1 to 10 of 10
- 10-21-2011, 12:17 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
find out where a function is called from.
Hey!
I've got a method in one class . This method is being used in another class, but also in my main program.
Is there any way to do a check within the method to determine where it was called from?
The easy fix is to change the main program, but im not supposed to.
Thanks :)
- 10-21-2011, 01:44 AM #2
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: find out where a function is called from.
What you'd have to do is pass an argument to said method indicating where it's being called from. Java has no built in functionality for checking where a method was called from. Unless you count forcing an exception to be thrown and caught and then looking at the stack trace, but that'd be a pain, not to mention being atrocious programming style.
- 10-21-2011, 02:38 AM #3
Re: find out where a function is called from.
Why do you think you need to know this? As a programmer writing a method you should not care where it is being called from. It should not affect the behaviour of the method. If you think it should then you have a smelly design.
- 10-21-2011, 04:23 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
Re: find out where a function is called from.
you don't have to throw an exception. just do this:
public void myDebugMethod(){
new Exception().printStackTrace();
}
no exception is thrown and the only way you can even tell that an exception was created is because of the stack trace info that was printed to System.err.
if you don't want it printed to System.err, then you can replace it with this:
public void myDebugMethod(){
new Exception().printStackTrace(System.out);
}
that is not always true. While the behavior of the method being called shouldn't be affected, the system as a whole can be screwed up if a method is being called at the wrong time or in the wrong context. I myself have had issues where knowing what, when, where, and in what order a method being called was absolutely CRUCIAL to finding and fixing bugs in (relatively) complex systems. Unfortunately, I am unable to think of such a situation off of the top of my head.Last edited by kennyman94; 10-21-2011 at 04:33 AM. Reason: combined it with my later comment and included a quote
- 10-21-2011, 06:33 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: find out where a function is called from.
For that use a debugger which will allow you set break points and examine the stack.knowing what, when, where, and in what order a method being called
Poking about with exceptions doesn't sound a good design to inflict on the end user who does not have any wish to fix bugs.
- 10-21-2011, 06:42 AM #6
Re: find out where a function is called from.
That is not the methods problem. That is the problem of the code calling the method. If the program crashes or some other unwanted behaviour occurs then it should be tracked as to why the code is executing at the wrong time and it should be fixed. You should not be fannying about in your method trying to foresee every possible problem that may occur when it is called and trying to code against those problems. Except where parameters are invalid but you still do not need to know where/when the method was called.
- 10-21-2011, 06:54 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 10-21-2011, 07:46 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
Re: find out where a function is called from.
if the code were correct there would be no need to debug. Unfortunately this is usually not the case. if only it were then there would be much fewer headaches when coding. Although i would think that the mechanism would be exceedingly useful when tracking the operation of concurrent programs to possibly identify race conditions.
- 10-21-2011, 09:40 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: find out where a function is called from.
Don't count on it; from the getStackTrace() API documentation for the Throwable class:
kind regards,Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this throwable is permitted to return a zero-length array from this method. Generally speaking, the array returned by this method will contain one element for every frame that would be printed by printStackTrace
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-21-2011, 09:25 PM #10
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
"javax.xml.transform.TransformerException: Could not find function: matches" help!
By omripe in forum XMLReplies: 3Last Post: 07-27-2011, 01:07 PM -
Calling function in Javascript- from other function
By jdigger in forum New To JavaReplies: 1Last Post: 02-27-2011, 09:00 PM -
Java equivalent of Linux 'find' function
By porchrat in forum New To JavaReplies: 5Last Post: 08-25-2010, 11:31 AM -
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
javascipt function is nt getting called
By pankaj_salwan in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-20-2008, 08:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks