Created
March 13, 2018 15:04
-
-
Save micer/05656834dd22cc9b3ab62cf4efd25666 to your computer and use it in GitHub Desktop.
Get name of error code static final variable using reflection
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
public static String getErrorCodeName(int errorCode) { | |
Field[] fields = ErrorCodes.class.getDeclaredFields(); | |
for (Field f : fields) { | |
if (Modifier.isStatic(f.getModifiers()) | |
&& Modifier.isFinal(f.getModifiers())) { | |
try { | |
int value = f.getInt(new Object()); | |
if (value == errorCode) { | |
return f.getName(); | |
} | |
} catch (IllegalAccessException e) { | |
AxisLogger.instance().e(TAG, e.getMessage(), e); | |
} | |
} | |
} | |
return ERROR_UNKNOWN; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment