1 17 18 package org.objectweb.jac.aspects.integrity; 19 20 import java.util.Iterator ; 21 import java.util.Vector ; 22 import org.aopalliance.intercept.ConstructorInvocation; 23 import org.aopalliance.intercept.MethodInvocation; 24 import org.apache.log4j.Logger; 25 import org.objectweb.jac.core.AspectComponent; 26 import org.objectweb.jac.core.Interaction; 27 import org.objectweb.jac.core.Wrapper; 28 import org.objectweb.jac.core.rtti.ClassItem; 29 import org.objectweb.jac.core.rtti.ClassRepository; 30 import org.objectweb.jac.core.rtti.FieldItem; 31 import org.objectweb.jac.core.rtti.MethodItem; 32 33 37 38 public class ConstraintWrapper extends Wrapper { 39 static final Logger logger = Logger.getLogger("integrity.conditions"); 40 41 public ConstraintWrapper(AspectComponent ac) { 42 super(ac); 43 } 44 45 private void invokeTest( 46 MethodItem method, 47 FieldItem field, 48 Object [] args, 49 String errorMsg) 50 throws Exception 51 { 52 Boolean result = (Boolean ) method.invoke(null, args); 53 54 if (result == null) 55 throw new Exception ("The constraint " + method + " does not exist"); 56 57 if (!result.booleanValue()) { 58 if ((errorMsg == null) || errorMsg.length() == 0) 59 throw new Exception ( 60 "The constraint " 61 + method 62 + " has not been validated for " 63 + field); 64 throw new Exception (field + ": " + errorMsg); 65 } 66 } 67 68 72 73 public Object testConditions(Interaction interaction) throws Exception { 74 logger.debug( 75 "entering test conditions for " + interaction.method); 76 77 IntegrityAC ac = (IntegrityAC) getAspectComponent(); 78 String fieldName = 79 ((MethodItem) interaction.method).getWrittenFields()[0].getName(); 80 ClassItem curClass = 81 ClassRepository.get().getClass(interaction.wrappee); 82 FieldItem field = null; 83 Vector preList = null; 84 85 while (preList == null && curClass != null) { 87 field = curClass.getField(fieldName); 88 preList = (Vector ) ac.preConditionsFields.get(field); 89 logger.debug( 90 field.getClassItem() + "." + field + " => " + preList); 91 curClass = curClass.getSuperclass(); 92 } 93 94 Vector postList = null; 95 curClass = ClassRepository.get().getClass(interaction.wrappee); 96 97 while (postList == null && curClass != null) { 99 field = curClass.getField(fieldName); 100 postList = (Vector ) ac.postConditionsFields.get(field); 101 logger.debug( 102 field.getClassItem() + "." + field + " => " + preList); 103 curClass = curClass.getSuperclass(); 104 } 105 106 if (preList != null) { 107 Iterator i = preList.iterator(); 108 Object currentFieldValue = 109 field.getThroughAccessor(interaction.wrappee); 110 while (i.hasNext()) { 111 ConstraintDescriptor conditionToTest = 112 (ConstraintDescriptor) i.next(); 113 logger.debug( 114 "testing pre " + conditionToTest.getConstraint()); 115 invokeTest( 116 conditionToTest.getConstraint(), 117 field, 118 new Object [] { 119 interaction.wrappee, 120 field, 121 currentFieldValue, 122 conditionToTest.getParams()}, 123 conditionToTest.getErrorMsg()); 124 } 125 } 126 127 if (postList != null) { 128 Iterator i = postList.iterator(); 129 while (i.hasNext()) { 130 ConstraintDescriptor conditionToTest = 131 (ConstraintDescriptor) i.next(); 132 logger.debug( 133 "testing post " + conditionToTest.getConstraint()); 134 invokeTest( 135 conditionToTest.getConstraint(), 136 field, 137 new Object [] { 138 interaction.wrappee, 139 field, 140 interaction.args[0], 141 conditionToTest.getParams()}, 142 conditionToTest.getErrorMsg()); 143 } 144 } 145 146 return proceed(interaction); 147 } 148 149 public Object invoke(MethodInvocation invocation) throws Throwable { 150 return testConditions((Interaction) invocation); 151 } 152 } 153 | Popular Tags |