Skip to content

Instantly share code, notes, and snippets.

@toagit
Created May 25, 2015 02:31
Show Gist options
  • Save toagit/50be438d3287a0ef10e0 to your computer and use it in GitHub Desktop.
Save toagit/50be438d3287a0ef10e0 to your computer and use it in GitHub Desktop.
Reflectionサンプル
/**
* 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