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