KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 import org.hammurapi.InspectorBase;
28 import org.hammurapi.HammurapiException;
29
30 import com.pavelvlasov.config.ConfigurationException;
31 import com.pavelvlasov.config.Parameterizable;
32 import com.pavelvlasov.jsel.CompilationUnit;
33 import com.pavelvlasov.jsel.Identifier;
34 import com.pavelvlasov.jsel.JselException;
35 import com.pavelvlasov.jsel.VariableDefinition;
36 import com.pavelvlasov.review.SourceMarker;
37
38 /**
39  * ER-117
40  * Copyrights information should be present in each file.
41  * @author Janos Czako
42  * @version $Revision: 1.4 $
43  */

44 public class DoNotUseTypeRule extends InspectorBase implements Parameterizable {
45
46     /**
47      * Reviews the variable definitions if they violate agains the rule.
48      *
49      * @param element the variable definition to be reviewed.
50      */

51     public void visit(VariableDefinition element) {
52         // TODO Shall check not only for variable definition but also for
53
// parameters, superclasses, casts, and implemented interfaces
54
// so it might be better to visit(Identifier) or something like this
55
try {
56             if (disAllowedIncludes.contains(element.getTypeSpecification().getType().getName())) {
57                 context.reportViolation(element);
58             }
59         } catch (JselException e) {
60             context.warn(element, e);
61         }
62     }
63
64     /**
65      * Reviews the compilation unit if it's file violates agaianst the rule.
66      * If a file contains more than one compilation unit, the violation will be
67      * raised each time.
68      *
69      * @param element the compilation unit to be reviewed.
70      */

71     public void visit(CompilationUnit element) throws HammurapiException {
72         Iterator JavaDoc imports = element.getImports().iterator();
73         while (imports.hasNext()) {
74             Identifier importItem = (Identifier) imports.next();
75             String JavaDoc importName = importItem.getValue();
76             if (importName.endsWith(STAR)) {
77                 importName =
78                     importName.substring(0, importName.length()-SUB_STR_LEN);
79             }
80             if (disAllowedIncludes.contains(importName)) {
81                 context.reportViolation((SourceMarker) importItem);
82             } else {
83                 // If the whole package is disallowed and the import is exact.
84
int pos = importName.lastIndexOf(DOT);
85                 if (pos>0) {
86                 importName = importName.substring(0, pos);
87                 if (disAllowedIncludes.contains(importName)) {
88                     context.reportViolation((SourceMarker) importItem);
89                 }
90             }
91         }
92     }
93     }
94     
95     /**
96      * Stores the setting from the configuration of the disallowed types,
97      * packages. In case of a full package the ending "*" won't be stored.
98      */

99     private java.util.Set JavaDoc disAllowedIncludes = new java.util.HashSet JavaDoc();
100     
101     private static final String JavaDoc STAR = "*";
102     private static final String JavaDoc DOT = ".";
103     private static final int SUB_STR_LEN = 2;
104
105     
106     /**
107      * Configures the rule. Reads in the values of the parameter copyright.
108      *
109      * @param name the name of the parameter being loaded from Hammurapi configuration
110      * @param value the value of the parameter being loaded from Hammurapi configuration
111      * @exception ConfigurationException in case of a not supported parameter
112      */

113     public boolean setParameter(String JavaDoc name, Object JavaDoc parameter) throws ConfigurationException {
114         if ("include".equals(name)) {
115             String JavaDoc s = parameter.toString();
116             //cutting the ending "*" makes easier the later checks
117
if (s.endsWith(STAR)) {
118                 s = s.substring(0, s.length()-SUB_STR_LEN);
119             }
120             disAllowedIncludes.add(s);
121             return true;
122         } else {
123             throw new ConfigurationException("Parameter '"+name+"' is not supported by "+getClass().getName());
124         }
125     }
126
127     /**
128      * Gives back the preconfigured values.
129      */

130     public String JavaDoc getConfigInfo() {
131         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Disallowed types:");
132         java.util.Iterator JavaDoc iter = disAllowedIncludes.iterator();
133         while (iter.hasNext()) {
134             ret.append("\n\ttype: " + (String JavaDoc) iter.next());
135         }
136         ret.append("\n");
137
138         return ret.toString();
139     }
140 }
141
Popular Tags