NetBeans Java Hints: Quick & Dirty Guide

In NetBeans IDE 7.2, a new wizard will be found in the "Module Development" category in the New File dialog, for creating new Java Hints.

  1. Select a package in a NetBeans module project. Right click, choose New/Other.../Module Development/Java Hint:

    You'll then see this:

  2. Fill in:
    • Class Name: the name of the class that should be generated. E.g. "Example".

    • Hint Display Name: the display name of the hint itself (as will appear in Tools/Options). E.g. "Example Hint".

    • Warning Message: the warning that should be produced by the hint. E.g. "Something wrong is going on".

    • Hint Description: a longer description of the hint, will appear in Tools/Options and eventually some other places. E.g. "This is an example hint that warns about an example problem."

    • Will also provide an Automatic Fix: whether the hint will provide some kind of transformation. E.g. "yes".

    • Fix Display Name: the display name of such a fix/transformation. E.g. "Fix the problem".

  3. Click Finish.

    • Should generate "", the hint itself:
      import com.sun.source.util.TreePath;
      import org.netbeans.spi.editor.hints.ErrorDescription;
      import org.netbeans.spi.editor.hints.Fix;
      import org.openide.util.NbBundle.Messages;
      @Hint(displayName = "DN_com.bla.Example",
      description = "DESC_com.bla.Example",
      category = "general") //NOI18N
      @Messages({"DN_com.bla.Example=Example Hint",
          "DESC_com.bla.Example=This is an example hint that warns about an example problem."})
      public class Example {
          @TriggerPattern(value = "$str.equals(\"\")", //Specify a pattern as needed
          constraints =
          @ConstraintVariableType(variable = "$str", type = "java.lang.String"))
          @Messages("ERR_com.bla.Example=Something wrong is going on")
          public static ErrorDescription computeWarning(HintContext ctx) {
              Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath()).toEditorFix();
              return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_com.bla_Example(), fix);
          private static final class FixImpl extends JavaFix {
              public FixImpl(CompilationInfo info, TreePath tp) {
                  super(info, tp);
              @Messages("FIX_com.bla.Example=Fix the problem")
              protected String getText() {
                  return Bundle.FIX_com_bla_Example();
              protected void performRewrite(TransformationContext ctx) {
                  //perform the required transformation
    • Should also generate "", a test for it. Unfortunately, the wizard infrastructure is not capable of handling changes related to test dependencies. So the has a todo list at its begining:
      /* TODO to make this test work:
       - add test dependency on Java Hints Test API (and JUnit 4)
       - to ensure that the newest Java language features supported by the IDE are available,
          regardless of which JDK you build the module with:
       -- for Ant-based modules, add "requires.nb.javac=true" into nbproject/
       -- for Maven-based modules, use dependency:copy in validate phase to create
          target/endorsed/org-netbeans-libs-javacapi-*.jar and add to endorseddirs
          in maven-compiler-plugin configuration
      Warning: if this is a project for which tests never existed before, you may need to close&reopen the project, so that "Unit Test Libraries" node appears - a bug in apisupport projects, as far as I can tell. 

Thanks to Jan Lahoda for the above rough guide.


This is really fantastic! While it's been possible to add this sort of thing for a while, having it in a wizard should make it quite a bit easier.

Posted by guest on March 16, 2012 at 03:42 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« June 2016