Clover Coverage Report - Checkstyle
Coverage timestamp: Fri May 9 2008 10:48:13 EST
../../../../../../img/srcFileCovDistChart9.png 55% of files have more coverage
16   110   10   2.29
4   51   0.62   7
7     1.43  
1    
 
  AbstractClassNameCheck       Line # 38 85.2% 0.8518519
10.33 16 10 10 0.62
 
  (1)
 
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.naming;
20   
21    import com.puppycrawl.tools.checkstyle.api.DetailAST;
22    import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23    import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck;
24   
25    /**
26    * <p>
27    * Ensures that the names of abstract classes conforming to some
28    * regular expression.
29    * </p>
30    * <p>
31    * Rationale: Abstract classes are convenience base class
32    * implementations of interfaces, not types as such. As such
33    * they should be named to indicate this.
34    * </p>
35    *
36    * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
37    */
 
38    public final class AbstractClassNameCheck extends AbstractFormatCheck
39    {
40    /** Defualt format for abstract class names */
41    private static final String DEFAULT_FORMAT = "^Abstract.*$|^.*Factory$";
42   
43    /** Creates new instance of the check. */
 
44  1 toggle public AbstractClassNameCheck()
45    {
46  1 super(DEFAULT_FORMAT);
47    }
48   
49    /** {@inheritDoc} */
 
50  1 toggle public int[] getDefaultTokens()
51    {
52  1 return new int[]{TokenTypes.CLASS_DEF};
53    }
54   
55    /** {@inheritDoc} */
 
56  0 toggle public int[] getRequiredTokens()
57    {
58  0 return getDefaultTokens();
59    }
60   
61    /** {@inheritDoc} */
 
62  8 toggle public void visitToken(DetailAST aAST)
63    {
64  8 switch (aAST.getType()) {
65  8 case TokenTypes.CLASS_DEF:
66  8 visitClassDef(aAST);
67  8 break;
68  0 default:
69  0 throw new IllegalStateException(aAST.toString());
70    }
71    }
72   
73    /**
74    * Checks class definition.
75    * @param aAST class definition for check.
76    */
 
77  8 toggle private void visitClassDef(DetailAST aAST)
78    {
79  8 if (isAbstract(aAST)) {
80  7 final String className =
81    aAST.findFirstToken(TokenTypes.IDENT).getText();
82   
83  7 if (!isMatchingClassName(className)) {
84  4 log(aAST.getLineNo(), aAST.getColumnNo(),
85    "illegal.abstract.class.name", className, getFormat());
86    }
87    }
88    }
89   
90    /**
91    * @param aAST class definition for check.
92    * @return true if a given class declared as abstract.
93    */
 
94  8 toggle private boolean isAbstract(DetailAST aAST)
95    {
96  8 final DetailAST abstractAST = aAST.findFirstToken(TokenTypes.MODIFIERS)
97    .findFirstToken(TokenTypes.ABSTRACT);
98   
99  8 return abstractAST != null;
100    }
101   
102    /**
103    * @param aClassName class name for check.
104    * @return true if class name matches format of abstract class names.
105    */
 
106  7 toggle private boolean isMatchingClassName(String aClassName)
107    {
108  7 return getRegexp().matcher(aClassName).find();
109    }
110    }