KickJava   Java API By Example, From Geeks To Geeks.

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


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

38 public class ConstructorConditionManager extends ConditionManager
39 {
40    public static synchronized ExecutableCondition[] getPreConditions(Constructor JavaDoc constructor)
41    {
42       ExecutableCondition[] pre = (ExecutableCondition[])preConditions.get(constructor);
43       if (pre != null)
44       {
45          return pre;
46       }
47       
48       initialise(constructor);
49       return (ExecutableCondition[])preConditions.get(constructor);
50    }
51    
52    public static synchronized ExecutableCondition[] getPostConditions(Constructor JavaDoc constructor)
53    {
54       ExecutableCondition[] post = (ExecutableCondition[])postConditions.get(constructor);
55       if (post != null)
56       {
57          return post;
58       }
59       
60       initialise(constructor);
61       return (ExecutableCondition[])postConditions.get(constructor);
62    }
63    
64    public static synchronized InvariantCondition[] getInvariants(Constructor JavaDoc constructor)
65    {
66       return getInvariants(constructor.getDeclaringClass());
67    }
68    
69    private static void initialise(Constructor JavaDoc constructor)
70    {
71       if (DesignByContractAspect.verbose) System.out.println("[dbc] ===== Intitalising constructor: " + constructor);
72       ArrayList JavaDoc preConds = new ArrayList JavaDoc();
73       ArrayList JavaDoc postConds = new ArrayList JavaDoc();
74       
75       
76       //Need @PreCond and @PostCond for this constructor, and all the super
77
//declarations of the constructor.
78
boolean first = true;
79       
80       Class JavaDoc clazz = constructor.getDeclaringClass();
81       Class JavaDoc curClazz = clazz;
82       Constructor JavaDoc superConstructor = constructor;
83       
84       while (curClazz != null)
85       {
86          if (first)
87          {
88             first = false;
89          }
90          else
91          {
92             superConstructor = findConstructorInClass(curClazz, constructor);
93          }
94          
95          if (superConstructor != null)
96          {
97             addConstructorConditions(constructor, superConstructor, preConds, postConds);
98          }
99          
100          curClazz = curClazz.getSuperclass();
101       }
102       
103       ExecutableCondition[] pre = (ExecutableCondition[])preConds.toArray(new ExecutableCondition[preConds.size()]);
104       preConditions.put(constructor, pre);
105       
106       ExecutableCondition[] post = (ExecutableCondition[])postConds.toArray(new ExecutableCondition[postConds.size()]);
107       postConditions.put(constructor, post);
108    }
109    
110    private static void addConstructorConditions(Constructor JavaDoc realConstructor, Constructor JavaDoc currentConstructor, ArrayList JavaDoc preConds, ArrayList JavaDoc postConds)
111    {
112       PreCond pre = (PreCond)AnnotationElement.getAnyAnnotation(currentConstructor, PreCond.class);
113       if (pre != null)
114       {
115          if (DesignByContractAspect.verbose) System.out.println("[dbc] Found preconditions in method: " + currentConstructor);
116          addConstructorConditions(realConstructor, preConds, pre.value());
117       }
118       
119       PostCond post = (PostCond)AnnotationElement.getAnyAnnotation(currentConstructor, PostCond.class);
120       if (post != null)
121       {
122          if (DesignByContractAspect.verbose) System.out.println("[dbc] Found postconditions in method: " + currentConstructor);
123          addConstructorConditions(realConstructor, postConds, post.value());
124       }
125    }
126    
127
128    private static ArrayList JavaDoc addConstructorConditions(Constructor JavaDoc realConstructor, ArrayList JavaDoc conditions, String JavaDoc[] exprs)
129    {
130       if (exprs == null)
131       {
132          return conditions;
133       }
134       
135       for (int i = 0 ; i < exprs.length ; i++)
136       {
137          conditions.add(new ConstructorCondition(realConstructor, exprs[i]));
138       }
139       
140       return conditions;
141    }
142    
143    private static Constructor JavaDoc findConstructorInClass(Class JavaDoc clazz, Constructor JavaDoc constructor)
144    {
145       String JavaDoc name = constructor.getName();
146       Constructor JavaDoc[] constructors = clazz.getDeclaredConstructors();
147       for (int i = 0 ; i < constructors.length ; i++)
148       {
149          if (constructors[i].getName().equals(name))
150          {
151             Class JavaDoc[] soughtParams = constructor.getParameterTypes();
152             Class JavaDoc[] foundParams = constructors[i].getParameterTypes();
153             
154             if (soughtParams.length == foundParams.length)
155             {
156                for (int j = 0 ; j < soughtParams.length ; j++)
157                {
158                   if (soughtParams[j] != foundParams[j])
159                   {
160                      break;
161                   }
162                }
163                
164                return constructors[i];
165             }
166          }
167       }
168       return null;
169    }
170
171 }
172
Popular Tags