KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import com.puppycrawl.tools.checkstyle.api.DetailAST;
26 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
27
28 /**
29  * Root class for entity bean method checks.
30  * @author Rick Giles
31  */

32 public class EntityBeanMethodChecker
33     extends BeanMethodChecker
34 {
35     /** set of ejbCreate methods for matching with ejbPostCreate methods */
36     private final Set JavaDoc mEjbCreates = new HashSet JavaDoc();
37
38     /** set of ejbPostCreate methods for matching with ejbCreate methods */
39     private final Set JavaDoc mEjbPostCreates = new HashSet JavaDoc();
40
41     /**
42      * Constructs a EntityBeanMethodChecker for a bean check.
43      * @param aCheck the bean check.
44      */

45     public EntityBeanMethodChecker(EntityBeanCheck aCheck)
46     {
47         super(aCheck);
48     }
49
50     /**
51      * {@inheritDoc}
52      */

53     public void checkMethods(DetailAST aAST)
54     {
55         mEjbCreates.clear();
56         mEjbPostCreates.clear();
57
58         super.checkMethods(aAST);
59
60         checkCreateMatch();
61     }
62
63     /**
64      * Checks that every ejbCreate method has a matching ejbPostCreate method.
65      */

66     protected void checkCreateMatch()
67     {
68         final Iterator JavaDoc it = mEjbCreates.iterator();
69         while (it.hasNext()) {
70             final DetailAST createMethod = (DetailAST) it.next();
71             final DetailAST nameAST =
72                 createMethod.findFirstToken(TokenTypes.IDENT);
73             final String JavaDoc name = nameAST.getText();
74             final String JavaDoc method = name.substring("ejbCreate".length());
75
76             // search for matching ejbPostCreate;
77
boolean match = false;
78             final Iterator JavaDoc itPostCreate = mEjbPostCreates.iterator();
79             while (!match && itPostCreate.hasNext()) {
80                 final DetailAST postCreateMethod =
81                     (DetailAST) itPostCreate.next();
82                 final DetailAST postCreateNameAST =
83                     postCreateMethod.findFirstToken(TokenTypes.IDENT);
84                 final String JavaDoc postCreateName = postCreateNameAST.getText();
85                 if (!postCreateName.equals("ejbPostCreate" + method)) {
86                     continue;
87                 }
88                 match =
89                     Utils.sameParameters(createMethod, postCreateMethod);
90             }
91             if (!match) {
92                 final String JavaDoc suffix = name.substring("ejbCreate".length());
93                 final String JavaDoc postCreateName = "ejbPostCreate" + suffix;
94                 logName(createMethod, "unmatchedejbcreate.bean",
95                     new Object JavaDoc[] {postCreateName});
96             }
97         }
98     }
99
100     /**
101      * {@inheritDoc}
102      */

103     public void checkMethod(DetailAST aMethodAST)
104     {
105         super.checkMethod(aMethodAST);
106
107         final DetailAST nameAST = aMethodAST.findFirstToken(TokenTypes.IDENT);
108         final String JavaDoc name = nameAST.getText();
109
110         if (name.startsWith("ejbHome")) {
111             checkHomeMethod(aMethodAST);
112         }
113         else if (name.startsWith("ejbPostCreate")) {
114             checkPostCreateMethod(aMethodAST);
115         }
116     }
117
118     /**
119      * {@inheritDoc}
120      */

121     protected void checkCreateMethod(DetailAST aMethodAST)
122     {
123         super.checkCreateMethod(aMethodAST);
124
125         mEjbCreates.add(aMethodAST);
126
127         // The return type must be the entity bean’s primary key type
128
if (Utils.isVoid(aMethodAST)) {
129             logName(aMethodAST, "voidmethod.bean", new Object JavaDoc[] {});
130         }
131     }
132
133
134     /**
135       * Checks whether an ejbHome<METHOD>(...) method of an
136       * entity bean satisfies requirements.
137       * @param aMethodAST the AST for the method definition.
138       */

139     protected void checkHomeMethod(DetailAST aMethodAST)
140     {
141         // the method may be final
142
checkMethod(aMethodAST, true);
143
144         // The throws clause for a home method of an entity bean must not throw
145
// the java.rmi.RemoteException.
146
checkNotThrows(aMethodAST, "java.rmi.RemoteException");
147     }
148
149     /**
150       * Checks whether an ejbPostCreate<METHOD>(...) method of an
151       * entity bean satisfies requirements.
152       * @param aMethodAST the AST for the method definition.
153       */

154     protected void checkPostCreateMethod(DetailAST aMethodAST)
155     {
156         // the method must not be final
157
checkMethod(aMethodAST, false);
158
159         mEjbPostCreates.add(aMethodAST);
160
161         // The return type must be void
162
if (!Utils.isVoid(aMethodAST)) {
163             logName(aMethodAST, "nonvoidmethod.bean", new Object JavaDoc[] {});
164         }
165     }
166 }
167
Popular Tags