Last active
October 7, 2019 13:01
-
-
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
This file contains hidden or 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
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