Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Eclipse's Java debugger, how can I set a breakpoint on every object creation of some class?

I would like to have Eclipse's Java debugger debug-suspend the monitored process whenever an object of a particular type (class / interface) gets instantiated.

As a workaround, I can set breakpoints

  • on all constructors of that type
  • on all constructors of that type's supertypes and subtypes

but

  • this is awkward
  • neglects instantiation with the default constructor
  • may cause false-positive debug suspensions on object instantiations of classes I didn't want to suspend on.
like image 583
Abdull Avatar asked Sep 13 '12 18:09

Abdull


People also ask

How do you set a breakpoint in Java?

To define a breakpoint in your source code, right-click in the left margin in the Java editor and select Toggle Breakpoint. Alternatively, you can double-click on this position. The Breakpoints view allows you to delete and deactivate Breakpoints and modify their properties.

How does a debugger set breakpoint?

To set a breakpoint in source code: Click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.

How do I add a breakpoint to all methods in Eclipse?

Select all the methods using ctrl . Right click and select Toggle Method Breakpoint .

How do I add a breakpoint to all methods in visual studio?

Press F3 and then press F9 to add a breakpoint.


2 Answers

You shouldn't need to set breakpoints on superclasses and subclasses. If you put a breakpoint on every constructor of the class you are interested in then every time an object of that class or a subclass of it is created the debugger will hit one of the breakpoints.

This doesn't work for interfaces because they are never constructed. I can't think of a way of doing it for interfaces.

Remember that the default constructor only exists for a class if you don't define any constructor at all, so as long as you have at least one constructor it won't be possible to instantiate an object of that type without using one of the constructors you have written.

like image 58
gandaliter Avatar answered Nov 15 '22 17:11

gandaliter


See this answer of Carlos Heuberger on this thread.

You can also set a breakpoint on particular class load if you need it. See this tutorial for reference

like image 31
Alex Avatar answered Nov 15 '22 16:11

Alex