| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
| <html xmlns="http://www.w3.org/1999/xhtml"> | |
| <head> | |
| <!-- -*- xhtml -*- --> | |
| <title>NetBeans Java Hint Module Tutorial for NetBeans Platform 7.1</title> | |
| <link rel="stylesheet" type="text/css" href="https://netbeans.org/netbeans.css"/> | |
| <meta name="AUDIENCE" content="NBUSER"/> | |
| <meta name="TYPE" content="ARTICLE"/> | |
| <meta name="EXPIRES" content="N"/> | |
| <meta name="developer" content="gwielenga@netbeans.org"/> | |
| <meta name="indexed" content="y"/> | |
| <meta name="description" | |
| content="A short guide to using the Java Hint API."/> | |
| <!-- Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. --> | |
| <!-- Use is subject to license terms.--> | |
| </head> | |
| <body> | |
| <h1>NetBeans Java Hint Module Tutorial</h1> | |
| <p>This tutorial demonstrates how to create a NetBeans module that provides | |
| one or more Java hints. In this particular scenario, whenever the user | |
| types "<tt>showMessageDialog</tt>", a Java hint will appear, with the reminder that | |
| using the NetBeans Debugger is more effective than using calls to <tt>JOptionPane</tt>:</p> | |
| <p><img alt="" src="../../images/tutorials/hint/hint-result.png"/></p> | |
| <p><b class="notes">Note:</b> This document is applicable to NetBeans IDE 7.1 and | |
| NetBeans Platform 7.1. If you | |
| are using an earlier version, see <a href="../70/nbm-java-hint.html">the previous version | |
| of this document</a>.</p> | |
| <p><b>Contents</b></p> | |
| <p><img src="../../images/articles/71/netbeans-stamp.png" class="stamp" width="114" height="114" alt="Content on this page applies to NetBeans IDE 7.1" title="Content on this page applies to NetBeans IDE 7.1"/></p> | |
| <ul class="toc"> | |
| <li><a href="#creatingthemoduleproject">Setting Up the Module Project</a> | |
| </li> | |
| <li><a href="#coding-module">Creating the Java Hint</a></li> | |
| <li><a href="#registering-module">Declaring and Registering the Java Hint</a> | |
| </li> | |
| <li><a href="#building">Building and Installing the Java Hint</a> | |
| <ul> | |
| <li><a href="#try-plugin">Trying Out the Java Hint</a></li> | |
| <li><a href="#share-plugin">Creating a Shareable Module Binary</a></li> | |
| </ul></li> | |
| </ul> | |
| <p><b>To follow this tutorial, you need the software and resources listed in the following | |
| table.</b></p> | |
| <table> | |
| <tbody> | |
| <tr> | |
| <th class="tblheader" scope="col">Software or Resource</th> | |
| <th class="tblheader" scope="col">Version Required</th> | |
| </tr> | |
| <tr> | |
| <td class="tbltd1"><a href="https://netbeans.org/downloads/index.html">NetBeans IDE</a></td> | |
| <td class="tbltd1">version 7.1 or above</td> | |
| </tr> | |
| <tr> | |
| <td class="tbltd1"><a href="http://java.sun.com/javase/downloads/index.jsp">Java Developer Kit (JDK)</a></td> | |
| <td class="tbltd1">version 6 or above</td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <h2 class="tutorial"><a name="creatingthemoduleproject"></a>Setting up the Module Project</h2> | |
| <p>Before you start writing the module, you have to make sure you | |
| that your project is set up correctly. The IDE provides a wizard that sets up all the basic files | |
| needed for a module.</p> | |
| <ol> | |
| <li>Choose File > New Project (Ctrl+Shift+N). Under Categories, select NetBeans Modules. | |
| Under Projects, select Module. Click Next.</li> | |
| <li>In the Name and Location panel, type <tt>HintDemo</tt> in the Project Name field. | |
| Change the Project Location to any directory on your computer. Leave the Standalone Module option | |
| and Set as Main Project checkbox selected. Click Next.</li> | |
| <li>In the Basic Module Configuration panel, type <tt>org.nb.hintdemo</tt> | |
| in Code Name Base. Click Finish.</li> | |
| </ol> | |
| <p> The IDE creates the <tt>HintDemo</tt> | |
| project. The project contains all of your sources and | |
| project metadata, such as the project's Ant build script. The project | |
| opens in the IDE. You can view its logical structure in the Projects window (Ctrl-1) and its | |
| file structure in the Files window (Ctrl-2).</p> | |
| <!-- ===================================================================================== --> | |
| <h2><a name="coding-module"></a>Creating the Java Hint</h2> | |
| <p>In this section, you provide the Java code for the Java hint.</p> | |
| <div class="indent"> | |
| <ol> | |
| <li><p>Right-click the HintDemo project node and | |
| choose Properties. In the Libraries panel, you need | |
| to set dependencies on the following modules:</p> | |
| <p></p> | |
| <ul> | |
| <li>Datasystems API</li> | |
| <li>Editor</li> | |
| <li>Editor Hints</li> | |
| <li>Editor Library 2</li> | |
| <li>File System API</li> | |
| <li>Javac API Wrapper</li> | |
| <li>Java Source</li> | |
| <li>Java Support APIs</li> | |
| <li>Nodes API</li> | |
| <li>Text API</li> | |
| <li>UI Utilities API</li> | |
| <li>Utilities API</li> | |
| </ul></li> | |
| <li> <p>You need to set an implementation | |
| dependency on the "Java Hints" module. The module | |
| is not a stable API, i.e., it is not officially | |
| supported. A new API is likely to be created in a future | |
| release, it will be annotation-based and will allow the | |
| Java hints to use patterns, similar to Jackpot patterns. | |
| However, in the meantime, the "AbstractHint" class, in the | |
| "Java Hints" module should be used. Just be aware of this | |
| upcoming change while using the class.</p> | |
| <p>To use the implementation version of the "Java Hints" API, | |
| right-click the HintDemo project node and | |
| choose Properties. In the Libraries panel, click Add. | |
| Make sure to put a checkmark in the "Show Non-API Modules" | |
| checkbox. Then find the Java Hints module and click OK, | |
| making it a dependency | |
| of the module you're working on.</p> | |
| <p>Finally, to make | |
| the new dependency an "implementation" dependency, | |
| which means that you're aware the classes you're going | |
| to use from this module are likely to change, | |
| right-click the "Java Hints" display name in the Projects | |
| window, click Edit, and select the "Implementation | |
| Version" checkbox. Then click OK.</p></li> | |
| <li>Right-click the <tt>org.nb.hintdemo</tt> package node and | |
| choose New > Java Class. | |
| Click Next.</li> | |
| <li>Type <tt>DemoHint</tt> | |
| and click Finish.</li> | |
| <li>Replace the default content of the <tt>DemoHint.java</tt> file with the following: | |
| <pre class="examplecode">import <a href="http://java.sun.com/javase/6/docs/jdk/api/javac/tree/com/sun/source/tree/Tree.html">com.sun.source.tree.Tree</a>; | |
| import <a href="http://java.sun.com/javase/6/docs/jdk/api/javac/tree/com/sun/source/tree/Tree.Kind.html">com.sun.source.tree.Tree.Kind</a>; | |
| import <a href="http://java.sun.com/javase/6/docs/jdk/api/javac/tree/com/sun/source/util/class-use/TreePath.html">com.sun.source.util.TreePath</a>; | |
| import java.util.Collections; | |
| import java.util.EnumSet; | |
| import java.util.List; | |
| import java.util.Set; | |
| import javax.lang.model.element.Element; | |
| import <a href="http://bits.netbeans.org/dev/javadoc/org-netbeans-modules-java-source/org/netbeans/api/java/source/CompilationInfo.html">org.netbeans.api.java.source.CompilationInfo</a>; | |
| import org.netbeans.modules.java.hints.spi.AbstractHint; | |
| import <a href="http://kickjava.com/src/org/netbeans/spi/editor/hints/ErrorDescription.java.htm">org.netbeans.spi.editor.hints.ErrorDescription</a>; | |
| import <a href="http://kickjava.com/src/org/netbeans/spi/editor/hints/ErrorDescriptionFactory.java.htm">org.netbeans.spi.editor.hints.ErrorDescriptionFactory</a>; | |
| import <a href="http://kickjava.com/src/org/netbeans/spi/editor/hints/Fix.java.htm">org.netbeans.spi.editor.hints.Fix</a>; | |
| public class DemoHint extends AbstractHint { | |
| <b>//This hint does not enable the IDE to fix the problem:</b> | |
| private static final List<Fix> NO_FIXES = Collections.<Fix>emptyList(); | |
| <b>//This hint applies to method invocations:</b> | |
| private static final Set<Tree.Kind> TREE_KINDS = | |
| EnumSet.<Tree.Kind>of(Tree.Kind.METHOD_INVOCATION); | |
| public DemoHint() { | |
| super(true, true, AbstractHint.HintSeverity.WARNING); | |
| } | |
| <b>//Specify the kind of code that the hint applies to, in this case, | |
| //the hint applies to method invocations:</b> | |
| @Override | |
| public Set<Kind> getTreeKinds() { | |
| return TREE_KINDS; | |
| } | |
| @Override | |
| public List<ErrorDescription> run(CompilationInfo info, TreePath treePath) { | |
| Tree t = treePath.getLeaf(); | |
| Element el = info.getTrees().getElement(treePath); | |
| String name = el.getSimpleName().toString(); | |
| <b>//This is where it all happens: if the method invocation is 'showMessageDialog', | |
| //then the hint infrastructure kicks into action:</b> | |
| if (name.equals("showMessageDialog")) { | |
| return Collections.<ErrorDescription>singletonList( | |
| ErrorDescriptionFactory.createErrorDescription( | |
| getSeverity().toEditorSeverity(), | |
| getDisplayName(), | |
| NO_FIXES, | |
| info.getFileObject(), | |
| (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), t), | |
| (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), t))); | |
| } | |
| return null; | |
| } | |
| <b>//This is called if/when the hint processing is cancelled:</b> | |
| @Override | |
| public void cancel() { | |
| } | |
| <b>//Message that the user sees in the left sidebar:</b> | |
| @Override | |
| public String getDisplayName() { | |
| return "Hey buddy, shouldn't you be using the NetBeans Debugger instead?"; | |
| } | |
| <b>//Name of the hint in the Options window:</b> | |
| @Override | |
| public String getId() { | |
| return "Demo Hint"; | |
| } | |
| <b>//Description of the hint in the Options window:</b> | |
| @Override | |
| public String getDescription() { | |
| return "This is a dummy description for the Demo hint!"; | |
| } | |
| }</pre> | |
| <p class="tips">The second "true" in the constructor | |
| in the class above is | |
| for the "showInTaskListDefault" parameter. This | |
| parameter was ignored in pre-7.1 NetBeans IDE, | |
| but has a meaning now. False means that the hint | |
| will not appear in the "Inspect and Refactor" | |
| dialog. True means that the hint will appear, | |
| though a non-java.hints AbstractHint cannot provide | |
| a transformation and can only serve as a "query".</p> | |
| <p>Right-click in the Source Editor and choose Format (Alt-Shift-F) and then save the file. | |
| </p> | |
| </li> | |
| </ol> | |
| </div> | |
| <h2><a name="registering-module"></a>Declaring and Registering the Java Hint</h2> | |
| <p>Hints are registered in the <tt>layer.xml</tt> file for | |
| the category to which they apply. Right-click the | |
| "HintDemo" project node and then choose "New | Other". In | |
| the New File dialog, go to "Module Development | XML Layer" and | |
| then click Next and Finish.</p> | |
| <p>Add the following tags to the <tt>layer.xml</tt> file, between the <tt><filesystem></tt> tags:</p> | |
| <pre class="examplecode"><folder name="org-netbeans-modules-java-hints"> | |
| <folder name="rules"> | |
| <folder name="hints"> | |
| <folder name="general"> | |
| <file name="org-nb-hintdemo-DemoHint.instance"/> | |
| </folder> | |
| </folder> | |
| </folder> | |
| </folder></pre> | |
| <!-- ======================================================================================= --> | |
| <h2><a name="building"></a>Building and Installing the Java Hint</h2> | |
| <p>Now we need to think about installation and distribution. | |
| In the first section below, we install the Java hint, | |
| next we create an NBM file and examine distribution channels.</p> | |
| <div class="indent"> | |
| <h3 class="tutorial"><a name="try-plugin"></a>Trying Out the Java Hint</h3> | |
| <p>Install and try out the Java hint, | |
| by following the steps below.</p> | |
| <ol> | |
| <li><p>In the Projects window, right-click the <tt>HintDemo</tt> | |
| project and choose Run. The module is built and installed in the target platform. The target platform opens so that you | |
| can try out your new module. The default target platform is the | |
| installation used by the current instance of the development IDE.</p></li> | |
| <li><p>Open a Java source file and call '<tt>showMessageDialog</tt>' on <tt>JOptionPane</tt>. | |
| Notice the hint that is created in the left sidebar or press Alt-Enter to invoke it.</p></li> | |
| <li><p>In the main menu choose | |
| "Refactor | Inspect and Transform". Click Browse. | |
| Now you should see that your hint has been registered, as shown below. The user can | |
| modify the severity level in the drop-down list.</p> | |
| <p><img alt="" src="../../images/tutorials/hint/71/hint-options.png"/></p></li> | |
| </ol> | |
| <h3 class="tutorial"><a name="share-plugin"></a>Creating a Shareable Module Binary</h3> | |
| <p>An NBM file is the binary version of the module that | |
| provides the Java hint. Below, using one menu item, we create | |
| the NBM file.</p> | |
| <ol> | |
| <li><p>In the Projects window, right-click | |
| the <tt>HintDemo</tt> project and choose Create NBM. | |
| The NBM file is created and you can view it in the Files window (Ctrl-2).</p></li> | |
| <li>Make the module available to others via, for example, the | |
| <a href="http://plugins.netbeans.org/PluginPortal/">Plugin Portal</a>.</li> | |
| <li>The recipient can install the module by using their IDE's Plugin Manager. They | |
| would choose Tools > Plugins | |
| from the main menu.</li> | |
| </ol> | |
| </div> | |
| <div class="feedback-box"><a href="https://netbeans.org/about/contact_form.html?to=3&subject=Feedback:%20Java%20Hint%20Module%20Tutorial">Send Us Your Feedback</a></div> | |
| <!-- ======================================================================================== --> | |
| <h2><a name="nextsteps"></a>Next Steps</h2> | |
| <p>For more information about creating and developing NetBeans Java | |
| hints, see the following resources:</p> | |
| <ul> | |
| <li>Learn about how you can let the IDE fix the problem that is identified | |
| by the hint, in <a href="http://blogs.oracle.com/geertjan/entry/fixable_hint">Fixable Hint</a> | |
| in Geertjan's blog.</li> | |
| <li>Check out the NetBeans sources from Mercurial and then look at the code in | |
| the 'java.hints' folder.</li> | |
| </ul> | |
| <!-- ======================================================================================== | |
| <h2><a name="version"></a>Versioning </h2> | |
| <table width="76%" border="1"> | |
| <tbody> | |
| <tr> | |
| <td> | |
| <div align="left"><b>Version</b></div> | |
| </td> | |
| <td> | |
| <div align="left"><b>Date</b></div> | |
| </td> | |
| <td> | |
| <div align="left"><b>Changes</b></div> | |
| </td> | |
| <td> | |
| <div align="left"><b>Open Issues</b></div> | |
| </td> | |
| </tr> | |
| <tr> | |
| <td> | |
| 1 | |
| </td> | |
| <td> | |
| 15 November 2008 | |
| </td> | |
| <td> | |
| Initial version. | |
| </td> | |
| <tr> | |
| <td> | |
| 2 | |
| </td> | |
| <td> | |
| 12 November 2011 | |
| </td> | |
| <td> | |
| Updated for 7.1. | |
| </td> | |
| <td> | |
| ... | |
| </td> | |
| </tr> | |
| </tbody> | |
| </table>--> | |
| </body> | |
| </html> |