KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 import javassist.Modifier;
28
29 import org.jboss.aop.annotation.AnnotationElement;
30 import org.jboss.aspects.dbc.DesignByContractAspect;
31 import org.jboss.aspects.dbc.PostCond;
32 import org.jboss.aspects.dbc.PreCond;
33
34 /**
35  *
36  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
37  * @version $Revision: 37406 $
38  */

39 public class MethodConditionManager extends ConditionManager
40 {
41    public static synchronized ExecutableCondition[] getPreConditions(Method JavaDoc method)
42    {
43       ExecutableCondition[] pre = (ExecutableCondition[])preConditions.get(method);
44       if (pre != null)
45       {
46          return pre;
47       }
48       
49       initialise(method);
50       return (ExecutableCondition[])preConditions.get(method);
51    }
52    
53    public static synchronized ExecutableCondition[] getPostConditions(Method JavaDoc method)
54    {
55       ExecutableCondition[] post = (ExecutableCondition[])postConditions.get(method);
56       if (post != null)
57       {
58          return post;
59       }
60       
61       initialise(method);
62       return (ExecutableCondition[])postConditions.get(method);
63    }
64    
65    public static synchronized InvariantCondition[] getInvariants(Method JavaDoc method)
66    {
67       return getInvariants(method.getDeclaringClass());
68    }
69
70    private static void initialise(Method JavaDoc method)
71    {
72       if (DesignByContractAspect.verbose) System.out.println("[dbc] ===== Intitalising method: " + method);
73       ArrayList JavaDoc preConds = new ArrayList JavaDoc();
74       ArrayList JavaDoc postConds = new ArrayList JavaDoc();
75       
76       
77       //Need @PreCond and @PostCond for this method, and all the super
78
//declarations of the method.
79
//Likewise we need the @Invariant for this class and the super classes
80
boolean first = true;
81       
82       Class JavaDoc clazz = method.getDeclaringClass();
83       Class JavaDoc curClazz = clazz;
84       Method JavaDoc superMethod = method;
85       
86       while (curClazz != null)
87       {
88          if (first)
89          {
90             first = false;
91          }
92          else
93          {
94             superMethod = findMethodInClass(curClazz, method);
95          }
96          
97          if (superMethod != null)
98          {
99             addMethodConditions(method, superMethod, preConds, postConds);
100          }
101          
102          addMethodConditionsForInterfaces(preConds, postConds, curClazz, method);
103
104          curClazz = curClazz.getSuperclass();
105       }
106       
107       ExecutableCondition[] pre = (ExecutableCondition[])preConds.toArray(new ExecutableCondition[preConds.size()]);
108       preConditions.put(method, pre);
109       
110       ExecutableCondition[] post = (ExecutableCondition[])postConds.toArray(new ExecutableCondition[postConds.size()]);
111       postConditions.put(method, post);
112    }
113    
114    private static void addMethodConditionsForInterfaces(ArrayList JavaDoc preConds, ArrayList JavaDoc postConds, Class JavaDoc clazz, Method JavaDoc method)
115    {
116       Class JavaDoc[] interfaces = clazz.getInterfaces();
117       for (int i = 0 ; i < interfaces.length ; i++)
118       {
119          //System.out.println("Checking interface: " + interfaces[i]);
120
Method JavaDoc foundMethod = findMethodInClass(interfaces[i], method);
121          
122          if (foundMethod != null)
123          {
124             //System.out.println("Found method: " + foundMethod);
125
addMethodConditions(method, foundMethod, preConds, postConds);
126          }
127       }
128       
129    }
130    private static void addMethodConditions(Method JavaDoc realMethod, Method JavaDoc currentMethod, ArrayList JavaDoc preConds, ArrayList JavaDoc postConds)
131    {
132       PreCond pre = (PreCond)AnnotationElement.getAnyAnnotation(currentMethod, PreCond.class);
133       if (pre != null)
134       {
135          if (DesignByContractAspect.verbose) System.out.println("[dbc] Found preconditions in method: " + currentMethod);
136          addMethodConditions(realMethod, preConds, pre.value());
137       }
138       
139       PostCond post = (PostCond)AnnotationElement.getAnyAnnotation(currentMethod, PostCond.class);
140       if (post != null)
141       {
142          if (DesignByContractAspect.verbose) System.out.println("[dbc] Found postconditions in method: " + currentMethod);
143          addMethodConditions(realMethod, postConds, post.value());
144       }
145    }
146    
147
148    private static ArrayList JavaDoc addMethodConditions(Method JavaDoc realMethod, ArrayList JavaDoc conditions, String JavaDoc[] exprs)
149    {
150       if (exprs == null)
151       {
152          return conditions;
153       }
154       
155       boolean staticCall = Modifier.isStatic(realMethod.getModifiers());
156
157       for (int i = 0 ; i < exprs.length ; i++)
158       {
159          conditions.add(new MethodCondition(realMethod, exprs[i], staticCall));
160       }
161       
162       return conditions;
163    }
164    
165    private static Method JavaDoc findMethodInClass(Class JavaDoc clazz, Method JavaDoc method)
166    {
167       String JavaDoc name = method.getName();
168       Method JavaDoc[] methods = clazz.getDeclaredMethods();
169       for (int i = 0 ; i < methods.length ; i++)
170       {
171          if (methods[i].getName().equals(name))
172          {
173             Class JavaDoc[] soughtParams = method.getParameterTypes();
174             Class JavaDoc[] foundParams = methods[i].getParameterTypes();
175             
176             if (soughtParams.length == foundParams.length)
177             {
178                for (int j = 0 ; j < soughtParams.length ; j++)
179                {
180                   if (soughtParams[j] != foundParams[j])
181                   {
182                      break;
183                   }
184                }
185                
186                return methods[i];
187             }
188          }
189       }
190       return null;
191    }
192
193
194    
195    
196 }
197
Popular Tags