I have updated the NetBeans 6.0 compatible LineTools module to NetBeans Plugin Portal. I have removed the Move/Copy Selection/Line Up/Down actions from this module as they are now part of NetBeans 6.0 editors. I have checked in the updated sources in here.

Saturday Jun 02, 2007

If you develop NetBeans modules you must be aware of layer files. The layer files are the NetBeans module system's mechanism for declarative registration of almost all aspects of module's functionality. The overall functionality of a NetBeans Platform based application is derived by merging the layer files contributed by all the modules in the application.

The NetBeans modules can be developed using the NetBeans IDE itself, using the NetBeans module projects. The NetBeans module project's logical view has the following nodes related to the layer file(s) (see the first screen shot below).

Important Files/XML Layer/<this layer> - This node's children display the structure of the current modules layer file content.

Important Files/XML Layer/<this layer in context> - This node's children display the structure of the merged layer file that will be effective when the module runs inside the NetBeans Platform based application. The layer files of the other modules in the module's suite or the NetBeans platform against which it is being deverloped are used to build this structure.

Sometimes it is desirable to quickly find out which layer file(s) a particular node comes from. Starting with today's (6/2/2007) build there is a context menu action called Open Layer File(s) just for such a pupose (see the screenshot below).

When the Open Layer File(s) action is invoked on a particular node, the layer file(s) which declares that node is opened in the editor window. Please note that a particular node (such as Actions folder) may be declared in more than one file, in which case all the layer files will be opened in the editor window. The layer file may be read-only if it comes from a module's jar file in the NetBeans platform against which this module is being built.

In the above screen shot the layer file of the debuggercore module project was opened because the Actions/Debug/New Breakpoint... action is declared by it.

The layer file may be owned by other module's in this modules suite. For modules in the NetBeans workspace the layer file may be owned by other NetBeans modules. In those cases you can use the Select in > Projects action in the layer file editors context menu to quickly open the owner project and select the layer file in it's local view. If that module project is not already open you will be prompted to open it as shown in the screen shot below.

Monday May 21, 2007

Note: This functionality is available in recent (last weeks) NetBeans IDE build with JDK 6.

Here is the explaination of what is going on:

The screenshot shows the debugger stopped at a line in the contructor of class Foo. Foo has a member variable bar of type Bar which is an inner class of Foo. The Local Variables window show the this instance of type Foo and bar field of type Bar. Nothing new or interesting so far. Right clicking on the bar field's row in theLocal Variables window reveals the Show References action in the popup menu. Invoking the Show references action shows the Instances window. The Instances window is the interesting bit. The Instances window shows three panes - Instances pane, Fields pane and References pane. The Instances pane shows all instances of type Bar. The Fields pane is analogous to the Local Variables window and shows the structure of the instance of Bar. The References pane shows the references to the selected instance of Bar. In this example the only reference to the instance pointed to by the bar field is the instance of Foo. I think this is huge! What this means is that I can look at the references to the objects while I am debugging my program. The ability to look at the references has been traditionally available in the Profiler tools such as HeapWalker. The differences is that the HeapWalker tool shows the static information from a heap dump file. The Instances window above shows the information from a live VM that is being debugged. This brings the power of Profiler like tools to the NetBeans debugger. I think this is very synergistic. For example, normally one can only look at the objects visible from the current stack frame in the Local Variables window. Using the Instances pane in the Instances window now it is possible to look at other instances of any reference type that may not be directly visible from the current stack frame. Similarly by using the fix&continue feature of the debugger it will be possible to debug memory leaks in certain scenarios.

There is also a Classes window which can be invoked using Windows:Debugger:Classes action. This is analogous to Classes window of the HeapWalker.

Small nit:

The #nnn numbers shown in the Instances pane of Instances window are not the same ad Unique Id# shown in the Local Variables window. I have already filed an RFE to display the debuger assigned Unique ids in the Instances window so that it will be easy to corelate the information those two windows.

The Fields pane shows information very similar to the Local Variables window. The two GUIs could be better unified.

Going forward I hope to see more and more integration of NetBeans debugger and profiler which will help integrate the profiling in the regular edit-compile-debug cycle.

Type the key sequence in the textfield on top of the first column to see what it is bound to.

Search based on raw keys (no modifiers)

Type the keys (without modifiers) in the textfield on top of the second column to see what the they are bound to.

Search based on action name

Type the name of the action in the textfield on top of the third column to see what key sequence is bound to it. You can use regexps in the action name.

You can sort the columns. You can do primary, secondary and tertiary sort by control clicking on the column headings. You can generate the html or xml file. You could use an xml+XSLT to generate nice PDF reference card. If anyone develops such a tool please send me the information.

The factory is registered under the org-netbeans-modules-java-hints/rules/errors folder because it creates a fix for certain compilation errors such as type mismatch of the left and right side of a vatriable declaration statement.

Implementation of the factory

