1 3 package org.jmock.util; 4 5 import java.lang.reflect.Field ; 6 import java.util.Vector ; 7 import junit.framework.Assert; 8 import org.jmock.core.Verifiable; 9 10 11 31 public class Verifier 32 { 33 34 private static Vector myProcessingObjects = new Vector (); 35 36 42 static synchronized public void verifyObject( Object anObject ) { 43 verifyFieldsForClass(anObject, anObject.getClass(), myProcessingObjects); 44 } 45 46 static private void verifyFieldsForClass( Object anObject, Class aClass, Vector alreadyProcessed ) { 47 if (alreadyProcessed.contains(anObject) || isBaseObjectClass(aClass)) { 48 return; 49 } 50 51 verifyFieldsForClass(anObject, aClass.getSuperclass(), alreadyProcessed); 52 try { 53 alreadyProcessed.addElement(anObject); 54 55 Field [] fields = aClass.getDeclaredFields(); 56 for (int i = 0; i < fields.length; ++i) { 57 verifyField(fields[i], anObject, alreadyProcessed); 58 } 59 } 60 finally { 61 alreadyProcessed.removeElement(anObject); 62 } 63 } 64 65 static private void verifyField( Field aField, Object anObject, Vector alreadyProcessed ) { 66 try { 67 aField.setAccessible(true); 68 Object fieldObject = aField.get(anObject); 69 70 if (isVerifiable(fieldObject) && !alreadyProcessed.contains(fieldObject)) { 71 ((Verifiable)fieldObject).verify(); 72 } 73 } 74 catch (IllegalAccessException e) { 75 Assert.fail("Could not access field " + aField.getName()); 76 } 77 } 78 79 private static boolean isVerifiable( Object anObject ) { 80 return anObject instanceof Verifiable; 81 } 82 83 private static boolean isBaseObjectClass( Class aClass ) { 84 return aClass.equals(Object .class); 85 } 86 } 87 | Popular Tags |