001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi.inspectors;
024:
025: import java.util.List;
026:
027: import org.hammurapi.InspectorBase;
028:
029: import com.pavelvlasov.config.ConfigurationException;
030: import com.pavelvlasov.config.Parameterizable;
031: import com.pavelvlasov.jsel.LanguageElement;
032: import com.pavelvlasov.jsel.TypeDefinition;
033: import com.pavelvlasov.jsel.VariableDefinition;
034:
035: /**
036: * Checks if the class has only static final, or final public fields. Even the
037: * permission for this two kind of fields can be configured.
038: *
039: * @author Pavel Vlasov
040: * @version $Revision: 1.7 $
041: */
042: public class NoPublicFieldsRule extends InspectorBase implements
043: Parameterizable {
044: /**
045: * Stores the setting form the configuration if static public final
046: * attributes are allowed in the code.
047: */
048: private boolean staticFinalAllowed = true;
049:
050: /**
051: * Stores the setting form the configuration if public final attributes are
052: * allowed in the code.
053: */
054: private boolean finalAllowed = false;
055:
056: /**
057: * Reviews the variable definitions.
058: *
059: * @param variableDefinition
060: * the variable definition being reviewed.
061: */
062: public void visit(VariableDefinition variableDefinition) {
063: LanguageElement parent = variableDefinition.getParent();
064: if (!(parent instanceof TypeDefinition)) {
065: return; // local variable
066: }
067: if (((TypeDefinition) parent).getModifiers()
068: .contains("private")) {
069: return; // private class
070: }
071: List modifiers = variableDefinition.getModifiers();
072: if (modifiers.contains("public")) {
073: if (modifiers.contains("final")) {
074: if (finalAllowed) {
075: return;
076: } else if (modifiers.contains("static")
077: && staticFinalAllowed) {
078: return;
079: } else {
080: context.reportViolation(variableDefinition);
081: }
082: } else {
083: context.reportViolation(variableDefinition);
084: }
085: }
086: }
087:
088: /**
089: * Configures rule. Reads in the values of the parameters
090: * static-final-allowed and final-allowed.
091: *
092: * @param name
093: * the name of the parameter being loaded from Hammurapi
094: * configuration
095: * @param value
096: * the value of the parameter being loaded from Hammurapi
097: * configuration
098: * @exception ConfigurationException
099: * in case of a not supported parameter
100: */
101: public boolean setParameter(String name, Object value)
102: throws ConfigurationException {
103: if ("static-final-allowed".equals(name)) {
104: staticFinalAllowed = "yes".equals(value);
105: } else if ("final-allowed".equals(name)) {
106: finalAllowed = "yes".equals(value);
107: } else {
108: throw new ConfigurationException("Parameter " + name
109: + " is not supported.");
110: }
111: return true;
112: }
113:
114: /**
115: * Gives back the preconfigured values.
116: */
117: public String getConfigInfo() {
118: StringBuffer ret = new StringBuffer(
119: "Allowed for public fields:\n");
120: ret.append("static: " + staticFinalAllowed + "\t");
121: ret.append("final: " + finalAllowed + "\t");
122: return ret.toString();
123: }
124: }
|