Created
December 24, 2017 14:29
-
-
Save guyca/9834c9c44de003ff30cf77bec9782c39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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