KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > ManageThreadsFromEjbRule


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi.inspectors;
24
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.hammurapi.InspectorBase;
29
30 import com.pavelvlasov.jsel.JselException;
31 import com.pavelvlasov.jsel.LanguageElement;
32 import com.pavelvlasov.jsel.OperationInfo;
33 import com.pavelvlasov.jsel.expressions.MethodCall;
34 import com.pavelvlasov.jsel.expressions.NewObject;
35 import com.pavelvlasov.review.SourceMarker;
36 import com.pavelvlasov.util.Visitor;
37
38
39 /**
40  * ER-085
41  * Avoid starting, stopping, or managing threads in any way
42  * @author Janos Czako
43  * @version $Revision: 1.3 $
44  */

45 public class ManageThreadsFromEjbRule extends InspectorBase {
46
47     /**
48      * method which is violations
49      */

50     private Set JavaDoc allowedMethods=new HashSet JavaDoc();
51     
52     /**
53      * Type which's method must not be used.
54      */

55     private static final String JavaDoc VIOLATION_CLASS = "java.lang.Thread";
56
57     /**
58      * Reviews the type definition if it is an Enterprise Bean and in that case
59      * if it violates against the rule.
60      *
61      * @param clazz the class object of the type definition
62      */

63     public void visit(final com.pavelvlasov.jsel.Class clazz) {
64         try {
65             if (clazz.isKindOf("javax.ejb.EnterpriseBean")) {
66                 clazz.accept(new Visitor() {
67                     public boolean visit(Object JavaDoc target) {
68                         if (target instanceof MethodCall) {
69                             try {
70                                 OperationInfo provider=((MethodCall) target).getProvider();
71                                 
72                                 if (provider==null) {
73                                     context.warn((SourceMarker) target, "Provider is null for "+target+" at "+((LanguageElement) target).getLocation());
74                                 } else {
75                                     if (provider.getDeclaringType().isKindOf(VIOLATION_CLASS) && !allowedMethods.contains(provider.getSignature())) {
76                                         context.reportViolation((SourceMarker) target);
77                                     }
78                                 }
79                             } catch (JselException e) {
80                                 context.warn((SourceMarker) target, e);
81                             }
82                         } else if (target instanceof NewObject) {
83                             NewObject newObject=(NewObject) target;
84                             try {
85                                 if (newObject.getTypeSpecification().getType().isKindOf(VIOLATION_CLASS)) {
86                                     context.reportViolation((SourceMarker) target);
87                                 }
88                             } catch (JselException e) {
89                                 context.warn((SourceMarker) target, e);
90                             }
91                         }
92                         return true;
93                     }
94                 });
95             }
96         } catch (JselException e) {
97             context.warn(clazz, e);
98         }
99     }
100
101     // TODO Make this inspector parameterizable and accept allowed thread methods, e.g. getName()
102
}
103
Popular Tags