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.lang.reflect.Constructor;
025: import java.util.ArrayList;
026:
027: import org.jboss.aop.annotation.AnnotationElement;
028: import org.jboss.aspects.dbc.DesignByContractAspect;
029: import org.jboss.aspects.dbc.PostCond;
030: import org.jboss.aspects.dbc.PreCond;
031:
032: /**
033: *
034: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
035: * @version $Revision: 57186 $
036: */
037: public class ConstructorConditionManager extends ConditionManager {
038: public static synchronized ExecutableCondition[] getPreConditions(
039: Constructor constructor) {
040: ExecutableCondition[] pre = (ExecutableCondition[]) preConditions
041: .get(constructor);
042: if (pre != null) {
043: return pre;
044: }
045:
046: initialise(constructor);
047: return (ExecutableCondition[]) preConditions.get(constructor);
048: }
049:
050: public static synchronized ExecutableCondition[] getPostConditions(
051: Constructor constructor) {
052: ExecutableCondition[] post = (ExecutableCondition[]) postConditions
053: .get(constructor);
054: if (post != null) {
055: return post;
056: }
057:
058: initialise(constructor);
059: return (ExecutableCondition[]) postConditions.get(constructor);
060: }
061:
062: public static synchronized InvariantCondition[] getInvariants(
063: Constructor constructor) {
064: return getInvariants(constructor.getDeclaringClass());
065: }
066:
067: private static void initialise(Constructor constructor) {
068: if (DesignByContractAspect.verbose)
069: System.out.println("[dbc] ===== Intitalising constructor: "
070: + constructor);
071: ArrayList preConds = new ArrayList();
072: ArrayList postConds = new ArrayList();
073:
074: //Need @PreCond and @PostCond for this constructor, and all the super
075: //declarations of the constructor.
076: boolean first = true;
077:
078: Class clazz = constructor.getDeclaringClass();
079: Class curClazz = clazz;
080: Constructor super Constructor = constructor;
081:
082: while (curClazz != null) {
083: if (first) {
084: first = false;
085: } else {
086: super Constructor = findConstructorInClass(curClazz,
087: constructor);
088: }
089:
090: if (super Constructor != null) {
091: addConstructorConditions(constructor, super Constructor,
092: preConds, postConds);
093: }
094:
095: curClazz = curClazz.getSuperclass();
096: }
097:
098: ExecutableCondition[] pre = (ExecutableCondition[]) preConds
099: .toArray(new ExecutableCondition[preConds.size()]);
100: preConditions.put(constructor, pre);
101:
102: ExecutableCondition[] post = (ExecutableCondition[]) postConds
103: .toArray(new ExecutableCondition[postConds.size()]);
104: postConditions.put(constructor, post);
105: }
106:
107: private static void addConstructorConditions(
108: Constructor realConstructor,
109: Constructor currentConstructor, ArrayList preConds,
110: ArrayList postConds) {
111: PreCond pre = (PreCond) AnnotationElement.getAnyAnnotation(
112: currentConstructor, PreCond.class);
113: if (pre != null) {
114: if (DesignByContractAspect.verbose)
115: System.out
116: .println("[dbc] Found preconditions in method: "
117: + currentConstructor);
118: addConstructorConditions(realConstructor, preConds, pre
119: .value());
120: }
121:
122: PostCond post = (PostCond) AnnotationElement.getAnyAnnotation(
123: currentConstructor, PostCond.class);
124: if (post != null) {
125: if (DesignByContractAspect.verbose)
126: System.out
127: .println("[dbc] Found postconditions in method: "
128: + currentConstructor);
129: addConstructorConditions(realConstructor, postConds, post
130: .value());
131: }
132: }
133:
134: private static ArrayList addConstructorConditions(
135: Constructor realConstructor, ArrayList conditions,
136: String[] exprs) {
137: if (exprs == null) {
138: return conditions;
139: }
140:
141: for (int i = 0; i < exprs.length; i++) {
142: conditions.add(new ConstructorCondition(realConstructor,
143: exprs[i]));
144: }
145:
146: return conditions;
147: }
148:
149: private static Constructor findConstructorInClass(Class clazz,
150: Constructor constructor) {
151: String name = constructor.getName();
152: Constructor[] constructors = clazz.getDeclaredConstructors();
153: for (int i = 0; i < constructors.length; i++) {
154: if (constructors[i].getName().equals(name)) {
155: Class[] soughtParams = constructor.getParameterTypes();
156: Class[] foundParams = constructors[i]
157: .getParameterTypes();
158:
159: if (soughtParams.length == foundParams.length) {
160: for (int j = 0; j < soughtParams.length; j++) {
161: if (soughtParams[j] != foundParams[j]) {
162: break;
163: }
164: }
165:
166: return constructors[i];
167: }
168: }
169: }
170: return null;
171: }
172:
173: }
|