4

I have a class:

MessageReceiver.java

that receives messages but can also produce messages indirectly (that then could potentially be redelivered to this class). I don't want to process messages that were sent with MessageReceiver.java in the stack trace. Is there a way to efficiently determine if the message I received was from MessageReceiver.java?

The following chain is possible:

MessageReceiver.java -> OtherClass.java -> MessageProducer.java -> MessageReceiver.java

3 Answers 3

7

I think this is what you're after:

Class<?> myClass = MessageReceiver.class;
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

for (StackTraceElement element : stackTrace) {
    if (element.getClassName().equals(myClass.getCanonicalName())) {
        System.out.println("class found in stack trace");
        break;
    }
}

If you're using or higher, you can also use the following

Class<?> myClass = MessageReceiver.class;
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

boolean found = Arrays.stream(stackTrace)
                      .map(StackTraceElement::getClassName)
                      .anyMatch(myClass.getCanonicalName::equals);

if (found) {
    System.out.println("class found in stack trace");
}
3
  • This is probably as good as it gets.. I was hoping for a isClassInStack(Class<?>) method
    – jsurls
    Jul 10, 2012 at 14:22
  • I was looking for something like that too. A find it strange that you can not the actual calling class, only the name. String matching is not pretty, but it looks like that's the way to do it. Jul 10, 2012 at 14:28
  • 2
    For Java 9, Stackwalker is probably a safer bet: docs.oracle.com/javase/9/docs/api/java/lang/StackWalker.html Nov 2, 2017 at 13:42
1

You could add a custom header to the message that indicates the original emitter. So MessageReceiver will emit messages by setting the header value to "MessageReceiver", and will discard all messages that have this specific header value.

2
  • But it may be N more classes deep than OtherClass.java, and I may not know the existence of the other classes. Basically OtherClass.java is a blackhole of code that may produce messages.
    – jsurls
    Jul 10, 2012 at 13:57
  • I don't think there is a difference. What you want is to detect who the original emitter is. Other potential emitter classes should first check this header value and only set it if not previously set by other class.
    – Mihai
    Jul 10, 2012 at 14:01
0

You could get the stacktrace from the Exception and simply loop them comparing class names.

Exception.getStackTrace() returns an array of StackTraceElements, which has a getClassName method.

http://docs.oracle.com/javase/6/docs/api/java/lang/Throwable.html#getStackTrace() http://docs.oracle.com/javase/6/docs/api/java/lang/StackTraceElement.html

Assuming I understood the question

1
  • I looked at this originally and think this may be my only option. I didn't want to have to iterate through the list just to find my class as this can get very inefficient depending on how large the trace may get.
    – jsurls
    Jul 10, 2012 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.