Help required on writing a new Check

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Help required on writing a new Check

Nitin Bhardwaj
Hi Friends,
 
I am a new user of Checkstyle. I am using Checkstyle 4.4 on Windows XP with JDK1.6 and Ant 1.7
I've to write a check which can identify the java classes which implements java.io.Serializable and further I've to check that whether they have defined a serialVersionUID field or not.
My approach is to write a new check class which extends com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.
Right now I just know a little bit about how to traverse and check for conditions inside a specific Java file using public void visitToken(DetailAST aAST)  method.
However, I don't know about the following scenario:
(i) How to traverse through a set of files?
(ii) How to check these files for a particular condition (like a class which implements a particular interface etc.) and select files on the basis of this condition.?
(iii) How to get a DetailAST for a file selected in (ii) step?
 
I'll really like to learn about the basic concepts of writing new checks.
Any help, probably in the form of some other similar type of check's code / documentation / or concept, is greatly appreciated.
 
Best Regards,
Nitin Bhardwaj
 

------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today.
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user
Reply | Threaded
Open this post in threaded view
|

Re: Help required on writing a new Check

Lars Kühne-3
Hi Nitin,

using your approach, you would have to duplicate the functionality of Checkstyle's TreeWalker class to transform the set of files you found to a set of DetailAST.

The trick you should use is to not implement (i) and (iii) yourself - let TreeWalker do it and implement a Check, not a FileSetCheck. Then, for each DetailAST in each file you will get the visitToken / leaveToken callbacks. The check has to register for class definitions and field definitions.

When a class is entered, you figure out whether it's serializable by using reflection (see below) and push that boolean value on a stack. When a field is entered, you figure out whether it's name is "serialVersionUID" and it has the right modifiers, if that is the case you modify the top of the stack to contain false. Then, when a classdef is left, you inspect the top value of the stack (and pop it), and if it indicates that a serializable class has been entered without finding the field, you log an error.

You can find examples for using reflection in the checkstyle code. I don't have it in front of me right now but the JavadocMethodCheck should contain sonething to check whether a given Exception is a subclass of RuntimeException.

All that said let me add that both the very cool findbugs tool (which I see as orthogonal to checkstyle) and the eclipse compiler can detect serializable classes without a serialVersionUID out of the box, and they do it very reliably because they work on the bytecode level. The approach above is only a rough sketch, and it may have to be extended quite a bit to handle corner cases like anonymous inner classes (which can be nested).

Good luck,
Lars


On Thu, Apr 23, 2009 at 8:19 AM, Nitin Bhardwaj wrote:
Hi Friends,
 
I am a new user of Checkstyle. I am using Checkstyle 4.4 on Windows XP with JDK1.6 and Ant 1.7
I've to write a check which can identify the java classes which implements java.io.Serializable and further I've to check that whether they have defined a serialVersionUID field or not.
My approach is to write a new check class which extends com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.
Right now I just know a little bit about how to traverse and check for conditions inside a specific Java file using public void visitToken(DetailAST aAST)  method.
However, I don't know about the following scenario:
(i) How to traverse through a set of files?
(ii) How to check these files for a particular condition (like a class which implements a particular interface etc.) and select files on the basis of this condition.?
(iii) How to get a DetailAST for a file selected in (ii) step?
 
I'll really like to learn about the basic concepts of writing new checks.
Any help, probably in the form of some other similar type of check's code / documentation / or concept, is greatly appreciated.
 
Best Regards,
Nitin Bhardwaj




------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensign option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user