2

I am trying to write a small internal tool to use for debugging focus problems in Swing applications. Right now the debugger tool is built in to the app and enabled via a debug command-line argument.

What I'd love to do is have a separate, standalone Java application that can connect to another JVM and listen for certain events (in this case, PropertyChangeEvents on the KeyboardFocusManager).

Ideally, it should be able to do this without any changes to the application being debugged, so it can be used "on-demand" to debug focus issues in any Swing app. Sort of like how VisualVM can attach to any running VM.

Is this possible? What are some starting points? JMX? Some other IPC mechanism?

Joe Attardi
  • 4,381
  • 3
  • 39
  • 41

2 Answers2

4

You can use the java platform debugger architecture (JPDA)

http://download.oracle.com/javase/6/docs/technotes/guides/jpda/examples.html

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • This is just what I needed. Added a ModificationWatchpointEvent to KeyboardFocusManager.focusOwner, and it works like a charm. Thanks! – Joe Attardi Mar 23 '11 at 20:33
2

Not sure if this covers it, but.. Assuming the application is started with these flags:

-Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y,suspend=n

You can attach the debugger to the process remotely using Eclipse.

Gray
  • 115,027
  • 24
  • 293
  • 354
MarcB
  • 549
  • 6
  • 14
  • Right, but I want a standalone application that basically notifies you of all focus change events. Is it possible to do this programmatically with the debugging API? – Joe Attardi Mar 23 '11 at 19:03
  • focus change events are programmatic, that is why you have to have listeners to react to them, the JVM can't magically react to something that it knows nothing about. –  Mar 23 '11 at 19:07
  • 2
    Ah. The class MethodEntryRequest in the JDI (Java Debugging Interface) does that. Basically the flow is that you attach to the process, get an instance of the EventRequestManager, register your method entry request there and enable it. You still need those debug flags i posted above though. – MarcB Mar 23 '11 at 19:08
  • Response to above - Assuming the process is notified about the focus change events by the host OS, they can be intercepted once they're inside the application. – MarcB Mar 23 '11 at 19:10
  • @Jarrod: You can listen for all focus changes by adding a PropertyChangeListener to the KeyboardFocusManager: KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener("focusOwner", new PropertyChangeListener(...)); – Joe Attardi Mar 23 '11 at 19:11
  • @Joe that is still programmatic, which proves my point, you have to add something to your code, there is no magic way for the JVM to know what you are intending. –  Mar 26 '11 at 23:14
  • @Jarrod not sure why you keep saying I'm trying to do something by "magic". I solved my problem by having the second Java program attach to the first using JPDA, and then add a ModificationWatchpointRequest to the attached VM to listen for any changes to KeyboardFocusManager.focusOwner. The only changes I have to make to the program being debugged/monitored is simply have it listen for debugger connections. – Joe Attardi Mar 27 '11 at 05:00