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.JselException;
030: import com.pavelvlasov.jsel.Operation;
031: import com.pavelvlasov.jsel.TypeIdentifier;
032:
033: /**
034: * ER-114
035: * Declare only predefined set of exceptions in throws clause (application layer specific)
036: * @author Janos Czako
037: * @version $Revision: 1.3 $
038: */
039: public class ThrowsClauseRule extends InspectorBase implements
040: Parameterizable {
041:
042: /**
043: * Reviews the operations, if they have items in their throws clause which
044: * is nor predefined.
045: *
046: * @param element the operation to be reviewed.
047: */
048: public void visit(Operation element) {
049: java.util.Iterator throwsList = element.getThrows().iterator();
050:
051: while (throwsList.hasNext()) {
052: TypeIdentifier item = (TypeIdentifier) throwsList.next();
053: try {
054: if (!allowedThrows.contains(item.getName())) {
055: context.reportViolation(element,
056: new Object[] { item });
057: }
058: } catch (JselException e) {
059: context.warn(item, e);
060: }
061: }
062: }
063:
064: /**
065: * Stores the setting form the configuration for the allowed
066: * exceptions in the trows clause of the operations.
067: */
068: private java.util.Set allowedThrows = new java.util.HashSet();
069:
070: /**
071: * Configures the rule. Reads in the values of the parameter allowed-throws.
072: *
073: * @param name the name of the parameter being loaded from Hammurapi configuration
074: * @param value the value of the parameter being loaded from Hammurapi configuration
075: * @exception ConfigurationException in case of a not supported parameter
076: */
077: public boolean setParameter(String name, Object parameter)
078: throws ConfigurationException {
079: if ("allowed-throw".equals(name)) {
080: allowedThrows.add(parameter.toString());
081: return true;
082: } else {
083: throw new ConfigurationException("Parameter '" + name
084: + "' is not supported by " + getClass().getName());
085: }
086: }
087:
088: /**
089: * Gives back the preconfigured values.
090: */
091: public String getConfigInfo() {
092: StringBuffer ret = new StringBuffer(
093: "Allowed exceptions in the thorws clauses:\n");
094: java.util.Iterator iter = allowedThrows.iterator();
095: while (iter.hasNext()) {
096: ret.append("allowed-throw: " + (String) iter.next() + "\t");
097: }
098: ret.append("\n");
099: return ret.toString();
100: }
101: }
|