Sunday, May 29, 2011

Eclipse: Show all instances of selected Java Class

As mentioned in the Eclipse help Java Development User Guide Tips and tricks Debugging section, when debugging with JavaSE-1.6 you can see all instances of a particular class. You can invoke this functionality via the popup menu (All Instances...) of Variables view rows for reference type values or in the Java editor by positioning the cursor on or selecting a reference type expression. However it is not possible to see all instances of an arbitrary class. The All Instances plug-in adds this functionality. It can be invoked using the:
  • Run > All Instances of Java Class... 
  • All Instances of Java Class... toolbar buttons in the Variables and Expressions views
Once invoked it shows the following customized Open Type dialog of JDT.


Once you selects the class it adds the <Java Class> Instances expression to the Expressions view. Please note that instances of sub classes are also shown if the Show instances of subclasses checkbox is selected. For example, when java.io.OutputStream is selected it shows the instances of it's subclasses also. You can further filter out inner and anonymous inner classes using the additional check boxes. This functionality is not provided by Eclipse Java debugger's All Instances... action.


In addition, an expression showing java.security.CodeSource info for the class is also added if that information is available.



NOTE: The selected class (FQN) may have been loaded by different ClassLoaders. Entries for each class loaded by distinct Classloader are shown independently. This is helpful in debugging issues related to class loading and seemingly mysterious ClassCastExceptions.

These expressions are automatically removed from the Expressions view when the debug session terminates.

You can further explore the instances using the All References... popup menu item provided by the Eclipse Expressions view. This can help you track down who is holding on to the instance potentially causing a memory leak.

The functionality works only if you have a active Java debug session, running under a JavaSE-1.6 (and above) that supports the all instances retrieval functionality, selected in the Debug view.

You can install the plug-in from the plug-in's update site:

http://sandipchitaleseclipseplugins.googlecode.com/svn/trunk/AllInstancesUpdateSite

Please file any issues at:

http://code.google.com/p/sandipchitaleseclipseplugins/issues/list

It works in Eclipse 3.5.2, 3.6.x and 3.7.x.

Also available on Eclipse Marketplace!

Enjoy!