DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • Top 10 Pillars of Zero Trust Networks
  • Getting Started With the YugabyteDB Managed REST API
  • Operator Overloading in Java

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • Top 10 Pillars of Zero Trust Networks
  • Getting Started With the YugabyteDB Managed REST API
  • Operator Overloading in Java
  1. DZone
  2. Coding
  3. Java
  4. How to Create a Hint in the NetBeans Java Editor

How to Create a Hint in the NetBeans Java Editor

Geertjan Wielenga user avatar by
Geertjan Wielenga
·
Feb. 19, 08 · News
Like (0)
Save
Tweet
Share
4.35K Views

Join the DZone community and get the full member experience.

Join For Free
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.

 

Java (programming language) NetBeans

Opinions expressed by DZone contributors are their own.

Trending

  • Unlocking the Power of AIOps: Enhancing DevOps With Intelligent Automation for Optimized IT Operations
  • Top 10 Pillars of Zero Trust Networks
  • Getting Started With the YugabyteDB Managed REST API
  • Operator Overloading in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: