Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 4 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Eclipse: Add Debug Statements to Code You Don't Own

At 10:24 PM on Jul 25, 2005, R.J. Lorimer wrote:

Okay, recently I posted on my frustration when working with certain libraries that are less than friendly to debuggers and extenders (*cough* javax.swing.text *cough*). One of the things I found that was really helpful in those scenarios was using System.identityHashCode() to give me a quick ID for an object programmatically that I could print out to the console to track an object instance (see General: When Debugging Get a Unique Identifier For Any Object ).

One of the tricky aspects of this, however, is when dealing with a lot of code that you don't own, you can't easily go changing it to add print statements to debug. Sure, you can still debug that code with breakpoints, but if the code in question is complex (like my favorite pain-to-debug example: SAX parsers) breakpoints are sometimes not the right solution. Besides, some code (such as UI event code) may need to have errors reproduced without affecting the execution (such as typing 3 characters, right mouse clicking, and hitting tab all without losing focus to reproduce a null pointer exception). In those cases, it would be nice to get in there, and throw some print statements in to see when code is hit (and potentially to print out any variable values at the time of the breakpoint). This can be done by 'cheating the system' in Eclipse a little bit, and using conditional breakpoints.

Conditional breakpoints are a handy feature in Eclipse that allow you to supply a condition contextually relative to the code being executed so that the breakpoint only stops when that condition is met. They can be really useful, and in a lot of cases they can make debugging a lot less tedious. They don't really help with what I am describing here exactly (at least not directly), but it is important to understand them. To create a conditional breakpoint for this code:

public void someMethod(String argument) {
 String a = getSomeValue();
 String b = getSomeOtherValue(); // conditional breakpoint on this line
}

... you add the breakpoint in your editor by either right clicking and adding it, or double clicking in the breakpoint gutter. Once installed, right click, select Properties for the breakpoint, and then enable a condition:

This breakpoint would then stop for the case when a, b, and argument are all equal. What we are wanting, however, has nothing to do with conditional breakpoints. We just want to add some print statements into a method we can change. We can use conditional breakpoints, however, to add arbitrary code during debugging. So, for instance:

By returning false, we ensure that the breakpoint won't actually stop, but instead when Eclipse is attempting to evaluate the breakpoint, it will unwittingly execute these lines of code, and continue on as if the breakpoint didn't exist.

Note one caveat - if you are already in a debugging session and step into the line, these lines won't be executed because Eclipse doesn't need to check the breakpoint condition since the thread is already suspended.

Give it a shot!

Until next time,

R.J. Lorimer
Contributing Editor - rj -at- javalobby.org
Author              - http://www.coffee-bytes.com
Software Consultant - http://www.crosslogic.com

1 . At 12:07 AM on Jul 26, 2005, John Deere wrote:
  Click to reply to this thread Reply

Re: Eclipse: Add Debug Statements to Code You Don't Own

Geez this is cool.
Great tip R.J.

Thanks.
2 . At 12:50 AM on Jul 26, 2005, Cedric Beust wrote:
  Click to reply to this thread Reply

Re: Eclipse: Add Debug Statements to Code You Don't Own

Interestingly, I posted this exact tip a few months ago, along with a few more:

http://beust.com/weblog/archives/000195.html

Definitely for specialized situations, but when you need it, it's invaluable.

--
Cedric
http://testng.org
3 . At 1:29 AM on Jul 26, 2005, Ray Cromwell DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Eclipse: Add Debug Statements to Code You Don't Own

(preface: no, I don't work for JetBrains, but there are enough IDEA users on this site, that I like showing the IDEA equivalents)

The same trick also works in IDEA, simply bring up the property of the breakpoint, and set a condition. The dropdown box has a list of other expressions you've typed in the past, or you can type a new one. IDEA completion facilities work here.

But IDEA has two other features that make debugging foreign code easier:

1) "Log expression to Console" checkbox/textfield does exactly what you want above, but without using the condition trick, and without tripping over the gotcha's (condition not being triggered)

2) Instance filters. IDEA can filter breakpoints based on the System.identityHashCode of a particular object. Thus, if you are debugging say, java.swing.text, and you looked at a bunch of Elements, only one of them which is suspect or wrong, you can tell IDEA to breakpoint in that object instance, instead of the class as a whole.
4 . At 8:37 AM on Jul 26, 2005, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: Eclipse: Add Debug Statements to Code You Don't Own

Ray,

Thanks for updating on IDEA. I am always fond of readers posting the same solution information about my Eclipse tips for IDEA (or Netbeans for that matter). They are invaluable - at some point I'd like to be able to show features that can be done in both with screenshots and information about both - but frankly I don't currently have the time or brain power :).

Log expression to console and instance filters sound really cool!

Regards,
Best, R.J. Lorimer

thread.rss_message