Re: Help required on writing a new Check

Lars Kühne <[email protected]> Thu, 23 Apr 2009 21:47:59 +0200
Newsgroups gmane.comp.java.audit.checkstyle.user
Message-ID <[email protected]>
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 &#45; New Free Runtime and 30 Day Trial
Check out the new simplified licensign option that enables unlimited
royalty&#45;free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects

_______________________________________________
Checkstyle-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user