Over a million developers have joined DZone.

How to Create a Hint in the NetBeans Java Editor

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

I have a bad tendency to use JOptionPanes and System.out for debugging, rather than the IDE's state of the art Debugger. What's almost as bad is that I tend to leave those method calls in my code. But now I've solved my problem. I created a hint in the NetBeans editor that tells me about all the instances where I'm using these 'antipatterns'.

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.

 

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}