KickJava   Java API By Example, From Geeks To Geeks.

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


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.JselException;
29 import com.pavelvlasov.jsel.Method;
30 import com.pavelvlasov.jsel.TypeSpecification;
31 import com.pavelvlasov.jsel.VariableDefinition;
32 import com.pavelvlasov.jsel.expressions.NewObject;
33 import com.pavelvlasov.jsel.statements.CompoundStatement;
34 import com.pavelvlasov.jsel.statements.Statement;
35 import com.pavelvlasov.util.AccumulatingVisitorExceptionSink;
36 import com.pavelvlasov.util.DispatchingVisitor;
37
38 /**
39  * ER-089
40  * Avoid using constructors in the 'clone ()' method
41  * @author Janos Czako
42  * @version $Revision: 1.4 $
43  */

44 public class ConstructorsInCloneRule extends InspectorBase {
45     
46     /**
47      * The chained visitor class which searches after calling new on the
48      * visited class.
49      */

50     public static class NewSnooper {
51         
52         /**
53          * The name of the class (not full quailified), the reviewed clone()
54          * method belongs to.
55          */

56         private String JavaDoc classNameToReview;
57         
58         /**
59          * Constructor which takes the name of the visited class.
60          * @param theClassName the name of the class to be reviewed.
61          */

62         NewSnooper(String JavaDoc theClassName) {
63             classNameToReview = theClassName;
64         }
65         
66         /**
67          * The list of the new calls found.
68          */

69         java.util.List JavaDoc returns=new java.util.ArrayList JavaDoc();
70         
71         /**
72          * Reviews the calls to new, if they are referred to the class to be
73          * reviewed.
74          *
75          * @param newCall the calls to new.
76          */

77         public void visit(NewObject newCall) throws JselException {
78             TypeSpecification ts = newCall.getTypeSpecification();
79             if (classNameToReview.equals(ts.getType().getName())) {
80                 returns.add(newCall);
81             }
82         }
83     }
84     
85     /**
86      * The name of the "clone" method.
87      */

88     private static final String JavaDoc CLONE_NAME = "clone";
89     
90     /**
91      * The error text for exceptions in the chained visitor.
92      */

93     private static final String JavaDoc CHAINED_ERRS =
94         "There have been exceptions (see above)";
95     
96     /**
97      * Reviews the method definitions, if they are "clone()" and call
98      * constructor.
99      *
100      * @param element the method definition to be reviewed.
101      * @throws HammurapiException In case of any exception in the chained visitor.
102      */

103     public void visit(Method element) throws HammurapiException {
104         if (CLONE_NAME.equals(element.getName()) && element.getParameters().isEmpty()) {
105             CompoundStatement compoundStatement = element.getCompoundStatement();
106             if (compoundStatement!=null) {
107                 java.util.Iterator JavaDoc statements = compoundStatement.getStatements().iterator();
108                     
109                 while (statements.hasNext()) {
110                     Statement statement = (Statement) statements.next();
111                     if (statement instanceof VariableDefinition) {
112                         AccumulatingVisitorExceptionSink es = new AccumulatingVisitorExceptionSink();
113                         NewSnooper rs = new NewSnooper(element.getEnclosingType().getFcn());
114                         element.accept(new DispatchingVisitor(rs, es));
115             
116                         if( !rs.returns.isEmpty()) {
117                             context.reportViolation(element);
118                         }
119                         if (!es.getExceptions().isEmpty()) {
120                             es.dump();
121                             throw new HammurapiException(CHAINED_ERRS);
122                         }
123                     }
124                 }
125             }
126         }
127     }
128 }
129
Popular Tags