问题
In eclipse you can set custom "Detail Formatters" under Preferences -> Java -> Debug -> Detail Formatters to print objects in custom ways when debugging. I'd like to use a utility class to print an object using a formatter like:
return com.foo.Bar.xzyToString(this);
where xzyToString is a static method of Bar returning a String, but eclipse complains that it
'could not resolve type: com.foo.Bar.xzyToString'. Adding 'Bar' to the project build path doesn't allow the class to be found. How/where do I update the path eclipse uses to resolve names in Detail Formatters?
回答1:
The detail formatter hooks into the Eclipse JDI framework and uses the classloader of the class currently being debugged. So, you can only use classes that your running application knows about.
Since you only need your utility class while debugging, you can add the class to the classpath of your launch configuration (or add it directly to your project). Unfortunately, I don't know a way of doing this automatically for all launches.
回答2:
The class also needs to have been loaded before the detail formatter is called. If it's a class that you use elsewhere in your app, this often won't be a problem, but if you use it only for detail formatting, you will always get that error.
You can get around this by loading the class in the formatter - change your formatter to the below:
if ( Class.forName("com.foo.Bar") != null ) {
return com.foo.Bar.xzyToString( this );
}
else {
return "Could not load com.foo.Bar in detail formatter!";
}
回答3:
Just give the part after your return
statement. E.g. for StringBuffer
try:
toString() + " (" + length() + ")"
来源:https://stackoverflow.com/questions/3556925/how-does-the-debug-detail-formatters-resolve-classes-in-eclipse