Hey buddy, you probably want to remove that JOptionPane...

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.

Today on NetBeans Zone. Antonio Santiago tells us about Changing a Default Action's Icon in NetBeans RCP.

Comments:

Off the topic: Those .<Fix> and .<Τree.Kind> hurt my eyes. Please don't type them!

Posted by Dimitris Andreou on February 19, 2008 at 04:49 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

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.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today