Geertjan's Blog

  • March 16, 2012

NetBeans Java Hints: Quick & Dirty Guide

Geertjan Wielenga
Product Manager

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 "Example.java", the hint itself:
      import com.sun.source.util.TreePath;
      import org.netbeans.api.java.source.CompilationInfo;
      import org.netbeans.spi.editor.hints.ErrorDescription;
      import org.netbeans.spi.editor.hints.Fix;
      import org.netbeans.spi.java.hints.ConstraintVariableType;
      import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
      import org.netbeans.spi.java.hints.Hint;
      import org.netbeans.spi.java.hints.HintContext;
      import org.netbeans.spi.java.hints.JavaFix;
      import org.netbeans.spi.java.hints.TriggerPattern;
      import org.openide.util.NbBundle.Messages;
      @Hint(displayName = "DN_
      description = "DESC_com.bla.Example",
      category = "general") //NOI18N
      com.bla.Example=Example Hint",
      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"))
      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);
      com.bla.Example=Fix the problem")
      protected String getText() {
      return Bundle.FIX_
      protected void performRewrite(TransformationContext ctx) {
      //perform the required transformation
    • Should also generate "ExampleTest.java", a test for it. Unfortunately, the wizard infrastructure is not capable of handling changes related to test dependencies. So the ExampleTest.java 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/project.properties
       -- 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.

Join the discussion

Comments ( 1 )
  • guest Friday, March 16, 2012

    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.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.