KickJava   Java API By Example, From Geeks To Geeks.

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


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.Stack JavaDoc;
26
27 import org.hammurapi.InspectorBase;
28
29 import com.pavelvlasov.jsel.JselException;
30 import com.pavelvlasov.jsel.LanguageElement;
31 import com.pavelvlasov.jsel.Parameter;
32 import com.pavelvlasov.jsel.expressions.Dot;
33 import com.pavelvlasov.jsel.expressions.Expression;
34 import com.pavelvlasov.jsel.expressions.Ident;
35 import com.pavelvlasov.jsel.expressions.MethodCall;
36 import com.pavelvlasov.jsel.statements.Handler;
37 import com.pavelvlasov.review.SourceMarker;
38
39
40 /**
41  * ER-069
42  * Do not use printStackTrace(), use logger(<Message>, <exception>) instead.
43  * @author Janos Czako
44  * @version $Revision: 1.3 $
45  */

46 public class PrintStackTraceRule extends InspectorBase {
47     
48     private static final String JavaDoc PRINTSTACKTRACE_NAME = "printStackTrace(";
49     
50     /*
51      * This method can give a bad result only if a block exists inside a
52      * handler clause and this block hides the exception variable for the
53      * handler block.
54      */

55     
56     private ThreadLocal JavaDoc handlerStack=new ThreadLocal JavaDoc() {
57         protected Object JavaDoc initialValue() {
58             return new Stack JavaDoc();
59         }
60     };
61     
62     private Stack JavaDoc getHandlerStack() {
63         return (Stack JavaDoc) handlerStack.get();
64     }
65     
66     /**
67      * Reviews the try blocks, if they contain calls
68      * to the method printStackTrace
69      *
70      * @param element the try block to be reviewed.
71      */

72     public void visit(Handler element) {
73         getHandlerStack().push(element.getParameter());
74     }
75     
76     public void leave(Handler element) {
77         getHandlerStack().pop();
78     }
79     
80     public void visit(MethodCall methodCall) {
81         if (methodCall.getParameters().isEmpty()
82             || methodCall.getParameters().size() == 1) {
83             Stack JavaDoc stack = getHandlerStack();
84             if (!stack.isEmpty()) {
85                 Parameter parameter = (Parameter) stack.peek();
86                 // e.printStackTrace -> Dot
87
if (methodCall.getName() instanceof Dot) {
88                     Expression lastOperand =
89                         (Expression) methodCall.getName().getOperand(1);
90                     if ((lastOperand instanceof Ident)
91                         && PRINTSTACKTRACE_NAME.equals(
92                             ((Ident) lastOperand).getText())) {
93                         
94                         Expression firstOperand =
95                             (Expression) methodCall
96                                 .getName()
97                                 .getOperand(0);
98                         
99                         try {
100                             if ((firstOperand instanceof Ident)
101                                 && ((LanguageElement) methodCall)
102                                     .getEnclosingScope()
103                                     .getVariableNamespace()
104                                     .find(
105                                     ((Ident) lastOperand).getText())
106                                     == parameter) {
107                                 context.reportViolation((SourceMarker) methodCall);
108                             }
109                         } catch (JselException e) {
110                             context.warn(parameter, e);
111                         }
112                     }
113                 }
114             }
115         }
116     }
117
118 /*
119  * This way works also in standard situations, but is not so correct,
120  * like the above implemented one.
121  */

122  
123 /******************************************************************
124     public void visit(MethodCall method) {
125         String methodName = method.getName().toString();
126         if ( methodName.indexOf(".printStackTrace")>0) {
127             context.reportViolation(method);
128         }
129     }
130 *******************************************************************/

131     
132 }
133
Popular Tags