As the user edits the source code, the Java editor incrementally runs the javac in the background. As the javac emits compilation errors the editor shows the red squiggly lines. Aside from that it also invokes the factories registered to handle the particular compilation errors. In this case org.netbeans.modules.javahints.ChangeTypeCreator is one such factory. The ChangeTypeCreator implements the interface org.netbeans.modules.java.hints.spi.ErrorRule. This intreface essentially has two important method:

/\*\* Get the diagnostic codes this rule should run on \*/ public Set<String> getCodes();

The getCodes() method essentially indicates the set of compilatioin error codes that are handled by the factory. The ChangeTypeCreator handles:

compiler.err.prob.found.reqcompiler.err.incomparable.types

These codes are part of standard error codes emitted by the javac.

In the run(...) method of ChangeTypeCreator,enough information about the context in the editor is passed in. The ChangeTypeCreator uses the APIs to determine the types of expressions on the left and right side of a variable assignmenets and returns an instance of org.netbeans.modules.javahints.ChangeTypeHint. This instance is used to populate the list of fixes that appear as actions in the light bulb pop up menu in the left margin of the editor.

Implementation of the Change variable type fix

The ChangeTypeHint which implements org.netbeans.spi.editor.hints.Fix . It provides the text of menu item which is returned from:

Friday May 04, 2007

In NetBeans 6.0 Java editor supports writing custom, so called, fixes, hints and suggestions. These appear as a light bulb icon in the left margin of the Java editor. For example, in the code below:

100 List list = ...;101 ::110 String s = list.get(0);

the type of the variable s does not match the type of the expression list.get(0). In such cases a light bulb icon appears in the left margin of the Java editor on the line 110 of the code. Clicking or invoking the fix action reveals a menu item saying Cast ...get(...) to String. Selecting the menu item changes the code on line 110 to:

110 String s = (String) list.get(0);

This is all well and good. However what if you wanted to adjust the type of the variable s to Object. It is very easy to write such a fix and that is what I have implemented.

With this fix, I always write my variable like this:

110 int i = .....; // some expression that return a type I don't know - assume it is RefactoringPluginFactory

Select the hint to change the type of i to the correct type.

110 RefactoringPluginFactory i = ....;

(I know, I know some may say why I am doing this...because I am an extremely poor typist :( )Then I quickly delete i and use the smart variable name proposal code completion to get:

110 RefactoringPluginFactory refactoringPluginFactory = ....;

I have added the fix to the Experimental Java Hints (contrib/editorhints/java) module. You can get the module from Development Update center.

Saturday Apr 14, 2007

In this blog entry I talked about how to use NetBeans debugger breakpoints to trace your application functionality.

In the newer trunk (6.0) builds of NetBeans debugger it is possible to view the values of method parameters even if the debug information is missing. What this means is that you can also debug into the JDK libraries and look at the parameter values. NOTE: AFAIK this only works if you are running the debugee with JDK 6.0.

Using the above two features it is possible to explore and/or debug class loading problems in a multi classloader applications such as NetBeans IDE, App server. So what is the trick?

As we know - in Java - the classes are loaded by classloaders. Ultimately any class is defined using the following method of the java.lang.ClassLoader :

By setting the breakpoint in this method and customizing the Print Text field of the breakpoint it is possible to generate the trace of class loading. Also note that the Suspend setting is set to No thread (continue). That way the debugee does not stop every time the breakpoint is hit. The below screenshot shows the required breakpoint customization:

I used the above breakpoint on one of my modules. The same technic can be used for App Server. Here is the output I got in the debugger console:

As you can see it shows exactly which one of the NetBeans classloaders loaded which classes. Formatting this information as a comma separated list and loading it into a spreadsheet one can analyse the class loading further. For example - same class laoded by two different class loaders or a class loaded by unexpected classloader and so on. Customizing the Breakpoint condition you can go after a particular class (e.g. name.equals("packagename.classname") or classes from a cetrain package (e.g. name.startsWith("packagename.") .

I think there are many more cool features coming up in the NetBeans debugger. You will be able to learn about these new features at NetBeans Day (May 7th) and JavaOne 2007 (May 8th - 11th) in San Francisco. I know I will be there :)

Monday Mar 26, 2007

The NetBeans has had an API to find out what projects are open in the IDE. See OpenProjects. Also there is an API to monitor the opening and closing of the projects. Basically, you have to register a PropertyChangeListener and listen for PROPERTY_OPEN_PROJECTS property changes. However due to the issues #98894 and #64397 this was harder. The problem was that the property change event did not populate the old and new value of open projects array. Therefore the client code had to cache the open projects array by calling the OpenProjects.getDefault().getOpenProjects(), and when the property change event was received call the the OpenProjects.getDefault().getOpenProjects()again and compute opened and closed projects by comparing with previously cached array.

Thanks to very recent bug fixes for issues 98894 and #64397 in NetBeans CVS trunk, the task of monitoring the opening and closing of projects has become really simple. Here is the code: