Skip to content

Instantly share code, notes, and snippets.

@logcat
Created July 8, 2015 11:52
Show Gist options
  • Save logcat/ea815a623dedb3cad3e5 to your computer and use it in GitHub Desktop.
Save logcat/ea815a623dedb3cad3e5 to your computer and use it in GitHub Desktop.
public static Field getField(Class<?> clazz, String fieldName) {
Class<?> tmpClass = clazz;
do {
try {
Field f = tmpClass.getDeclaredField(fieldName);
f.setAccessible(true);
return f;
} catch (NoSuchFieldException e) {
tmpClass = tmpClass.getSuperclass();
}
} while (tmpClass != null);
throw new RuntimeException("Field '" + fieldName + "' not found on class " + clazz);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment