what is name for token type for root of AST?

48 views
Skip to first unread message

Googmeister

unread,
Apr 5, 2016, 1:37:31 PM4/5/16
to checkstyle
I would like to write a check to flag files that define between x and y nested classes. The built-in DescendantToken check seems appropriate, except that I need to know the token type of the root node of the AST. When using the checkstyle GUI, the root node is referred to as "ROOT". However, using "ROOT"  gives me a "cannot initialize module TreeWalker - given name ROOT" error when I try to refer to it in the checkstyle.xml file. The documentation at

http://checkstyle.sourceforge.net/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html

suggests that the root node is referred to as "EOF" but that does not seem to work for me either. Any help would be much appreciated! Thanks.

       <module name="DescendantToken">
           <property name="tokens" value="EOF"/>
           <property name="limitedTokens" value="CLASS_DEF"/>
           <property name="maximumNumber" value="1"/>
           <property name="maximumNumber" value="99"/>
           <property name="minimumDepth" value="2"/>
           <property name="minimumMessage" value="You must define at least 1 nested class."/>
           <property name="maximumMessage" value="You may define at most 99 nested classes."/>
       </module>

Roman Ivanov

unread,
Apr 6, 2016, 10:03:53 AM4/6/16
to checkstyle
you have a typo in config, it should be 

cat Test.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name = "Checker">
   
<module name="TreeWalker">

       
<module name="DescendantToken">
           
<property name="tokens" value="EOF"/>
           
<property name="limitedTokens" value="CLASS_DEF"/>

           
<property name="minimumNumber" value="3"/>

           
<property name="maximumNumber" value="99"/>
           
<property name="minimumDepth" value="2"/>

           
<property name="minimumMessage" value="You must define at least 3 nested class."/>

           
<property name="maximumMessage" value="You may define at most 99 nested classes."/>
       
</module>

   
</module>
</module>

but it produce no result 

/var/tmp $ cat TestClass.java
package org;
import org;
public class TestClass {
/**
* Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557
*/
public void run() {
}

}
/var/tmp $ java -jar checkstyle-6.17-SNAPSHOT-all.jar -c Test.xml TestClass.java
Starting audit...
Audit done.

Because there is no ROOT node at all (it is not EOF):

/var/tmp $ java -jar checkstyle-6.17-SNAPSHOT-all.jar -t TestClass.java 
PACKAGE_DEF -> package [1:0]
|--ANNOTATIONS -> ANNOTATIONS [1:8]
|--IDENT -> org [1:8]
`--SEMI -> ; [1:11]
IMPORT -> import [2:0]
|--IDENT -> org [2:7]
`--SEMI -> ; [2:10]
CLASS_DEF -> CLASS_DEF [3:0]
|--MODIFIERS -> MODIFIERS [3:0]
|   `--LITERAL_PUBLIC -> public [3:0]
|--LITERAL_CLASS -> class [3:7]
|--IDENT -> TestClass [3:13]
`--OBJBLOCK -> OBJBLOCK [3:23]
    |--LCURLY -> { [3:23]
    |--METHOD_DEF -> METHOD_DEF [8:4]
    |   |--MODIFIERS -> MODIFIERS [8:4]
    |   |   `--LITERAL_PUBLIC -> public [8:4]
    |   |--TYPE -> TYPE [8:11]
    |   |   `--LITERAL_VOID -> void [8:11]
    |   |--IDENT -> run [8:16]
    |   |--LPAREN -> ( [8:19]
    |   |--PARAMETERS -> PARAMETERS [8:20]
    |   |--RPAREN -> ) [8:20]
    |   `--SLIST -> { [8:22]
    |       `--RCURLY -> } [9:4]
    `--RCURLY -> } [11:0]


Unfortunately this is even clear from debug of TreeWalker, parser at
return "package" node - PACKAGE_DEF .

You can open issue on this, we need to investigate this and update misleading javadoc or introduce ROOT TokenType.

Roman Ivanov

unread,
Apr 13, 2016, 9:22:45 AM4/13/16
to checkstyle
Reply all
Reply to author
Forward
0 new messages