Created
May 24, 2012 04:08
-
-
Save STAR-ZERO/2779365 to your computer and use it in GitHub Desktop.
スーパークラスも含めてフィールド値をデバッグするtoString
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
@Override | |
public String toString() { | |
StringBuffer sb = new StringBuffer(); | |
sb.append(this.getClass().getSimpleName() + "["); | |
Class<?> clazz = this.getClass(); | |
while (clazz != null) { | |
Field[] fields = clazz.getDeclaredFields(); | |
for (Field field: fields) { | |
try { | |
field.setAccessible(true); | |
sb.append(field.getName() + "=" + field.get(this) + ","); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
clazz = clazz.getSuperclass(); | |
} | |
sb.append("]"); | |
return sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment