Christian
Christian

Reputation: 22343

Check which class has called function

How can I check which class has called my method?

For example: If Class A uses the MethodB in the Class C than, the function should do something else than the function would do, if Class B calls the MethodB.

I can't add a boolean or something like that to the method.

Upvotes: 8

Views: 4090

Answers (6)

Soroush Lotfi
Soroush Lotfi

Reputation: 602

class A {
     B().foo()
}

class B {
     fun foo() {
         val className = Throwable().stackTrace[1].className.substringAfterLast('.')
         Log.d(TAG, className) //prints "A"
     }
}

Upvotes: 1

Kia Khoshvaght
Kia Khoshvaght

Reputation: 1

You can print stack trance for that manner:

@Override
public void finish() {
    new Exception("").printStackTrace(); 
}

Upvotes: 0

Ichigo Kurosaki
Ichigo Kurosaki

Reputation: 3843

I am answering too late. You can use Thread.dumpStack() to check who is calling your function.

suppose you want to check who is calling function ABC(). Then you can write the code as follows:

function ABC(){
   Thread.dumpStack()
   ......
   .....
   Rest of your code

}

When Thread.dumpStack() will get execute, then it will print a stacktrace.
You can refer this link
Note that, this function is not only limited to threads, it can be used anywhere/

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

It is possible to find the name of the class for a calling method. Here is how you can achieve it.

class A {

    public void testMethodCall() {
        new C().testMethod();
    }
}

class B {
  public void testMethodCall() {
      new C().testMethod();
  }
}

class C {

    public void testMethod() {
        System.out.println("Called from class : " +Thread.currentThread().getStackTrace()[2].getClassName());
    }
}
public class Test
{
    public static void main(String args[]) {
        A a = new A();
        B b = new B();
        a.testMethodCall();
        b.testMethodCall();
    }

}

Output

Called from class : A

Called from class : B

You can use this sample code to adapt to your need.

Upvotes: 5

RamonBoza
RamonBoza

Reputation: 9038

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()  

javadoc:

The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.

and the documentation of what you can get from each StackTraceElement

http://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.html

getClassName()
getFileName()
getLineNumber() 
getMethodName()

Upvotes: 5

Jon Skeet
Jon Skeet

Reputation: 1503529

There's no good way of doing this - and it's fundamentally a bad design, IMO. If ClassA and ClassB want different things to happen, they should call different methods.

The only time this is reasonable in my experience is when trying to work out a simple way of initializing loggers, where basically you want the calling class name to be part of the logger name. One horrible way of doing that is to throw and catch an exception, then look at its stack trace. But avoid if possible...

Upvotes: 6

Related Questions