KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
25
26 import org.jboss.aop.joinpoint.Invocation;
27 import org.jboss.aspects.dbc.DesignByContractAspect;
28 import org.jboss.aspects.dbc.condition.parser.BeanshellGenerator;
29 import org.jboss.aspects.dbc.condition.parser.Expression;
30 import org.jboss.aspects.dbc.condition.parser.ExpressionParser;
31
32 import bsh.Capabilities;
33
34 /**
35  * Stores a
36  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
37  * @version $Revision: 37406 $
38  */

39 public abstract class Condition
40 {
41    public final static String JavaDoc TARGET = "tgt";
42    public final static String JavaDoc RETURN = "rtn";
43    
44    static
45    {
46       //Make sure that the beanshell can access private fields and variables
47
Capabilities.setAccessibility(true);
48    }
49
50    //Map of valid java identifiers to the original $tgt, $ret, $0, $1, $2 etc.
51
//used for the condition strings in the annotations
52
protected HashMap JavaDoc parameterLookup = new HashMap JavaDoc();
53    protected String JavaDoc originalExpr;
54    protected String JavaDoc condExpr;
55    protected Class JavaDoc clazz;
56    
57    //If this condition is for a static method or is a static invariant condition
58
protected boolean isStatic;
59    
60    public Condition(String JavaDoc condExpr, Class JavaDoc clazz, boolean isStatic)
61    {
62       this.isStatic = isStatic;
63       
64       if (DesignByContractAspect.verbose) System.out.println("[dbc] Initialising condition: " + condExpr);
65       originalExpr = condExpr;
66       condExpr += " ";
67
68       StringBuffer JavaDoc newcond = new StringBuffer JavaDoc();
69       StringBuffer JavaDoc param = null;
70
71       for (int i = 0 ; i < condExpr.length() ; i++)
72       {
73          char c = condExpr.charAt(i);
74          if (c == '$')
75          {
76             param = new StringBuffer JavaDoc();
77             continue;
78          }
79          else if (param != null && (c == '.' || c == ' ' || c == '=' || c == '>' || c == '<'
80             || c == ')' || c == '}' || c == ';' || c == '[' || c == ']'))
81          {
82             //Get param name as it was (without the leading '$')
83
String JavaDoc prm = param.toString();
84             
85             if (prm.equals(TARGET) && isStatic)
86             {
87                //For a static method or static condition, use the class name instead of a parameter
88
newcond.append(clazz.getName());
89             }
90             else
91             {
92                //Get param name for beanshell
93
String JavaDoc bsparam = "p" + i;
94                
95                newcond.append(bsparam);
96                parameterLookup.put(bsparam, prm);
97             }
98                
99             param = null;
100          }
101
102          if (param == null)
103          {
104             newcond.append(c);
105          }
106          else
107          {
108             param.append(c);
109          }
110       }
111
112       Expression expr = ExpressionParser.parseExpression(newcond.toString());
113       BeanshellGenerator gen = new BeanshellGenerator(expr);
114       this.condExpr = gen.createBeanshellCode();
115       
116       if (DesignByContractAspect.verbose) System.out.println("[dbc] Expanded to Java code:\n" + this.condExpr);
117       
118       this.clazz = clazz;
119    }
120
121    public String JavaDoc toString()
122    {
123       return originalExpr;
124    }
125    
126    public boolean equals(Object JavaDoc o){
127       if (o instanceof Condition)
128       {
129          Condition c = (Condition)o;
130          return c.clazz.equals(clazz) && originalExpr.equals(c.originalExpr);
131       }
132       
133       return false;
134    }
135    
136    public int hashCode()
137    {
138       // TODO Auto-generated method stub
139
return originalExpr.hashCode();
140    }
141    
142    protected Object JavaDoc getTarget(Invocation invocation, boolean isStatic)
143    {
144       if (!isStatic)
145       {
146          return invocation.getTargetObject();
147       }
148       else
149       {
150          return clazz;
151       }
152    }
153 }
154
Popular Tags