How to set a breakpoint on a default Java construc

2019-02-03 03:05发布

问题:

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:

If the code where you want to set a breakpoint in, is on the build path and not in your project itself, then if you open the Outline view, you'll see that the default constructor is present there, and you can right-click on it and choose Toggle Method Breakpoint.

Note that sometimes the default constructor is filtered out of the Outline view. In that case it can be included by changing the filter settings. This is done by going to Outline view menu → Filters... → and uncheding Synthetic members.

This is in Eclipse Indigo, I don't know how long this functionality has been around.



回答2:

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...



回答3:

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.



回答4:

I'd like to make a small improvement to the answer given by rsp, making it a bit easier, but I can't post comments yet.

If you create the default constructor (e.g. by pressing ctrl+alt+s and then c), place the breakPoint in the call to this constructor and then press ctrl+z to undo the creation of the default constructor you'll still have the breakpoint with no changes in the code.

Hope it helps



回答5:

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.



回答6:

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?



回答7:

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



回答8:

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?



回答9:

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";
}