Created
May 25, 2015 02:31
-
-
Save toagit/50be438d3287a0ef10e0 to your computer and use it in GitHub Desktop.
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
/** | |
* boolean型Privateフィールド取得 | |
* @throws Exception JUnitでの使用を想定しているため、例外は全てスローする。 | |
*/ | |
private boolean getBooleanByReflection(Class<?> clazz, Object obj String name) throws Exception { | |
Field field = clazz.getDeclaredField(name); | |
field.setAccessible(true); | |
return field.getBoolean(obj); | |
} | |
/** | |
* String型Privateフィールド取得 | |
* @throws Exception JUnitでの使用を想定しているため、例外は全てスローする。 | |
*/ | |
private boolean getStringByReflection(Class<?> clazz, Object obj String name) throws Exception { | |
Field field = clazz.getDeclaredField(name); | |
field.setAccessible(true); | |
return field.get(obj).toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment