Unable to add custom check for non-java files to Checker module

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

Unable to add custom check for non-java files to Checker module

shrgh
Hi,

I am trying to create a custom check for non-java files.

This is my check:

package com.mycompany.checks;

import com.puppycrawl.tools.checkstyle.api.Check;

        public class EmptyLineCheck extends Check {

        private int maxLines = 0;

        public void setMaxLines(int maxLines) {
                this.maxLines = maxLines;
        }

        public int[] getDefaultTokens() {
                return new int[0];
        }

  public void beginTree(DetailAST detailAst) {
                String[] lines = getLines();
                int count = 0;
                for (int i=0; i<lines.length; i++) {
                        boolean blank = lines[i].trim().equals("");
                        if(blank){
                           count++;
                        }
                        else{
                                if(count >= maxLines){
                                    int l = i-count;
                                        log(l, "Too many empty lines: Line "+l);
                                }
                        }
                }
        }

}


and my config file:

<module name="Checker">
        <module name="com.comcast.checks.EmptyLineCheck"/>
       
       
</module>


When this check is added as a submodule of TreeWalker module in the config file, it works fine (for java files of course).

In order to apply it to non-java files, when I try to define it as a submodule of Checker module (i.e outside the Treewalker), I am getting the following exception:

Unable to create Checker: cannot initialize module com.mycompany.checks.EmptyLineCheck - com.mycompany.checks.EmptyLineCheck is not allowed as a child in Checker
com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module com.mycompany.checks.EmptyLineCheck - com.mycompany.checks.EmptyLineCheck is not allowed as a child in Checker
        at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:177)
        at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(Automatic
Bean.java:207)
        at com.puppycrawl.tools.checkstyle.Main.createChecker(Main.java:138)
        at com.puppycrawl.tools.checkstyle.Main.main(Main.java:115)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: com.mycompany.
checks.EmptyLineCheck is not allowed as a child in Checker
        at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:171)

Is it not allowed to add custom modules directly to Checker module? If not, how to add this module so that it can be used for any file?
I am a new user of checkstyle. Sorry if I am missing something basic here.
Thanks in advance

Reply | Threaded
Open this post in threaded view
|

Re: Unable to add custom check for non-java files to Checker module

Oliver Burn
Hi,

Have a look at http://checkstyle.sourceforge.net/writingchecks.html. It is a good guide on how to write checks.

Since you want to write a check that is for non-java files, you need to subclass FileSetCheck.

Regards,
Oliver

On Fri, Aug 6, 2010 at 22:39, shrgh <[hidden email]> wrote:

Hi,

I am trying to create a custom check for non-java files.

This is my check:

package com.mycompany.checks;

import com.puppycrawl.tools.checkstyle.api.Check;

       public class EmptyLineCheck extends Check {

       private int maxLines = 0;

       public void setMaxLines(int maxLines) {
               this.maxLines = maxLines;
       }

       public int[] getDefaultTokens() {
               return new int[0];
       }

       public void beginTree(DetailAST detailAst) {
               String[] lines = getLines();
               int count = 0;
               for (int i=0; i<lines.length; i++) {
                       boolean blank = lines[i].trim().equals("");
                       if(blank){
                          count++;
                       }
                       else{
                               if(count >= maxLines){
                                   int l = i-count;
                                       log(l, "Too many empty lines: Line "+l);
                               }
                       }
               }
       }

}


and my config file:

<module name="Checker">
       <module name="com.comcast.checks.EmptyLineCheck"/>


</module>


When this check is added as a submodule of TreeWalker module in the config
file, it works fine (for java files of course).

In order to apply it to non-java files, when I try to define it as a
submodule of Checker module (i.e outside the Treewalker), I am getting the
following exception:

Unable to create Checker: cannot initialize module
com.mycompany.checks.EmptyLineCheck - com.mycompany.checks.EmptyLineCheck is
not allowed as a child in Checker
com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize
module com.mycompany.checks.EmptyLineCheck -
com.mycompany.checks.EmptyLineCheck is not allowed as a child in Checker
       at
com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:177)
       at
com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(Automatic
Bean.java:207)
       at com.puppycrawl.tools.checkstyle.Main.createChecker(Main.java:138)
       at com.puppycrawl.tools.checkstyle.Main.main(Main.java:115)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException:
com.mycompany.
checks.EmptyLineCheck is not allowed as a child in Checker
       at
com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:171)

Is it not allowed to add custom modules directly to Checker module? If not,
how to add this module so that it can be used for any file?
I am a new user of checkstyle. Sorry if I am missing something basic here.
Thanks in advance


--
View this message in context: http://checkstyle.2069334.n4.nabble.com/Unable-to-add-custom-check-for-non-java-files-to-Checker-module-tp2316266p2316266.html
Sent from the CheckStyle - User mailing list archive at Nabble.com.

------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user


------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user