How to Create a Hint in the NetBeans Java Editor
Join the DZone community and get the full member experience.
Join For Free
Helpful marks appear in the right side of the editor, so that I can see everywhere in my code where these method calls are found. As the user of my own hint, I can change the way it is displayed, i.e., either as a warning (as above) or as a squiggly red error mark. I didn't need to do any coding for that part of my hint. By registering it in the layer, it landed on its own two feet in the Options window for these purposes:
To create hints yourself, similar to the above, you need a reasonable understanding of the new 6.0 Java Language Infrastructure. The NetBeans Java Language Infrastructure Tutorial will get you up and running, after which you should read the Java Infrastructure Developer's Guide and the Retouche Developer FAQ.
Armed with all that knowledge, you are ready to get stuck into the hint infrastructure. To get you warmed up, here's how I created the above hint. First, register it in the layer file:
<folder name="org-netbeans-modules-java-hints">
<folder name="rules">
<folder name="hints">
<folder name="general">
<attr name="SystemFileSystem.localizingBundle" stringvalue="org.netbeans.modules.demohint.Bundle"/>
<file name="org-netbeans-modules-demohint-WrongDebugMethodologies.instance"/>
</folder>
</folder>
</folder>
</folder>
Then create the class referred to above, WrongDebugMethodologies.java, with this content:
public class WrongDebugMethodologies extends AbstractHint {
private static final List<Fix> NO_FIXES = Collections.<Fix>emptyList();
private static final Set<Tree.Kind> TREE_KINDS =
EnumSet.<Tree.Kind>of(Tree.Kind.METHOD_INVOCATION);
public WrongDebugMethodologies() {
super(true, true, AbstractHint.HintSeverity.WARNING);
}
public Set<Kind> getTreeKinds() {
return TREE_KINDS;
}
public List<ErrorDescription> run(CompilationInfo info, TreePath treePath) {
Tree t = treePath.getLeaf();
Element el = info.getTrees().getElement(treePath);
String name = el.getSimpleName().toString();
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;
}
public void cancel() {
// Does nothing
}
public String getId() {
return "Wrong_Debug"; // NOI18N
}
public String getDisplayName() {
return NbBundle.getMessage(WrongDebugMethodologies.class, "LBL_WrongDebug");
}
public String getDescription() {
return NbBundle.getMessage(WrongDebugMethodologies.class, "DSC_WrongDebug");
}
}
Finally, add the following strings to your bundle:
LBL_WrongDebug=Hey buddy, you probably want to remove that JOptionPane...
DSC_WrongDebug=JOptionPane shouldn't be used, probably.
org-netbeans-modules-java-hints/rules/hints/general=General
Required modules: Editor Hints (Experimental), File System API, Javac API Wrapper, Java Hints, Java Source, Utilities API. As you can see at least one of these is experimental, requiring the implementation version to be used.
Opinions expressed by DZone contributors are their own.
Trending
-
Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
-
Top 10 Pillars of Zero Trust Networks
-
Getting Started With the YugabyteDB Managed REST API
-
Operator Overloading in Java
Comments