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 org.hammurapi.InspectorBase;
026:
027: import com.pavelvlasov.config.ConfigurationException;
028: import com.pavelvlasov.config.Parameterizable;
029: import com.pavelvlasov.jsel.Scope;
030: import com.pavelvlasov.jsel.TypeBody;
031: import com.pavelvlasov.jsel.statements.CompoundStatement;
032: import com.pavelvlasov.jsel.statements.ForStatement;
033: import com.pavelvlasov.jsel.statements.WhileStatement;
034: import com.pavelvlasov.review.SourceMarker;
035:
036: /**
037: * ER-030
038: * Logical nesting limit exceeded
039: * @author Janos Czako
040: * @version $Revision: 1.7 $
041: */
042: public class LogicalNestingRule extends InspectorBase implements
043: Parameterizable {
044:
045: /**
046: * Reviews the statements, if their nesting is too deep.
047: *
048: * @param element the statement to be reviewed.
049: */
050: public void visit(CompoundStatement element) {
051: if (!isEmpty(element)) {
052: // Collection levels=new ArrayList();
053: Scope prevScope = null;
054: int actNesting = -1;
055: for (Scope enclosingScope = element.getEnclosingScope(); !(enclosingScope == null || enclosingScope instanceof TypeBody); enclosingScope = enclosingScope
056: .getEnclosingScope()) {
057: if (!((enclosingScope instanceof ForStatement || enclosingScope instanceof WhileStatement) && prevScope instanceof CompoundStatement)) {
058: actNesting++;
059: // levels.add(enclosingScope);
060: }
061: prevScope = enclosingScope;
062: }
063:
064: if (actNesting > maxNesting.intValue()) {
065: context.reportViolation((SourceMarker) element,
066: new Object[] { maxNesting,
067: new Integer(actNesting) });
068: // Iterator it=levels.iterator();
069: // while (it.hasNext()) {
070: // LanguageElement le=(LanguageElement) it.next();
071: // System.out.println("***\t"+le.getClass()+" "+le.getLocation());
072: // }
073: }
074: }
075: }
076:
077: /**
078: * Stores the setting form the configuration for the allowed
079: * maximal logical nesting.
080: */
081: private Integer maxNesting;
082:
083: /**
084: * Configures rule. Reads in the values of the parameter max-nesting.
085: *
086: * @param name the name of the parameter being loaded from Hammurapi configuration
087: * @param value the value of the parameter being loaded from Hammurapi configuration
088: * @exception ConfigurationException in case of a not supported parameter name, or value.
089: */
090: public boolean setParameter(String name, Object parameter)
091: throws ConfigurationException {
092: if ("max-nesting".equals(name)) {
093: maxNesting = (Integer) parameter;
094: } else {
095: throw new ConfigurationException("Parameter '" + name
096: + "' is not supported by " + getClass().getName());
097: }
098: return true;
099: }
100:
101: /**
102: * Gives back the preconfigured values.
103: */
104: public String getConfigInfo() {
105: if (maxNesting != null) {
106: StringBuffer ret = new StringBuffer(
107: "Allowed maximum logical nesting:\n");
108: ret.append("max-nesting: " + maxNesting + "\n");
109: return ret.toString();
110: } else {
111: return "";
112: }
113: }
114: }
|