Results 1 to 10 of 10
  1. #1
    hreodbeohrt is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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 :)

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.

  4. #4
    kennyman94 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default 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);
    }

    Quote Originally Posted by Junky View Post
    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.
    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 05:33 AM. Reason: combined it with my later comment and included a quote

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: find out where a function is called from.

    knowing what, when, where, and in what order a method being called
    For that use a debugger which will allow you set break points and examine the stack.

    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: find out where a function is called from.

    Quote Originally Posted by kennyman94 View Post
    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.
    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.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: find out where a function is called from.

    Quote Originally Posted by kennyman94 View Post
    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.
    That is just a debugging issue; if the code were correct, you wouldn't need this mechanism.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    kennyman94 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default Re: find out where a function is called from.

    Quote Originally Posted by JosAH View Post
    if the code were correct, you wouldn't need this mechanism.
    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.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: find out where a function is called from.

    Quote Originally Posted by kennyman94 View Post
    Although i would think that the mechanism would be exceedingly useful when tracking the operation of concurrent programs to possibly identify race conditions.
    Don't count on it; from the getStackTrace() API documentation for the Throwable class:

    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
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    kennyman94 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default Re: find out where a function is called from.

    @JosAH Thank you for making me aware of that.

Similar Threads

  1. Replies: 3
    Last Post: 07-27-2011, 02:07 PM
  2. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 10:00 PM
  3. Java equivalent of Linux 'find' function
    By porchrat in forum New To Java
    Replies: 5
    Last Post: 08-25-2010, 12:31 PM
  4. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  5. javascipt function is nt getting called
    By pankaj_salwan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-20-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •