KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.pavelvlasov.config.ConfigurationException;
28 import com.pavelvlasov.config.Parameterizable;
29 import com.pavelvlasov.jsel.LanguageElement;
30 import com.pavelvlasov.jsel.expressions.DoubleConstant;
31 import com.pavelvlasov.jsel.expressions.FloatConstant;
32 import com.pavelvlasov.jsel.expressions.IntegerConstant;
33 import com.pavelvlasov.jsel.expressions.LongConstant;
34
35 /**
36  * ER-011
37  * Avoid hardwired numeric literals
38  * @author Pavel Vlasov
39  * @version $Revision: 1.2 $
40  */

41 public class HardcodedNumericLiteralsRule extends HardcodedLiteralsRule implements Parameterizable {
42     
43     /**
44      * Reviews the double constant literals, if they are allowed.
45      *
46      * @param element the literal to be reviewed.
47      */

48     public void visit(DoubleConstant element) {
49         if (!isAllowed(element.getValue())) {
50             analyze((LanguageElement) element);
51         }
52     }
53     
54     /**
55      * Reviews the float constant literals, if they are allowed.
56      *
57      * @param element the literal to be reviewed.
58      */

59     public void visit(FloatConstant element) {
60         if (!isAllowed(element.getValue())) {
61             analyze((LanguageElement) element);
62         }
63     }
64     
65     /**
66      * Reviews the int constant literals, if they are allowed.
67      *
68      * @param element the literal to be reviewed.
69      */

70     public void visit(IntegerConstant element) {
71         if (!isAllowed(element.getValue())) {
72             analyze((LanguageElement) element);
73         }
74     }
75     
76     /**
77      * Reviews the long constant literals, if they are allowed.
78      *
79      * @param element the literal to be reviewed.
80      */

81     public void visit(LongConstant element) {
82         if (!isAllowed(element.getValue())) {
83             analyze((LanguageElement) element);
84         }
85     }
86     
87     /**
88      * Stores the setting form the configuration for the allowed
89      * number literals. They are acceptable as double.
90      */

91     private java.util.Set JavaDoc allowedLiterals = new java.util.HashSet JavaDoc();
92
93     /**
94      * Checks if a double value is in the allowed list.
95      * It looks through the allowed list.
96      *
97      * @param value the value to be checked.
98      * @return true if the value is in the allowed list.
99      */

100     private boolean isAllowed(double value) {
101         return allowedLiterals.contains(new Double JavaDoc(value));
102     }
103
104     
105     /**
106      * Configures rule. Reads in the values of the parameters containing the
107      * allowed numeric literals. It checks them if they can be parsed as double.
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 name, or value.
112      */

113     public boolean setParameter(String JavaDoc name, Object JavaDoc parameter) throws ConfigurationException {
114         if ("allowed-literal".equals(name)) {
115             allowedLiterals.add(parameter);
116             return true;
117         } else {
118             throw new ConfigurationException("Parameter '" + name +
119                 "' is not supported by "+getClass().getName());
120         }
121     }
122     
123
124     /**
125      * Gives back the preconfigured values.
126      */

127     public String JavaDoc getConfigInfo() {
128         StringBuffer JavaDoc ret=new StringBuffer JavaDoc("Allowed numeric literals:\n");
129         Iterator JavaDoc it=allowedLiterals.iterator();
130         while (it.hasNext()) {
131             ret.append("\t");
132             ret.append(it.next());
133             ret.append("\n");
134         }
135         return ret.toString();
136     }
137 }
138
Popular Tags