This function is called by various TestCase implementations, at tearDown() time, in order
to scrub out any class variables. This protects against memory leaks in the case where a
test case creates a non-static inner class (thus referencing the test case) and gives it to
someone else to hold onto.
final Field[] fields = getClass().getDeclaredFields();
for (Field field : fields) {
final Class<?> fieldClass = field.getDeclaringClass();
if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive()
&& (field.getModifiers() & Modifier.FINAL) == 0) {
try {
field.setAccessible(true);
field.set(this, null);
} catch (Exception e) {
android.util.Log.d("TestCase", "Error: Could not nullify field!");
}
if (field.get(this) != null) {
android.util.Log.d("TestCase", "Error: Could not nullify field!");
}
}
}