Clover Coverage Report - Checkstyle
Coverage timestamp: Fri May 9 2008 10:48:13 EST
../../../../../img/srcFileCovDistChart9.png 55% of files have more coverage
53   255   31   3.79
16   147   0.58   14
14     2.21  
1    
 
  UncommentedMainCheck       Line # 44 86.7% 0.8674699
33.24 53 31 31 0.58
 
  (2)
 
1    ////////////////////////////////////////////////////////////////////////////////
2    // checkstyle: Checks Java source code for adherence to a set of rules.
3    // Copyright (C) 2001-2005 Oliver Burn
4    //
5    // This library is free software; you can redistribute it and/or
6    // modify it under the terms of the GNU Lesser General Public
7    // License as published by the Free Software Foundation; either
8    // version 2.1 of the License, or (at your option) any later version.
9    //
10    // This library is distributed in the hope that it will be useful,
11    // but WITHOUT ANY WARRANTY; without even the implied warranty of
12    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    // Lesser General Public License for more details.
14    //
15    // You should have received a copy of the GNU Lesser General Public
16    // License along with this library; if not, write to the Free Software
17    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18    ////////////////////////////////////////////////////////////////////////////////
19    package com.puppycrawl.tools.checkstyle.checks;
20   
21    import com.puppycrawl.tools.checkstyle.api.Check;
22    import com.puppycrawl.tools.checkstyle.api.DetailAST;
23    import com.puppycrawl.tools.checkstyle.api.FullIdent;
24    import com.puppycrawl.tools.checkstyle.api.TokenTypes;
25    import com.puppycrawl.tools.checkstyle.api.Utils;
26   
27    import java.util.regex.Pattern;
28    import java.util.regex.PatternSyntaxException;
29   
30    import org.apache.commons.beanutils.ConversionException;
31   
32    /**
33    * Detects uncommented main methods. Basically detects
34    * any main method, since if it is detectable
35    * that means it is uncommented.
36    *
37    * <pre class="body">
38    * &lt;module name=&quot;UncommentedMain&quot;/&gt;
39    * </pre>
40    *
41    * @author Michael Yui
42    * @author o_sukhodolsky
43    */
 
44    public class UncommentedMainCheck
45    extends Check
46    {
47    /** the pattern to exclude classes from the check */
48    private String mExcludedClasses = "^$";
49    /** compiled regexp to exclude classes from check */
50    private Pattern mExcludedClassesPattern =
51    Utils.createPattern(mExcludedClasses);
52    /** current class name */
53    private String mCurrentClass;
54    /** current package */
55    private FullIdent mPackage;
56    /** class definition depth */
57    private int mClassDepth;
58   
59    /**
60    * Set the excluded classes pattern.
61    * @param aExcludedClasses a <code>String</code> value
62    * @throws ConversionException unable to parse aExcludedClasses
63    */
 
64  1 toggle public void setExcludedClasses(String aExcludedClasses)
65    throws ConversionException
66    {
67  1 try {
68  1 mExcludedClasses = aExcludedClasses;
69  1 mExcludedClassesPattern = Utils.getPattern(mExcludedClasses);
70    }
71    catch (final PatternSyntaxException e) {
72  0 throw new ConversionException("unable to parse "
73    + mExcludedClasses,
74    e);
75    }
76    }
77   
78    /** {@inheritDoc} */
 
79  2 toggle public int[] getDefaultTokens()
80    {
81  2 return new int[] {
82    TokenTypes.METHOD_DEF,
83    TokenTypes.CLASS_DEF,
84    TokenTypes.PACKAGE_DEF,
85    };
86    }
87   
88    /** {@inheritDoc} */
 
89  0 toggle public int[] getRequiredTokens()
90    {
91  0 return getDefaultTokens();
92    }
93   
94    /** {@inheritDoc} */
 
95  2 toggle public void beginTree(DetailAST aRootAST)
96    {
97  2 mPackage = FullIdent.createFullIdent(null);
98  2 mCurrentClass = null;
99  2 mClassDepth = 0;
100    }
101   
102    /** {@inheritDoc} */
 
103  38 toggle public void leaveToken(DetailAST aAst)
104    {
105  38 if (aAst.getType() == TokenTypes.CLASS_DEF) {
106  18 if (mClassDepth == 1) {
107  18 mCurrentClass = null;
108    }
109  18 mClassDepth--;
110    }
111    }
112   
113    /** {@inheritDoc} */
 
114  38 toggle public void visitToken(DetailAST aAst)
115    {
116  38 switch (aAst.getType()) {
117  2 case TokenTypes.PACKAGE_DEF:
118  2 visitPackageDef(aAst);
119  2 break;
120  18 case TokenTypes.CLASS_DEF:
121  18 visitClassDef(aAst);
122  18 break;
123  18 case TokenTypes.METHOD_DEF:
124  18 visitMethodDef(aAst);
125  18 break;
126  0 default:
127  0 throw new IllegalStateException(aAst.toString());
128    }
129    }
130   
131    /**
132    * Sets current package.
133    * @param aPackage node for package definition
134    */
 
135  2 toggle private void visitPackageDef(DetailAST aPackage)
136    {
137  2 mPackage = FullIdent.createFullIdent(aPackage.getLastChild()
138    .getPreviousSibling());
139    }
140   
141    /**
142    * If not inner class then change current class name.
143    * @param aClass node for class definition
144    */
 
145  18 toggle private void visitClassDef(DetailAST aClass)
146    {
147    // we are not use inner classes because they can not
148    // have static methods
149  18 if (mClassDepth == 0) {
150  18