Skip to content

Instantly share code, notes, and snippets.

@guyca
Created December 24, 2017 14:29
Show Gist options
  • Save guyca/9834c9c44de003ff30cf77bec9782c39 to your computer and use it in GitHub Desktop.
Save guyca/9834c9c44de003ff30cf77bec9782c39 to your computer and use it in GitHub Desktop.
import android.support.annotation.Nullable;
import java.lang.reflect.Field;
class ReflectionUtils {
@Nullable
static Object getDeclaredField(Object obj, String fieldName) {
try {
Field f = getField(obj.getClass(), fieldName);
if (f == null) {
return null;
}
f.setAccessible(true);
return f.get(obj);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static Field getField(Class clazz, String name) {
try {
return clazz.getDeclaredField(name);
} catch (NoSuchFieldException nsfe) {
return getField(clazz.getSuperclass(), name);
} catch (Exception e) {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment