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