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.util.ArrayList;
025: import java.util.HashMap;
026:
027: import org.jboss.aop.annotation.AnnotationElement;
028: import org.jboss.aspects.dbc.DesignByContractAspect;
029: import org.jboss.aspects.dbc.Invariant;
030: import org.jboss.aspects.dbc.StaticInvariant;
031:
032: /**
033: *
034: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
035: * @version $Revision: 57186 $
036: */
037: public class ConditionManager {
038: public static HashMap preConditions = new HashMap();
039: public static HashMap postConditions = new HashMap();
040: public static HashMap invariants = new HashMap();
041:
042: public static synchronized InvariantCondition[] getInvariants(
043: Class clazz) {
044: InvariantCondition[] inv = (InvariantCondition[]) invariants
045: .get(clazz);
046: if (inv != null) {
047: return inv;
048: }
049:
050: initialise(clazz);
051: return (InvariantCondition[]) invariants.get(clazz);
052: }
053:
054: protected static void initialise(Class clazz) {
055: ArrayList invConds = new ArrayList();
056:
057: if (invariants.get(clazz) != null) {
058: if (DesignByContractAspect.verbose)
059: System.out
060: .println("[dbc] Already have invariants for class: "
061: + clazz);
062: }
063:
064: if (DesignByContractAspect.verbose)
065: System.out
066: .println("[dbc] ===== Initialising invariants for class: "
067: + clazz);
068:
069: //We need the @Invariant for this class and the super classes
070: Class curClazz = clazz;
071:
072: while (curClazz != null) {
073: addInvariantConditions(invConds, curClazz);
074:
075: Class[] interfaces = curClazz.getInterfaces();
076: for (int i = 0; i < interfaces.length; i++) {
077: addInvariantConditions(invConds, interfaces[i]);
078: }
079:
080: curClazz = curClazz.getSuperclass();
081: }
082:
083: InvariantCondition[] inv = (InvariantCondition[]) invConds
084: .toArray(new InvariantCondition[invConds.size()]);
085: invariants.put(clazz, inv);
086: }
087:
088: private static void addInvariantConditions(ArrayList conditions,
089: Class clazz) {
090: Invariant inv = (Invariant) AnnotationElement.getAnyAnnotation(
091: clazz, Invariant.class);
092: if (inv != null) {
093: if (DesignByContractAspect.verbose)
094: System.out
095: .println("[dbc] Found non-static invariants in class: "
096: + clazz);
097: String[] exprs = inv.value();
098: if (exprs != null) {
099: for (int i = 0; i < exprs.length; i++) {
100: conditions.add(new InvariantCondition(clazz,
101: exprs[i], false));
102: }
103: }
104: }
105:
106: StaticInvariant statinv = (StaticInvariant) AnnotationElement
107: .getAnyAnnotation(clazz, StaticInvariant.class);
108: if (statinv != null) {
109: if (DesignByContractAspect.verbose)
110: System.out
111: .println("[dbc] Found static invariants in class: "
112: + clazz);
113: String[] exprs = statinv.value();
114: if (exprs != null) {
115: for (int i = 0; i < exprs.length; i++) {
116: conditions.add(new InvariantCondition(clazz,
117: exprs[i], true));
118: }
119: }
120: }
121: }
122: }
|