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
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
  1. DZone
  2. Coding
  3. Tools
  4. JavaOne 2012: Custom Static Code Analysis [NetBeans]

JavaOne 2012: Custom Static Code Analysis [NetBeans]

Dustin Marx user avatar by
Dustin Marx
·
Oct. 03, 12 · Interview
Like (0)
Save
Tweet
Share
6.24K Views

Join the DZone community and get the full member experience.

Join For Free

I returned to the Hilton (Continental Ballroom 4) to attend Jan Lahoda's (Oracle/NetBeans) Birds-of-a-Feather presentation "Custom Static Code Analysis." I have previously blogged on a subset of the functionality covered by Lahoda in the post Creating a NetBeans 7.1 Custom Hint. In that post, I looked at writing custom hints for NetBeans using the custom declarative refactoring language that Lahoda discussed in his presentation.

Lahoda explained that the Custom Declarative Refactorings Language was founded in 2000 to "improve IDEs and the way developers develop." NetBeans 6.0 (announcement) adopted its write model in 2006 and it was revived for NetBeans 7.1 with Project Jackpot 3.0. It is an AST-based custom Java-like DSL language that "allows to define refactorings (almost) declaratively." It can be used to "check for anti-patterns" and to "determine how many clients call a particular method for a specified context."

Jackpot can be run outside of the NetBeans IDE on the command line using the Jackpot Standalone tool. Lahoda stated that this standalone command line based tool can even apply NetBeans warnings.

Lahoda had several slides covering options and syntax for specifying the static code analysis rules. The information on many of these slides is available in the JavaDeclarativeHintsFormat Wiki page that Lahoda referenced. Lahoda also referenced online Jackpot 3.0 Examples.

Lahoda provided a template of how the declarative hints language works:

   <source-pattern> :: <conditions>
=> <target-pattern> :: <conditions>
=> <target-pattern> :: <conditions>
;;

Lahoda stated that you can place "any number of such rules in a file" and then place the .hint file in META-INF/upgrade. See Geertjan Wielenga's post Oh No Vector! about this particular detail.

Patterns were another subject Lahoda covered in his presentation. He had a bullet stating that patterns can be applied to "Java expressions, statements, classes, and methods." He explained the significance of $, which in front of a pattern means substitution of one thing and with one at the end means multiple instances of that thing. Here is the rules using $ for patterns that he showed on his slide:

Statement$statement $statements$
0 or 1if ($cond) $then; else $else$
Modifiers$mods$ $type$ $variableName;
Multiple Catchestry {} $catches$ finally {}

Lahoda covered conditions. He stated that there are three types of conditions: language (instanceof, otherwise), standard (predefined), and custom. A condition result can be negated (!) and && works on condition results.

Lahoda's example of using instanceof is shown next (evaluates to true if expression bound to $variable is of type <type>):

$variable instanceof <type>

The otherwise keyword is used when designating fixes and is true when no other fix is available for a particular rule.

Lahoda stated that "like method invocations" are of the form:

hasModifier($variable, PRIVATE)

Lahoda concluded by reminding the audience of situations in which this approach to custom static code analysis might be useful:

  1. NetBeans custom refactoring to check for antipatterns
  2. Checking for violations during continuous build
  3. Producing standard NetBeans warnings

When I wrote my post Creating a NetBeans 7.1 Custom Hint, one of the biggest challenges was finding documentation outside the sparse online NetBeans documentation regarding use of the custom rules language. Lahoda's presentation and the newer Wiki pages and other documentation he has been a major contributor to in recent months have helped to reduce this hurdle. This is one of several things I've seen or heard more about at JavaOne 2012 that I hope to blog on in the future.

NetBeans

Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Silver Bullet or False Panacea? 3 Questions for Data Contracts
  • Data Mesh vs. Data Fabric: A Tale of Two New Data Paradigms
  • Apache Kafka Introduction, Installation, and Implementation Using .NET Core 6
  • Implementing Infinite Scroll in jOOQ

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
  • +1 (919) 678-0300

Let's be friends: