Skip to content

Instantly share code, notes, and snippets.

@avalanchas
Last active October 7, 2019 13:01
Show Gist options
  • Save avalanchas/90f620480471bba78ab1b19c4fd1fd54 to your computer and use it in GitHub Desktop.
Save avalanchas/90f620480471bba78ab1b19c4fd1fd54 to your computer and use it in GitHub Desktop.
How to access private final fields in a private static final inner class via reflection
public class MyApplication extends Application {
...
@SuppressWarnings({"unchecked", "ConstantConditions"})
private void handleDownloadManagerCrash(Thread thread, Throwable throwable) {
ArrayList<Download> downloads = null;
HashMap activeTasks = null;
try {
Field innerClassField = mDownloadManager.getClass().getDeclaredField("internalHandler");
innerClassField.setAccessible(true);
Object internalHandler = innerClassField.get(mDownloadManager);
Field activeTasksField = internalHandler.getClass().getDeclaredField("activeTasks");
Field downloadsField = internalHandler.getClass().getDeclaredField("downloads");
activeTasksField.setAccessible(true);
downloadsField.setAccessible(true);
activeTasks = (HashMap) activeTasksField.get(internalHandler);
downloads = (ArrayList) downloadsField.get(internalHandler);
} catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException | NullPointerException | SecurityException e) {
// Failed
}
Log.d("TAG", String.format("Handled crash, fields are -> downloads=[%s], activeTasks=[%s]",
downloads, activeTasks));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment