KickJava   Java API By Example, From Geeks To Geeks.

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


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  * J2EE check utility methods.
26  * @author Rick Giles
27  */

28 public class Utils
29 {
30     /**
31      * Determines whether an AST node has a definition of a public method.
32      * @param aAST the node to check. Normally aAST is a CLASS_DEF.
33      * @param aName the name of the method.
34      * @return true if aAST has a definition of a public method with name
35      * aName.
36      */

37     public static boolean hasPublicMethod(DetailAST aAST, String JavaDoc aName)
38     {
39         final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK);
40         if (objBlock != null) {
41             DetailAST child = (DetailAST) objBlock.getFirstChild();
42             while (child != null) {
43                 if ((child.getType() == TokenTypes.METHOD_DEF)
44                     && Utils.isPublicMethod(child, aName))
45                 {
46                     return true;
47                 }
48                 child = (DetailAST) child.getNextSibling();
49             }
50         }
51         return false;
52     }
53
54     /**
55      * Determines whether an AST node has a definition of a public method.
56      * @param aAST the node to check. Normally aAST is a CLASS_DEF.
57      * @param aName the name of the method.
58      * @param aIsVoid designates whether the method is void.
59      * @return true if aAST has a definition of a public method with name
60      * aName and that is void according to aIsVoid.
61      */

62     public static boolean hasPublicMethod(
63         DetailAST aAST,
64         String JavaDoc aName,
65         boolean aIsVoid)
66     {
67         final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK);
68         if (objBlock != null) {
69             DetailAST child = (DetailAST) objBlock.getFirstChild();
70             while (child != null) {
71                 if ((child.getType() == TokenTypes.METHOD_DEF)
72                     && Utils.isPublicMethod(
73                         child,
74                         aName,
75                         aIsVoid))
76                 {
77                     return true;
78                 }
79                 child = (DetailAST) child.getNextSibling();
80             }
81         }
82         return false;
83     }
84
85     /**
86      * Determines whether an AST node has a definition of a public method.
87      * @param aAST the node to check. Normally aAST is a CLASS_DEF.
88      * @param aName the name of the method.
89      * @param aIsVoid designates whether the method is void.
90      * @param aParameterCount the number of method parameters.
91      * @return true if aAST has a definition of a public method with name
92      * aName and that is void according to aIsVoid.
93      */

94     public static boolean hasPublicMethod(
95         DetailAST aAST,
96         String JavaDoc aName,
97         boolean aIsVoid,
98         int aParameterCount)
99     {
100         final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK);
101         if (objBlock != null) {
102             DetailAST child = (DetailAST) objBlock.getFirstChild();
103             while (child != null) {
104                 if ((child.getType() == TokenTypes.METHOD_DEF)
105                     && Utils.isPublicMethod(
106                         child,
107                         aName,
108                         aIsVoid,
109                         aParameterCount))
110                 {
111                     return true;
112                 }
113                 child = (DetailAST) child.getNextSibling();
114             }
115         }
116         return false;
117     }
118
119    /**
120     * Determines whether an AST defines a class with a public constructor
121     * with a given number of parameters.
122     * @param aAST the AST to check.
123     * @param aParameterCount the number of parameters
124     * @return true if aAST defines a class with a public constructor
125     * with aParameterCount parameters.
126     */

127     public static boolean hasPublicConstructor(
128         DetailAST aAST,
129         int aParameterCount)
130     {
131         final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK);
132         if (objBlock == null) {
133             return false;
134         }
135         int constructorCount = 0;
136         DetailAST child = (DetailAST) objBlock.getFirstChild();
137         while (child != null) {
138             if (child.getType() == TokenTypes.CTOR_DEF) {
139                 constructorCount++;
140                 final DetailAST parameters =
141                     child.findFirstToken(TokenTypes.PARAMETERS);
142                 if (Utils.isPublic(child)
143                     && (parameters.getChildCount() == aParameterCount))
144                 {
145                     return true;
146                 }
147             }
148             child = (DetailAST) child.getNextSibling();
149         }
150         // implicit, no parameter constructor?
151
return ((constructorCount == 0) && (aParameterCount == 0));
152     }
153
154     /**
155      * Determines whether an AST node is in the definition of a
156      * class that implements javax.ejb.EntityBean.
157      * @param aAST the AST to check.
158      * @return true if aAST is in the definition of a
159      * class that implements javax.ejb.SessionBean.
160      */

161     public static boolean implementsEntityBean(DetailAST aAST)
162     {
163         final DetailAST definer = getDefiner(aAST);
164         return ((definer != null)
165             && Utils.hasImplements(definer, "javax.ejb.EntityBean"));
166     }
167
168     /**
169      * Determines whether an AST node is in the definition of a
170      * class that implements javax.ejb.SessionBean.
171      * @param aAST the AST to check.
172      * @return true if aAST is in the definition of a
173      * class that implements javax.ejb.SessionBean.
174      */

175     public static boolean implementsSessionBean(DetailAST aAST)
176     {
177         final DetailAST definer = getDefiner(aAST);
178         return ((definer != null)
179             && Utils.hasImplements(definer, "javax.ejb.SessionBean"));
180     }
181
182     /**
183      * Determines whether an AST node is in the definition of an
184      * EJB class.
185      * @param aAST the AST to check.
186      * @return true if aAST is in the definition of a
187      * an EJB class.
188      */

189     public static boolean isInEJB(DetailAST aAST)
190     {
191         final DetailAST definer = getDefiner(aAST);
192         return (
193             (definer != null)
194                 && (Utils.hasImplements(definer, "javax.ejb.SessionBean")
195                     || Utils.hasImplements(definer, "javax.ejb.EntityBean")
196                     || (Utils
197                         .hasImplements(definer, "javax.ejb.MessageDrivenBean")
198                         && Utils.hasImplements(
199                             definer,
200                             "javax.jms.MessageListener"))));
201     }
202
203     /**
204      * Finds the DetailAST for the class definition of an AST.
205      * @param aAST the AST for the search.
206      * @return the class definition AST for aAST.
207      */

208     private static DetailAST getDefiner(DetailAST aAST)
209     {
210         DetailAST definer = aAST.getParent();
211         while ((definer != null)) {
212             if (definer.getType() == TokenTypes.CLASS_DEF) {
213                 break;
214             }
215             definer = definer.getParent();
216         }
217         return definer;
218     }
219
220     /**
221      * Determines whether an AST defines an abstract element.
222      * @param aAST the AST to check.
223      * @return true if aAST defines an abstract element.
224      */

225     public static boolean isAbstract(DetailAST aAST)
226     {
227         final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS);
228         return ((mods != null)
229             && mods.branchContains(TokenTypes.ABSTRACT));
230     }
231
232     /**
233      * Determines whether an AST defines a final element.
234      * @param aAST the AST to check.
235      * @return true if aAST defines a final element.
236      */

237     public static boolean isFinal(DetailAST aAST)
238     {
239         final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS);
240         return ((mods != null)
241             && mods.branchContains(TokenTypes.FINAL));
242     }
243
244     /**
245      * Determines whether an AST defines a public element.
246      * @param aAST the AST to check.
247      * @return true if aAST defines a public element.
248      */

249     public static boolean isPublic(DetailAST aAST)
250     {
251         final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS);
252         return ((mods != null)
253             && mods.branchContains(TokenTypes.LITERAL_PUBLIC));
254     }
255
256     /**
257      * Determines whether an AST defines a static element.
258      * @param aAST the AST to check.
259      * @return true if aAST defines a static element.
260      */

261     public static boolean isStatic(DetailAST aAST)
262     {
263         final DetailAST mods = aAST.findFirstToken(TokenTypes.MODIFIERS);
264         return ((mods != null)
265             && mods.branchContains(TokenTypes.LITERAL_STATIC));
266     }
267
268     /**
269      * Determines whether an AST defines a void method.
270      * @param aAST the AST to check.
271      * @return true if aAST defines a void method.
272      */

273     public static boolean isVoid(DetailAST aAST)
274     {
275         final DetailAST type = aAST.findFirstToken(TokenTypes.TYPE);
276         return ((type != null)
277             && type.branchContains(TokenTypes.LITERAL_VOID));
278     }
279
280     /**
281      * Determines whether an AST node declares an implementation of an
282      * interface.
283      * @param aAST the AST to check.
284      * @param aInterface the interface to check.
285      * @return if the class defined by aAST implements declares an
286      * implementation of aInterface.
287      */

288     public static boolean hasImplements(DetailAST aAST, String JavaDoc aInterface)
289     {
290         final String JavaDoc shortName =
291             com.puppycrawl.tools.checkstyle.api.Utils.baseClassname(aInterface);
292         final DetailAST implementsAST =
293             aAST.findFirstToken(TokenTypes.IMPLEMENTS_CLAUSE);
294         if (implementsAST != null) {
295             DetailAST child = (DetailAST) implementsAST.getFirstChild();
296             while (child != null) {
297                 if ((child.getType() == TokenTypes.IDENT)
298                     || (child.getType() == TokenTypes.DOT))
299                 {
300                     final String JavaDoc name = Utils.constructDottedName(child);
301                     if (name.equals(aInterface)
302                         || name.equals(shortName))
303                     {
304                         return true;
305                     }
306                 }
307                 child = (DetailAST) child.getNextSibling();
308             }
309         }
310         return false;
311     }
312
313     /**
314      * Determines whether an AST node declares an extension of a class or
315      * interface.
316      * @param aAST the AST to check.
317      * @param aClassOrInterface the class or interface to check.
318      * @return if the class defined by aAST implements declares an
319      * extension of aClassOrInterface.
320      */

321     public static boolean hasExtends(DetailAST aAST, String JavaDoc aClassOrInterface)
322     {
323         final String JavaDoc shortName =
324             com.puppycrawl.tools.checkstyle.api.Utils.baseClassname(
325                 aClassOrInterface);
326         final DetailAST extendsAST =
327             aAST.findFirstToken(TokenTypes.EXTENDS_CLAUSE);
328         if (extendsAST != null) {
329             DetailAST child = (DetailAST) extendsAST.getFirstChild();
330             while (child != null) {
331                 if ((child.getType() == TokenTypes.IDENT)
332                     || (child.getType() == TokenTypes.DOT))
333                 {
334                     final String JavaDoc name = Utils.constructDottedName(child);
335                     if (name.equals(aClassOrInterface)
336                         || name.equals(shortName))
337                     {
338                         return true;
339                     }
340                 }
341                 child = (DetailAST) child.getNextSibling();
342             }
343         }
344         return false;
345     }
346
347     /**
348      * Determines whether an AST node declares a throw of an Exception.
349      * @param aAST the AST to check.
350      * @param aException the name of the Exception to check.
351      * @return if the class defined by aAST implements declares a throw
352      * of aException.
353      */

354     public static boolean hasThrows(DetailAST aAST, String JavaDoc aException)
355     {
356         final String JavaDoc shortName =
357             com.puppycrawl.tools.checkstyle.api.Utils.baseClassname(
358                 aException);
359         final DetailAST throwsAST =
360             aAST.findFirstToken(TokenTypes.LITERAL_THROWS);
361         if (throwsAST != null) {
362             DetailAST child = (DetailAST) throwsAST.getFirstChild();
363             while (child != null) {
364                 if ((child.getType() == TokenTypes.IDENT)
365                     || (child.getType() == TokenTypes.DOT))
366                 {
367                     final String JavaDoc name = Utils.constructDottedName(child);
368                     if (name.equals(aException)
369                         || name.equals(shortName))
370                     {
371                         return true;
372                     }
373                 }
374                 child = (DetailAST) child.getNextSibling();
375             }
376         }
377         return false;
378     }
379
380     /**
381      * Determines whether an AST node defines a public method.
382      * @param aAST the node to check. Normally aAST is a METHOD_DEF.
383      * @param aName the name of the method.
384      * @param aIsVoid designates whether the method is void.
385      * @param aParameterCount the number of method parameters.
386      * @return true if aAST is the definition of a public method with name
387      * aName and that is void according to aIsVoid.
388      */

389     public static boolean isPublicMethod(
390         DetailAST aAST,
391         String JavaDoc aName,
392         boolean aIsVoid,
393         int aParameterCount)
394     {
395         final DetailAST nameNode = aAST.findFirstToken(TokenTypes.IDENT);
396         if (nameNode != null) {
397             final String JavaDoc name = nameNode.getText();
398             if (name.equals(aName)
399                 && isPublic(aAST)
400                 && (isVoid(aAST) == aIsVoid))
401             {
402                 final DetailAST parameters =
403                     aAST.findFirstToken(TokenTypes.PARAMETERS);
404                 if (parameters.getChildCount() == aParameterCount) {
405                     return true;
406                 }
407             }
408         }
409         return false;
410     }
411
412     /**
413      * Determines whether an AST node defines a public method.
414      * @param aAST the node to check. Normally aAST is a METHOD_DEF.
415      * @param aName the name of the method.
416      * @param aIsVoid designates whether the method is void.
417      * @return true if aAST is the definition of a public method with name
418      * aName and that is void according to aIsVoid.
419      */

