KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.hammurapi.InspectorBase;
30 import org.hammurapi.HammurapiException;
31
32 import com.pavelvlasov.jsel.statements.ReturnStatement;
33 import com.pavelvlasov.jsel.statements.TryBlock;
34 import com.pavelvlasov.review.SourceMarker;
35 import com.pavelvlasov.util.AccumulatingVisitorExceptionSink;
36 import com.pavelvlasov.util.DispatchingVisitor;
37 import com.pavelvlasov.util.Visitable;
38
39 /**
40  * Avoid return in finally block.
41  * @author Pavel Vlasov
42  * @version $Revision: 1.3 $
43  */

44 public class ReturnFromFinallyRule extends InspectorBase {
45     
46   /**
47    * Inner class fro creating a visit chain. It collects the return statements
48    * into the LinkedList returns.
49    */

50     public static class ReturnSnooper {
51     /**
52      * The LinkedList containing the return statements found.
53      */

54         List JavaDoc returns=new LinkedList JavaDoc();
55         
56     /**
57      * Reviews the return statement and adds to the list.
58      * @param statement the return statement to be reviewed.
59      */

60         public void visit(ReturnStatement statement) {
61             returns.add(statement);
62         }
63     }
64     
65     
66     /**
67      * Reviews the try blokk.
68      * Searches after return statements in the finnally part.
69      *
70      * @param tryBlock the try blokk to be reviewed.
71      * @throws HammurapiException error when parsing the code in jSel.
72      */

73     public void visit(TryBlock tryBlock) throws HammurapiException {
74         if (tryBlock.getFinallyClause()!=null) {
75             AccumulatingVisitorExceptionSink es=new AccumulatingVisitorExceptionSink();
76             ReturnSnooper rs=new ReturnSnooper();
77             ((Visitable) tryBlock.getFinallyClause()).accept(new DispatchingVisitor(rs, es));
78             
79             Iterator JavaDoc it=rs.returns.iterator();
80             while (it.hasNext()) {
81                 context.reportViolation((SourceMarker) it.next());
82             }
83             
84             if (!es.getExceptions().isEmpty()) {
85                 es.dump();
86                 throw new HammurapiException("There have been exceptions (see above)");
87             }
88         }
89     }
90 }
91
Popular Tags