Skip to content

Instantly share code, notes, and snippets.

@michaelrice
Created February 8, 2015 16:33
Show Gist options
  • Select an option

  • Save michaelrice/1f291d58a1d0328983ef to your computer and use it in GitHub Desktop.

Select an option

Save michaelrice/1f291d58a1d0328983ef to your computer and use it in GitHub Desktop.
assertion failure
public class ReflectUtil {
public static Field[] getAllFields(Class<?> c) {
List<Field> listOfFields = new ArrayList<Field>();
getAllFields(c, listOfFields);
Field[] arrayOfFields = new Field[listOfFields.size()];
listOfFields.toArray(arrayOfFields);
return arrayOfFields;
}
private static void getAllFields(Class<?> clazz, List<Field> listOfFields) {
Class<?> superClass = clazz.getSuperclass();
if (superClass != null) {
getAllFields(superClass, listOfFields);
}
Field[] fields = clazz.getDeclaredFields();
Collections.addAll(listOfFields, fields);
}
}
@Test
public void test_GetAllFields_Returns_All_Fields_With_Interface() throws Exception {
class MySuperClass implements SimpleInterface {
public String baz;
public void myBaz() {}
@Override
public void helloWorld() {}
}
class MyClass extends MySuperClass {
public String foo;
public int[] bar;
public void myFoo() {}
private int myBar;
}
Field[] fields = ReflectUtil.getAllFields(MyClass.class);
// I keep getting a fail here fields.length == 6 not 7
assertEquals(fields.length, 7);
}
public interface SimpleInterface {
public void helloWorld();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment