KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aspects > dbc > condition > InvariantCondition


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aspects.dbc.condition;
23
24 import java.util.Iterator JavaDoc;
25
26 import org.jboss.aop.joinpoint.Invocation;
27 import org.jboss.aspects.dbc.DesignByContractAspect;
28
29 import bsh.EvalError;
30 import bsh.Interpreter;
31
32 /**
33  *
34  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
35  * @version $Revision: 37406 $
36  */

37 public class InvariantCondition extends Condition
38 {
39    public InvariantCondition(Class JavaDoc clazz, String JavaDoc condExpr, boolean isStatic)
40    {
41       super(condExpr, clazz, isStatic);
42    }
43
44    public void evaluateCondition(Invocation inv, boolean staticCall, boolean constructor, Object JavaDoc ret)
45    {
46       try
47       {
48          if (DesignByContractAspect.verbose) System.out.println("[dbc] Evaluate condition : '" + originalExpr + "' for class: " + clazz);
49          
50          if (!isStatic && staticCall)
51          {
52             System.out.println("[dbc] Ignoring non-static invariant for static call");
53             return;
54          }
55    
56          Interpreter interpreter = new Interpreter();
57          Object JavaDoc target = (constructor) ? ret : getTarget(inv, staticCall);
58          
59          for (Iterator JavaDoc it = parameterLookup.keySet().iterator() ; it.hasNext() ; )
60          {
61             String JavaDoc bsname = (String JavaDoc)it.next();
62             String JavaDoc originalname = (String JavaDoc)parameterLookup.get(bsname);
63             if (originalname.equals(Condition.TARGET))
64             {
65                interpreter.set(bsname, target);
66                if (DesignByContractAspect.verbose) System.out.println("[dbc] Setting $" + originalname + " to " + target +" (type: " + target.getClass().getName() + ")");
67             }
68             else
69             {
70                System.out.println("INVARIANT CONDITION BROKEN: "+ originalExpr + " - " + clazz);
71                throw new RuntimeException JavaDoc("Invalid marker '" + originalname + "' in expression: " + originalExpr);
72             }
73          }
74          
75          Boolean JavaDoc eval = (Boolean JavaDoc)interpreter.eval(condExpr);
76    
77          if (!eval.booleanValue())
78          {
79             throw new RuntimeException JavaDoc("Invariant condition " + originalExpr + " was broken " + "for class: " + clazz);
80          }
81       }
82       catch (EvalError e)
83       {
84          throw new RuntimeException JavaDoc("There was an error in the expression: " + originalExpr, e);
85       }
86    }
87    
88    public boolean equals(Object JavaDoc o)
89    {
90       if (o instanceof InvariantCondition)
91       {
92          return super.equals(o);
93       }
94       return false;
95    }
96    
97 }
98
Popular Tags