420     public static boolean isPublicMethod(
421         DetailAST aAST,
422         String JavaDoc aName,
423         boolean aIsVoid)
424     {
425         final DetailAST nameNode = aAST.findFirstToken(TokenTypes.IDENT);
426         if (nameNode != null) {
427             final String JavaDoc name = nameNode.getText();
428             if (name.equals(aName)
429                 && isPublic(aAST)
430                 && (isVoid(aAST) == aIsVoid))
431             {
432                 return true;
433             }
434         }
435         return false;
436     }
437
438     /**
439      * Determines whether an AST node defines a public method.
440      * @param aAST the node to check. Normally aAST is a METHOD_DEF.
441      * @param aName the name of the method.
442      * @return true if aAST is the definition of a public method with name
443      * aName and that is void according to aIsVoid.
444      */

445     public static boolean isPublicMethod(DetailAST aAST, String JavaDoc aName)
446     {
447         final DetailAST nameNode = aAST.findFirstToken(TokenTypes.IDENT);
448         if (nameNode != null) {
449             final String JavaDoc name = nameNode.getText();
450             if (name.equals(aName) && isPublic(aAST)) {
451                 return true;
452             }
453         }
454         return false;
455     }
456
457     /**
458      * Builds the dotted name String representation of the object contained
459      * within an AST.
460      *
461      * @param aAST the AST containing the entire hierarcy of the object
462      * @return the dotted name String representation of the object contained
463      * within aAST.
464      */

465     public static String JavaDoc constructDottedName(DetailAST aAST)
466     {
467         String JavaDoc result;
468
469         if (aAST.getType() == TokenTypes.DOT) {
470             final DetailAST left = (DetailAST) aAST.getFirstChild();
471             final DetailAST right = (DetailAST) left.getNextSibling();
472
473             result =
474                 constructDottedName(left) + "." + constructDottedName(right);
475         }
476         else {
477             result = aAST.getText();
478         }
479
480         return result;
481     }
482
483     /**
484      * Tests whether two method definition ASTs have the same parameter lists
485      * according to type.
486      * @param aMethodAST1 the first method AST to test.
487      * @param aMethodAST2 the second method AST to test.
488      * @return true if aMethodAST1 and aMethodAST2 have the same
489      * parameter lists.
490      */

491     public static boolean sameParameters(
492         DetailAST aMethodAST1,
493         DetailAST aMethodAST2)
494     {
495         final DetailAST params1 =
496             aMethodAST1.findFirstToken(TokenTypes.PARAMETERS);
497         final DetailAST params2 =
498             aMethodAST2.findFirstToken(TokenTypes.PARAMETERS);
499         if (params1.getChildCount() != params2.getChildCount()) {
500             return false;
501         }
502         DetailAST param1 = (DetailAST) params1.getFirstChild();
503         DetailAST param2 = (DetailAST) params2.getFirstChild();
504         while (param1 != null) {
505             if ((param1.getType() == TokenTypes.PARAMETER_DEF)
506                 && (param2.getType() == TokenTypes.PARAMETER_DEF))
507             {
508                 final DetailAST type1 = param1.findFirstToken(TokenTypes.TYPE);
509                 final DetailAST type2 = param2.findFirstToken(TokenTypes.TYPE);
510                 if (!equalTypes(type1, type2)) {
511                     return false;
512                 }
513             }
514             param1 = (DetailAST) param1.getNextSibling();
515             param2 = (DetailAST) param2.getNextSibling();
516         }
517         return true;
518     }
519
520     /**
521      * Tests whether two type AST nodes have the same type.
522      * @param aTypeAST1 the first type AST to test.
523      * @param aTypeAST2 the second type AST to test.
524      * @return true if aTypeAST1 and aTypeAST2 have the same type.
525      */

526     public static boolean equalTypes(
527         DetailAST aTypeAST1,
528         DetailAST aTypeAST2)
529     {
530         final DetailAST child1 = (DetailAST) aTypeAST1.getFirstChild();
531         final DetailAST child2 = (DetailAST) aTypeAST2.getFirstChild();
532         final String JavaDoc name1 = Utils.constructDottedName(child1);
533         final String JavaDoc name2 = Utils.constructDottedName(child2);
534         return name1.equals(name2);
535     }
536 }
537
Popular Tags