KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.hammurapi.InspectorBase;
25 import org.hammurapi.HammurapiException;
26
27 import com.pavelvlasov.jsel.Constructor;
28 import com.pavelvlasov.jsel.TypeDefinition;
29 import com.pavelvlasov.jsel.statements.Statement;
30 import com.pavelvlasov.jsel.statements.SuperConstructorCall;
31 import com.pavelvlasov.util.AccumulatingVisitorExceptionSink;
32 import com.pavelvlasov.util.DispatchingVisitor;
33
34 /**
35  * @author Janos Czako
36  *
37  * <description>Unnecessary constructor detects when a constructor is not
38  * necessary; i.e., when there's only one constructor, it's public, has an
39  * empty body, and takes no arguments. (PMD) </description>
40  *
41  * Example:
42  * public DaoX(){
43         super();
44     }
45
46  */

47 public class UnnecessaryConstructorRule extends InspectorBase {
48
49     /**
50     * The chained visitor class which searches after constructor definitions.
51     */

52     public static class ConstructorSnooper {
53         
54         /**
55          * The list of the constructor definitions found.
56          */

57         java.util.List JavaDoc constList = new java.util.ArrayList JavaDoc();
58             
59         /**
60          * Reviews the constructor definitions and collects them.
61          *
62          * @param constructor the constructor definition
63          */

64         public void visit(Constructor constructor) {
65             constList.add(constructor);
66         }
67     }
68     
69     /**
70      * The error text for exceptions in the chained visitor.
71      */

72     private static final String JavaDoc CHAINED_ERRS =
73         "There have been exceptions (see above)";
74
75
76     /**
77      * Reviews the type definition if it violates against the rule.
78      *
79      * @param element the type definition to be reviewed.
80      * @throws HammurapiException in case of any exception in the chained visitor
81      */

82     public void visit(TypeDefinition element) throws HammurapiException {
83         AccumulatingVisitorExceptionSink es = new AccumulatingVisitorExceptionSink();
84         ConstructorSnooper rs= new ConstructorSnooper();
85         element.accept(new DispatchingVisitor(rs, es));
86
87         if (rs.constList.size()==1) {
88             Constructor constructor =
89                 (Constructor) rs.constList.get(0);
90             
91             checkConstructor(constructor);
92         }
93
94         if (!es.getExceptions().isEmpty()) {
95             es.dump();
96             throw new HammurapiException(CHAINED_ERRS);
97         }
98     }
99
100     /**
101      * "public" modifier of the constructor
102      */

103     private static final String JavaDoc PUBLIC = "public";
104     
105     /**
106      * Checks if the constructor violates against the rule.
107      */

108     private void checkConstructor(Constructor constructor) {
109         if (constructor.getModifiers().contains(PUBLIC) &&
110             constructor.getParameters().size()==0) {
111
112             if (isEmpty(constructor.getCompoundStatement())) {
113                 context.reportViolation(constructor);
114             }
115             else {
116                 java.util.Iterator JavaDoc statements =
117                     constructor.getCompoundStatement().
118                         getStatements().iterator();
119                 int cntr = 0;
120                 while (statements.hasNext()) {
121                     Statement statement = (Statement) statements.next();
122                     if (!(statement instanceof SuperConstructorCall)) {
123                         cntr++;
124                     }
125                 }
126                 if (cntr==0) {
127                     context.reportViolation(constructor);
128                 }
129             }
130         }
131     }
132 }
133
Popular Tags