Scala for NetBeans Screenshot#6: Working on Debug
=== Updated Jan 30, 08: ==>
The cause of adding breakpoints to object context is relative to complilationInfo's CompilationUnit in java.source module, that the ComplilationInfo is set by JavaSource#moveToPhase after call Iterable extends CompilationUnitTree> trees = currentInfo.getJavacTask().parse(new JavaFileObject[] {currentInfo.jfo}), thus the returning CompilationUnitTree is generated by sun's java parser tool, it of course can not be parsed properly with "object" keyword.
To resolve it, I have to rewrite a scala.debug.projects module, which will be derived from debugger.jpda.projects module
===
=== Updated Jan 26, 08: ==>
Yes, I've got a better Scala project support, not only debug works on pure Scala or Scala+Java project, but also got an UI for defining Main class of the application. Actually, Scala project support is now almost same as J2SE project on NetBeans. The only problem is, if you add a breakpoint on statements of a Scala object, since the org.netbeans.api.java.source.TreeUtilities#scopeFor will return a scope of "COMPILATION_UNIT", which will return null when is called by scope.getEnclosingClass() in org.netbeans.modules.debugger.jpda.projects.EditorContextImpl#getClassName, so the breakpoint will be invalid. To get this fixed, EditorContextImpl needs to be patched.
According to Scala FAQ, Scala object gets compiled to 2 classes, for example:
object HelloObj {
val f = "one field"
val x = 3
}
We will get:
public final class HelloObj extends java.lang.Object{
public static final int $tag();
public static final int x();
public static final java.lang.String f();
}
public final class HelloObj$ extends java.lang.Object implements scala.ScalaObject{
public static final HelloObj$ MODULE$;
public static {};
public HelloObj$();
public int x();
public java.lang.String f();
public int $tag();
}
Writing “HelloObj.x()” in Scala is the same as writing “HelloObj$.MODULE$.x()”, but the former should probably be preferred for readability.
The problem here is, when you add breakpoint at val x = 3 in the source file, what will be the breakpoint's scope in these two compiled classes (which one?), why scope.getClassName() will return null? anyone know what's the magic here?
===
I'm working on debug feature for Scala on NetBeans. I copied a smallest set of classes from Java Debugger module, and hacked something to make debug working on a mixed project (a Java project with Scala source files). By default, NetBeans IDE will step through Scala source files that are called (see Greetjan's article: Stepping through Groovy in NetBeans IDE ), but you can't add breakpoints directly on Scala source file. So the first thing that I should work on is let the user can add/remove breakpoints in Scala Editor, and get NetBeans' debugger stopping at these breakpoints. After a full day hacking, I got this working.
To got mouse click on adding/removing breakpoints working, you can implement and register a MIMEType related "GlyphGutterActions" in layer.xml, so, org.netbeans.modules.editor.impl.GlyphGutterActionsProvider#getGlyphGutterActions(String mimeType) can lookup this action. GlyphGutterActionsProvider#getGlyphGutterActions will be invoked by org.netbeans.editor.GlyphGutter#GutterMouseListener#mouseClicked, that's it.
The screenshot shows a mixed Java/Scala project, the example code was copied from Fred Janon's blog, it's a pretty simple example. I added a breakpoint on Dog.scala's val sound="Woof woof!" and another breakpoint at function talk()'s println(hi + sound), then invoke debug, the execution stopped at these two breakpoints as I expected, and you can see the context, for example, the values of variable "sound" and "hi" on debugging windows (at the bottom)
Of course, you can step into, step to cursor line too.
I need to get the debug feature working on a pure Scala project, I have not tried yet, but it seems I need to get the Scala project module better before that.
Click on the picture to enlarge it
Posted at 01:07PM Jan 26, 2008 by dcaoyuan in NetBeans | Comments[4]


You are the man!
Posted by Steve Lianoglou on January 26, 2008 at 02:54 PM PST #
Keep up the good work!
Posted by Daniel Green on January 26, 2008 at 04:11 PM PST #
I tried the latest download from sourceforge. It's pretty nice -- congrats. It needs a slight bit of polish and it'd be really amazing, like creating packages. Are the latest sources being committed to the Netbeans mercurial repo?
Posted by Saem on January 29, 2008 at 05:56 PM PST #
Hi Saem,
The latest sources have been committed to mercurial repo, but the debugger module is not stable. You can try the new project module, but not the debugger module, which needs some patch to other NetBeans modules.
Posted by Caoyuan on January 30, 2008 at 05:07 AM PST #