Skip to content

Instantly share code, notes, and snippets.

@micer
Created March 13, 2018 15:04
Show Gist options
  • 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