KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.hammurapi.InspectorBase;
26 import org.hammurapi.HammurapiException;
27
28 import com.pavelvlasov.jsel.Method;
29 import com.pavelvlasov.jsel.expressions.MethodCall;
30 import com.pavelvlasov.util.AccumulatingVisitorExceptionSink;
31 import com.pavelvlasov.util.DispatchingVisitor;
32
33 /**
34  * ER-090
35  * Call 'super.clone ()' in all 'clone ()' methods
36  * @author Janos Czako
37  * @version $Revision: 1.4 $
38  */

39 public class SuperCloneRule extends InspectorBase {
40     
41
42     /**
43      * The chained visitor class which searches after calling super.clone().
44      */

45     public static class MethodCallSnooper {
46                 
47         /**
48          * The list of the calls found.
49          */

50         java.util.List JavaDoc returns=new java.util.ArrayList JavaDoc();
51     
52         /**
53          * The name of the "clone" method.
54          */

55         private static final String JavaDoc SUPER_CLONE_CALL = "super.clone";
56         
57         /**
58          * Reviews the calls to super.clone()
59          *
60          * @param call the calls to be reviewed.
61          */

62         public void visit(MethodCall call) {
63             String JavaDoc callTxt = call.getName().toString();
64             if (callTxt.indexOf(SUPER_CLONE_CALL)==0 &&
65                 call.getParameters().size()==0) {
66
67                 returns.add(call);
68             }
69         }
70     }
71     
72     /**
73      * The name of the "clone" method.
74      */

75     private static final String JavaDoc CLONE_NAME = "clone";
76     
77     /**
78      * The error text for exceptions in the chained visitor.
79      */

80     private static final String JavaDoc CHAINED_ERRS =
81         "There have been exceptions (see above)";
82     
83     /**
84      * Reviews the method definitions, if they are "clone()" and call
85      * "super.clone()".
86      *
87      * @param element the method definition to be reviewed.
88      * @throws HammurapiException In case of any exception in the chained visitor.
89      */

90     public void visit(Method element) throws HammurapiException {
91         if (element.getName().compareTo(CLONE_NAME)==0 &&
92             element.getParameters().size()==0) {
93
94             AccumulatingVisitorExceptionSink es = new AccumulatingVisitorExceptionSink();
95             MethodCallSnooper rs = new MethodCallSnooper();
96             element.accept(new DispatchingVisitor(rs, es));
97     
98             if(rs.returns.size()!=1) {
99                 context.reportViolation(element);
100             }
101             if (!es.getExceptions().isEmpty()) {
102                 es.dump();
103                 throw new HammurapiException(CHAINED_ERRS);
104             }
105         }
106     }
107     
108 }
109
Popular Tags