NetBeans Java Hints: Quick & Dirty Guide
Posted
by Geertjan
on Oracle Blogs
See other posts from Oracle Blogs
or by Geertjan
Published on Fri, 16 Mar 2012 22:05:03 +0000
Indexed on
2012/03/18
18:12 UTC
Read the original article
Hit count: 369
/NetBeans IDE
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.
- Select a package in a NetBeans module project. Right click, choose New/Other.../Module Development/Java Hint:
You'll then see this: - 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".
- Class Name: the name of the class that should be generated. E.g. "Example".
- 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_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); } @Override @Messages("FIX_com.bla.Example=Fix the problem") protected String getText() { return Bundle.FIX_com_bla_Example(); } @Override 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.
- Should generate "Example.java", the hint itself:
Thanks to Jan Lahoda for the above rough guide.
© Oracle Blogs or respective owner