sandip chitale's blog    Sandip Chitale's blog (scblog)
NOTE: I have moved many of my modules to NetBeans Plugin Portal . Please check there for latest versions of modules described on this blog.
20070109 Tuesday January 09, 2007

Declaration and Javadoc module for NetBeans 6.0

I have uploaded the Declaration and Javadoc module for Netbeans 6.0 to my NetBeans update center described here.

This module displays the declration and javadoc of the element under caret in Java editor, in the Declaration and Javadoc windows respectively. Use Window:Declartion and Window:Javadoc menuitems to show the Declaration and Javadoc windows respectively.

Declaration and Javadoc module for Netbeans 6.0 

How does it work?

In some sense it works similar to the WhichElement module. The Declaration and Javadoc module registers a caret aware task:

DeclarationAndJavadocTask implements CancellableTask<CompilationInfo>

using the

DeclarationAndJavadocJavaSourceTaskFactory extends CaretAwareJavaSourceTaskFactory - a factory for creating a task which is run every time the caret position in the Java editor changes. This factory is registered using the: META-INF/services/org.netbeans.api.java.source.JavaSourceTaskFactory file.

In the task it finds the Element at the caret position. Then it gets the Tree using the:

compilationInfo.getTrees().getTree(element);

The compilationInfo object was passed into the task's run(CompilationInfo compilationInfo)method. The above call may return a null Tree . This is the only tricky part. Why does this happen? Well...this happens when the Element at the caret position is not declared in the current file e.g. println() method in the above screenshot. To get the Tree for such Element, it has to be obtained in the context of file that contains the declaration of the Element. The following API is used to get the file where the Element is declared:

FileObject fileObject = SourceUtils.getFile(element, compilationInfo.getClasspathInfo());

Another Javac task fo that file is executed to obtain the Tree for the Element's declaration. It must be noted that one needs to use the ElementHandle for the original Element and resolve it to Element in the new Javac task's context. See Java Developer Guide for more details on this. 

Once the Tree for the Element is obtained it is matter of calling the toString() on it to get the declaration.

The Javadoc is obtained from the Element using the following call:

compilationInfo.getElementUtilities().javaDocFor(element);

The declaration and the javadoc are displayed in the Declaration and Javadoc windows respectively. This is done on the AWT thread using SwingUtilities.invokeLater(). One small trick is to set the content type of the read-only JEditorPanes that display declaration and javadoc to text/x-java and text/html respectively. This enables the syntax coloring of Java code and html formatting of javadoc.  

Sources

DISCLAIMER: This module is experimental. So no guarantees. Use the module at your own risk.


Posted by sandipchitale ( Jan 09 2007, 11:51:41 PM PST ) Permalink Comments [2]


Trackback URL: http://blogs.sun.com/scblog/entry/declaration_and_javadoc_module_for
Comments:

This is great, but it is possible to see the JavaDoc as a tooltip? Similar to how Visual Studio does it.

Posted by Abraham Tehrani on January 11, 2007 at 09:30 AM PST #

Thansk.

Yes...just position the caret at the end of the method and type CTRL+SPACE to invoke code code completion popup as well as Javadoc popup. This may match multiple overloaded methods, so you may have to select the right one in the code completion popup. If there is only one method then you have to move the caret to the left by one as in such cases the code completion may not be shown as there is only one completion (bummer). What this means is that there should be a separate explicit way to invoke the Javadoc popup.

You can also use Alt+P to show the list of parameters of the method.

Besides, I did this module to counter a similar functionality in Eclipse. And to show how easy it was to build this module with new Javac based APIs in NB6.0.

Posted by Sandip on January 11, 2007 at 09:59 AM PST #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed








« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today

Get NetBeans 5.5

Locations of visitors to this page

Today's Page Hits: 372


XML
All
/Creator
/General
/Hobby
/Java
/JavaScript
/Mozilla
/NetBeans
/Ubuntu
/VisualWeb
/VisualWebPack
/Web 2.0

XML
All
/Creator
/General
/Hobby
/Java
/JavaScript
/Mozilla
/NetBeans
/Ubuntu
/VisualWeb
/VisualWebPack
/Web 2.0

scblog
scblog