KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > j2ee > AbstractBeanCheck


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.j2ee;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23
24 /**
25  * Abstract class for checks that verify Bean implementation.
26  *
27  * Checks that a Bean implementation satisfies Bean
28  * requirements:
29  * <ul>
30  * <li>The class is defined as <code>public</code>.</li>
31  * <li>The class cannot be defined as <code>abstract</code> or
32  * <code>final</code>.</li>
33  * <li>It contains a <code>public</code> constructor with no parameters.</li>
34  * <li>It must not define the <code>finalize</code> method.</li>
35 </ul>
36  * @author Rick Giles
37  */

38 public abstract class AbstractBeanCheck
39     extends AbstractJ2eeCheck
40 {
41     /**
42      * {@inheritDoc}
43      */

44     public int[] getDefaultTokens()
45     {
46         return new int[] {TokenTypes.CLASS_DEF};
47     }
48
49     /**
50      * {@inheritDoc}
51      */

52     public int[] getRequiredTokens()
53     {
54         return getDefaultTokens();
55     }
56
57 /**
58  * Checks a bean class requirements.
59  * <ul>
60  * <li>The class is defined as <code>public</code>.</li>
61   * <li>It contains a <code>public</code> constructor with no parameters.</li>
62  * <li>It must not define the <code>finalize</code> method.</li>
63  * </ul>
64  * @param aAST CLASS_DEF node for class definition to check.
65  * @param aBeanType bean type for error messages.
66  * @param aAllowAbstract if false, the class cannot be abstract.
67  */

68     protected void checkBean(
69         DetailAST aAST,
70         String JavaDoc aBeanType,
71         boolean aAllowAbstract)
72     {
73         final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT);
74         final String JavaDoc name = nameAST.getText();
75         final String JavaDoc arg = aBeanType + " '" + name + "'";
76
77         if (!Utils.isPublic(aAST)) {
78             log(nameAST.getLineNo(), nameAST.getColumnNo(),
79                 "nonpublic.bean", arg);
80         }
81         if (Utils.isFinal(aAST)) {
82             log(nameAST.getLineNo(), nameAST.getColumnNo(),
83                 "illegalmodifier.bean",
84                 new Object JavaDoc[] {arg, "final"});
85         }
86         if (!aAllowAbstract && Utils.isAbstract(aAST)) {
87             log(nameAST.getLineNo(), nameAST.getColumnNo(),
88                 "illegalmodifier.bean",
89                 new Object JavaDoc[] {arg, "abstract"});
90         }
91         if (!Utils.hasPublicConstructor(aAST, 0)) {
92             log(nameAST.getLineNo(), nameAST.getColumnNo(),
93                 "nonpublicconstructor.bean", arg);
94         }
95         if (Utils.hasPublicMethod(aAST, "finalize", true, 0)) {
96             log(
97                 nameAST.getLineNo(),
98                 nameAST.getColumnNo(),
99                 "hasfinalize.bean",
100                 arg);
101         }
102     }
103 }
104
Popular Tags