KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > naming > AbstractClassNameCheck


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 JavaDoc DEFAULT_FORMAT = "^Abstract.*$|^.*Factory$";
42
43     /** Creates new instance of the check. */
44     public AbstractClassNameCheck()
45     {
46         super(DEFAULT_FORMAT);
47     }
48
49     /** {@inheritDoc} */
50     public int[] getDefaultTokens()
51     {
52         return new int[]{TokenTypes.CLASS_DEF};
53     }
54
55     /** {@inheritDoc} */
56     public int[] getRequiredTokens()
57     {
58         return getDefaultTokens();
59     }
60
61     /** {@inheritDoc} */
62     public void visitToken(DetailAST aAST)
63     {
64         switch (aAST.getType()) {
65         case TokenTypes.CLASS_DEF:
66             visitClassDef(aAST);
67             break;
68         default:
69             throw new IllegalStateException JavaDoc(aAST.toString());
70         }
71     }
72
73     /**
74      * Checks class definition.
75      * @param aAST class definition for check.
76      */

77     private void visitClassDef(DetailAST aAST)
78     {
79         if (isAbstract(aAST)) {
80             final String JavaDoc className =
81                 aAST.findFirstToken(TokenTypes.IDENT).getText();
82
83             if (!isMatchingClassName(className)) {
84                 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     private boolean isAbstract(DetailAST aAST)
95     {
96         final DetailAST abstractAST = aAST.findFirstToken(TokenTypes.MODIFIERS)
97             .findFirstToken(TokenTypes.ABSTRACT);
98
99         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     private boolean isMatchingClassName(String JavaDoc aClassName)
107     {
108         return getRegexp().matcher(aClassName).find();
109     }
110 }
111
Popular Tags