-1
class Foo{
    static void fooMethod1(){
        // do something
    }
    static void fooMethod2(){
        // do something
    }
    static void fooMethod3(){
        // do something
    }
}

class bar{
    public static void main(String[]args){
        Foo.fooMethod1();
        Foo.fooMethod2();
        Foo.fooMethod3();
    }
}

Using reflection, how to know (without knowing method name) that fooMethod1 || 2 || 3 is called (not from the current Thread)?

JohnJohnGa
  • 15,446
  • 19
  • 62
  • 87
  • 1
    possible duplicate of [Getting the name of the current executing method java](http://stackoverflow.com/questions/442747/getting-the-name-of-the-current-executing-method-java) – Kirk Woll Oct 01 '11 at 16:11
  • No idea what you mean by an "event" there. You want to be notified when any arbitrary method is called? If so, aspects will be your only hope (not that I'd recommend it). – Kirk Woll Oct 01 '11 at 16:13
  • you've confused me even more. Why would we be using a loop? – Kirk Woll Oct 01 '11 at 16:14
  • You want to call from the outside of the executing thread to find out what function it is executing – David Heffernan Oct 01 '11 at 16:15
  • @KirkWoll sorry. I want to know when a method is called at runtime (but not a specific one), in my example I will get "Foo.fooMethod1" then "Foo.fooMethod2" and "Foo.fooMethod3". – JohnJohnGa Oct 01 '11 at 16:16
  • Please write that in the question – David Heffernan Oct 01 '11 at 16:18
  • Reflection mechanism is a static structure analysis tool. I doubt it'll help you. Agree with Kirk Woll, you can instrument your code using AOP (check AspectJ for example - http://www.eclipse.org/aspectj/) to insert an aspect at execution join point. – mazaneicha Oct 01 '11 at 16:19
  • @KirkWoll it is not a duplicate. What to do if i'm not in the current Thread? – JohnJohnGa Oct 01 '11 at 16:30

1 Answers1

0

Given that you aren't writing some kind of debugging tool, perhaps you should have your threads talk to each other? Perhaps using an observer? To me, that seems like a much more straight forward, simple and easy-to-understand solution.

henko
  • 763
  • 7
  • 16