KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
26
27 import org.hammurapi.InspectorBase;
28
29 import com.pavelvlasov.config.ConfigurationException;
30 import com.pavelvlasov.config.Parameterizable;
31 import com.pavelvlasov.jsel.LanguageElement;
32 import com.pavelvlasov.jsel.TypeDefinition;
33 import com.pavelvlasov.jsel.VariableDefinition;
34
35 /**
36  * Checks if the class has only static final, or final public fields. Even the
37  * permission for this two kind of fields can be configured.
38  *
39  * @author Pavel Vlasov
40  * @version $Revision: 1.7 $
41  */

42 public class NoPublicFieldsRule extends InspectorBase implements
43         Parameterizable {
44     /**
45      * Stores the setting form the configuration if static public final
46      * attributes are allowed in the code.
47      */

48     private boolean staticFinalAllowed = true;
49
50     /**
51      * Stores the setting form the configuration if public final attributes are
52      * allowed in the code.
53      */

54     private boolean finalAllowed = false;
55
56     /**
57      * Reviews the variable definitions.
58      *
59      * @param variableDefinition
60      * the variable definition being reviewed.
61      */

62     public void visit(VariableDefinition variableDefinition) {
63         LanguageElement parent = variableDefinition.getParent();
64         if (!(parent instanceof TypeDefinition)) {
65             return; // local variable
66
}
67         if (((TypeDefinition) parent).getModifiers().contains("private")) {
68             return; // private class
69
}
70         List JavaDoc modifiers = variableDefinition.getModifiers();
71         if (modifiers.contains("public")) {
72             if (modifiers.contains("final")) {
73                 if (finalAllowed) {
74                     return;
75                 } else if (modifiers.contains("static") && staticFinalAllowed) {
76                     return;
77                 } else {
78                     context.reportViolation(variableDefinition);
79                 }
80             } else {
81                 context.reportViolation(variableDefinition);
82             }
83         }
84     }
85
86     /**
87      * Configures rule. Reads in the values of the parameters
88      * static-final-allowed and final-allowed.
89      *
90      * @param name
91      * the name of the parameter being loaded from Hammurapi
92      * configuration
93      * @param value
94      * the value of the parameter being loaded from Hammurapi
95      * configuration
96      * @exception ConfigurationException
97      * in case of a not supported parameter
98      */

99     public boolean setParameter(String JavaDoc name, Object JavaDoc value)
100             throws ConfigurationException {
101         if ("static-final-allowed".equals(name)) {
102             staticFinalAllowed = "yes".equals(value);
103         } else if ("final-allowed".equals(name)) {
104             finalAllowed = "yes".equals(value);
105         } else {
106             throw new ConfigurationException("Parameter " + name
107                     + " is not supported.");
108         }
109         return true;
110     }
111
112     /**
113      * Gives back the preconfigured values.
114      */

115     public String JavaDoc getConfigInfo() {
116         StringBuffer JavaDoc ret = new StringBuffer JavaDoc("Allowed for public fields:\n");
117         ret.append("static: " + staticFinalAllowed + "\t");
118         ret.append("final: " + finalAllowed + "\t");
119         return ret.toString();
120     }
121 }
122
Popular Tags