views:

326

answers:

7

In Eclipse, I would like to set a breakpoint on a Java default constructor. I can't simply double click to the left of any line of code since default constructors have no source code - they are implicitly generated by the Java compiler.

I'd like to be able to set such a breakpoint without modifying the existing code.

+1  A: 

You can always create your own no-argument constructor and put the breakpoint there. More to the point, though, why do you want a breakpoint there? It will simply chain to the no-argument super(). If that has code you care about, put the breakpoint inside that constructor.

Hank Gay
That would break for every subclass created as well, may not be suitable.
Grundlefleck
+4  A: 

EDIT 1: Re-written from scratch after question was updated.

EDIT 2: Added another possible solution

Solution 1: member initializers

If you have any member variables with initializers, then you can put a breakpoint on them. For example:

class MyClass {
  private int i = 0; // this line can have a breakpoint in Eclipse
}

Solution 2: class load breakpoints

If you can get by with only hitting this breakpoint once, then you can use a class load breakpoint:

You can set a Class Load Breakpoint, which will stop when the class is being lodaed [sic]. Right-click on a class in the Package Explorer, Project Explorer, or Types view and choose "Toggle class load breakpoint"

As the name implies, this breakpoint will be hit when the class is first loaded, so it will only fire once (assuming you only have a single classloader). But depending on your needs, it might be good enough.

Matt Solnit
Yeah, ok, but that's still sorta, kinda kludgy - the default constructor exists, it runs, right? :) It seems reasonable to be able to break on it. I realize that it doesn't exist in the source, so the debugger would have to somehow signal that it is on "synthesized" code without a line number.
Greg Mattes
+1  A: 

you can do the following:

public class MyClass {

    public MyClass() {
       super();
    }

}

And then put the break point on that. However, what are you hoping to accomplish by this?

Zack
+1  A: 

How about creating a public no-argument constructor and setting a breakpoint on that? If that won't help, could you elaborate why not?

rsp
I'd rather not touch the code. It wasn't written by me, and it has some O/RM framework stuff going on that I'd rather not take the chance of disturbing if I can avoid it.
Greg Mattes
A: 

You could consider the YouDebug framework, in order to script your debug session, including a breakpoint on any specific method of any class.

Breakpoints are event callback handlers that are invoked when a certain event occurs in the target JVM. You can create breakpoints by calling breakpoint methods on the 'vm' object. These methods takes a closure that gets invoked when the event occurs, and they often takes additional arguments to control the nature of the breakpoint.

The following code defines a breakpoint on line 7 of the org.acme.SubStringTest.java (or whichever source file this class is defined in:)

vm.breakpoint("org.acme.SubStringTest",7) {
  println "I'm at SubStringTest.java line 7";
}
VonC
A: 

Your best bet is probably using an aspect based framework to inject the functionality you need in the classes.

I thought Eclipse provided a default constructor breakpoint, but alas only the class loaded breakpoint in the outline.

What problem do you need to actually solve?

Thorbjørn Ravn Andersen
+2  A: 

If you really need it, set a method breakpoint in one of the other methods of the class, select the breakpoint (Breakpoints view) and export it. Edit this file so the breakpoint points to the standard constructor. At least the following attrib's must be changed (Galileo):

  • org.eclipse.jdt.debug.ui.JAVA_ELEMENT_HANDLE_ID
  • org.eclipse.jdt.debug.core.methodName - value="<init>"
  • org.eclipse.jdt.debug.core.methodSignature - value="()V"
  • message - no idea if that is really needed

probably easier to also export a constructor breakpoint from an other class to see the correct values. Now import the changed file and you should have your constructor breakpoint.

It's a hack, but worked for me...

Carlos Heuberger