KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.beanutils.ConversionException;
22
23 import com.puppycrawl.tools.checkstyle.api.DetailAST;
24
25 /**
26  * Checks that an EntityBean implementation satisfies EntityBean
27  * requirements. Such as:
28  * <ul>
29  * <li>The class is defined as <code>public</code>.</li>
30  * <li>The class cannot be defined as <code>final</code>.</li>
31  * <li>It contains a <code>public</code> constructor with no parameters.</li>
32  * <li>It must not define the <code>finalize</code> method.</li>
33 </ul>
34  * @author Rick Giles
35  */

36 public class EntityBeanCheck
37     extends AbstractBeanCheck
38 {
39     /** the persistence policy to enforce */
40     private PersistenceOption mPersistenceOption;
41
42     /** EJB version */
43     private String JavaDoc mVersion = "2.0";
44
45     /**
46      * Creates a new <code>EntityBeanCheck</code> instance.
47      */

48     public EntityBeanCheck()
49     {
50         mPersistenceOption = PersistenceOption.MIXED;
51         setMethodChecker(new EntityBeanMethodChecker(this));
52     }
53
54     /**
55      * Set the persistence option to enforce.
56      * @param aOption string to decode option from
57      * @throws ConversionException if unable to decode
58      */

59     public void setPersistence(String JavaDoc aOption)
60         throws ConversionException
61     {
62         mPersistenceOption =
63             (PersistenceOption) mPersistenceOption.decode(aOption);
64         if (mPersistenceOption == null) {
65             throw new ConversionException("unable to parse " + aOption);
66         }
67         else if (mPersistenceOption == PersistenceOption.BEAN) {
68             setMethodChecker(new BeanManagedMethodChecker(this));
69         }
70         else if (mPersistenceOption == PersistenceOption.CONTAINER) {
71             setMethodChecker(new ContainerManagedMethodChecker(this));
72         }
73         else {
74             setMethodChecker(new EntityBeanMethodChecker(this));
75         }
76     }
77
78     /**
79      * Returns the set <code>PersistenceOption</code>.
80      * @return the set <code>PersistenceOption</code>
81      */

82     public PersistenceOption getPersistenceOption()
83     {
84         return mPersistenceOption;
85     }
86
87     /**
88      * Sets the EJB version.
89      * @param aVersion the EJB version.
90      */

91     public void setVersion(String JavaDoc aVersion)
92     {
93         mVersion = aVersion;
94     }
95
96     /**
97      * Determines the EJB version.
98      * @return the EJB version.
99      */

100     public String JavaDoc getVersion()
101     {
102         return mVersion;
103     }
104
105     /**
106      * {@inheritDoc}
107      */

108     public void visitToken(DetailAST aAST)
109     {
110         if (Utils.hasImplements(aAST, "javax.ejb.EntityBean")) {
111             checkBean(aAST, "Entity bean", true);
112             checkAbstract(aAST);
113             getMethodChecker().checkMethods(aAST);
114         }
115     }
116
117     /**
118      * Checks whether or not an entity bean is declared abstract
119      * according to it persistence management.
120      * @param aAST the AST for the entity bean class definition.
121      */

122     private void checkAbstract(DetailAST aAST)
123     {
124         // bean-managed persistence requires non-abstract class
125
if ((getPersistenceOption() == PersistenceOption.BEAN)
126             && Utils.isAbstract(aAST))
127         {
128             logName(aAST, "abstract.bean", new Object JavaDoc[] {});
129         }
130         //container-managed persistence requires abstract class
131
if ((getPersistenceOption() == PersistenceOption.CONTAINER)
132                     && !Utils.isAbstract(aAST))
133         {
134             logName(aAST, "nonabstract.bean", new Object JavaDoc[] {});
135         }
136     }
137 }
138
Popular Tags