Skip to content

Instantly share code, notes, and snippets.

@micer
Created March 13, 2018 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micer/05656834dd22cc9b3ab62cf4efd25666 to your computer and use it in GitHub Desktop.
Save micer/05656834dd22cc9b3ab62cf4efd25666 to your computer and use it in GitHub Desktop.
Get name of error code static final variable using reflection
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