001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.aspects.dbc.condition;
023:
024: import java.util.Iterator;
025:
026: import org.jboss.aop.joinpoint.Invocation;
027: import org.jboss.aspects.dbc.DesignByContractAspect;
028:
029: import bsh.EvalError;
030: import bsh.Interpreter;
031:
032: /**
033: *
034: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
035: * @version $Revision: 57186 $
036: */
037: public class InvariantCondition extends Condition {
038: public InvariantCondition(Class clazz, String condExpr,
039: boolean isStatic) {
040: super (condExpr, clazz, isStatic);
041: }
042:
043: public void evaluateCondition(Invocation inv, boolean staticCall,
044: boolean constructor, Object ret) {
045: try {
046: if (DesignByContractAspect.verbose)
047: System.out.println("[dbc] Evaluate condition : '"
048: + originalExpr + "' for class: " + clazz);
049:
050: if (!isStatic && staticCall) {
051: System.out
052: .println("[dbc] Ignoring non-static invariant for static call");
053: return;
054: }
055:
056: Interpreter interpreter = new Interpreter();
057: Object target = (constructor) ? ret : getTarget(inv,
058: staticCall);
059:
060: for (Iterator it = parameterLookup.keySet().iterator(); it
061: .hasNext();) {
062: String bsname = (String) it.next();
063: String originalname = (String) parameterLookup
064: .get(bsname);
065: if (originalname.equals(Condition.TARGET)) {
066: interpreter.set(bsname, target);
067: if (DesignByContractAspect.verbose)
068: System.out.println("[dbc] Setting $"
069: + originalname + " to " + target
070: + " (type: "
071: + target.getClass().getName() + ")");
072: } else {
073: System.out.println("INVARIANT CONDITION BROKEN: "
074: + originalExpr + " - " + clazz);
075: throw new RuntimeException("Invalid marker '"
076: + originalname + "' in expression: "
077: + originalExpr);
078: }
079: }
080:
081: Boolean eval = (Boolean) interpreter.eval(condExpr);
082:
083: if (!eval.booleanValue()) {
084: throw new RuntimeException("Invariant condition "
085: + originalExpr + " was broken " + "for class: "
086: + clazz);
087: }
088: } catch (EvalError e) {
089: throw new RuntimeException(
090: "There was an error in the expression: "
091: + originalExpr, e);
092: }
093: }
094:
095: public boolean equals(Object o) {
096: if (o instanceof InvariantCondition) {
097: return super .equals(o);
098: }
099: return false;
100: }
101:
102: }
|