1

I have the following code in an eclipse application:

import org.eclipse.swt.widgets.Listener; public class X { public void test() { Listener eclipseListener = new Listener() { public void handleEvent(Event evt) { System.err.println("starting"); Y.externalMethod(); System.err.println("finished"); } } } public class Y { public static void externalMethod() { System.err.println("in class Y"); } } 

When I run method test in class X, I get the following output:

starting

I don't understand why externalMethod didn't run in class Y and why control didn't return to class X (it never prints 'finished' or 'in class Y').

Any ideas about why externalMethod doesn't run? Are anonymous inner classes not permitted to call static methods outside their class? If so, why does this code compile?

1
  • What you've posted works fine if you just add eclipseListener.handleEvent(null); and run it. Can you generate an SCCE? May well find the actual problem in the process. Commented Feb 4, 2013 at 22:34

2 Answers 2

1

Instead of

 public void handleEvent(Event evt) { System.err.println("starting"); Y.externalMethod(); System.err.println("finished"); } 

you might have better luck with:

 public void handleEvent(Event evt) { System.err.println("starting handleEvent"); try { Y.externalMethod(); } finally { System.err.println("finished handleEvent"); } } 

That is,

  1. Put the method exit trace in finally
  2. Add method names to trace lines
Sign up to request clarification or add additional context in comments.

4 Comments

Very interesting idea. When I tried it, the code in the finally branch does execute. I also tried adding a catch Exception e around the externalMethod, but it didn't catch anything.
Then you need to do the same thing with external method. Maybe it is throwing a RuntimeException or Error?
When I added a try/catch/finally around the code in Y.externalMethod, none of them printed (not the code in the catch or the finally method).
you mean the entry to method was printed but the exit was not? maybe it got a jvm error? that's unusual though. can you post its code or debug?
0

The method handleEvent() is not invoked here. What you did is defining anonymous class and create an instance from it on the fly.

You need to register this listener (eclipseListener) to some event handler which will invoke the method handleEvent() when an event fires.

1 Comment

This is actually inside a much larger eclipse plugin event handler. The handleEvent method is executed when the listener is executed by clicking a button. When I execute the method, I get all debug lines until I call the static method in the external class. The static method in the external class has no parameters and it's first line is a debug message which does not print.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.